forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goCheck.ts
140 lines (123 loc) · 4.28 KB
/
goCheck.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable @typescript-eslint/no-explicit-any */
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import path = require('path');
import vscode = require('vscode');
import { getGoplsConfig } from './config';
import { goBuild } from './goBuild';
import { buildLanguageServerConfig } from './goLanguageServer';
import { goLint } from './goLint';
import { buildDiagnosticCollection, lintDiagnosticCollection, vetDiagnosticCollection } from './goMain';
import { isModSupported } from './goModules';
import { diagnosticsStatusBarItem, outputChannel } from './goStatus';
import { goVet } from './goVet';
import { getTestFlags, goTest, TestConfig } from './testUtils';
import { ICheckResult } from './util';
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
statusBarItem.command = 'go.test.showOutput';
const neverAgain = { title: "Don't Show Again" };
export function removeTestStatus(e: vscode.TextDocumentChangeEvent) {
if (e.document.isUntitled) {
return;
}
statusBarItem.hide();
statusBarItem.text = '';
}
export function notifyIfGeneratedFile(this: void, e: vscode.TextDocumentChangeEvent) {
const ctx: any = this;
if (e.document.isUntitled || e.document.languageId !== 'go') {
return;
}
if (
ctx.globalState.get('ignoreGeneratedCodeWarning') !== true &&
e.document.lineAt(0).text.match(/^\/\/ Code generated .* DO NOT EDIT\.$/)
) {
vscode.window.showWarningMessage('This file seems to be generated. DO NOT EDIT.', neverAgain).then((result) => {
if (result === neverAgain) {
ctx.globalState.update('ignoreGeneratedCodeWarning', true);
}
});
}
}
interface IToolCheckResults {
diagnosticCollection: vscode.DiagnosticCollection;
errors: ICheckResult[];
}
export function check(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration): Promise<IToolCheckResults[]> {
diagnosticsStatusBarItem.hide();
outputChannel.clear();
const runningToolsPromises = [];
const cwd = path.dirname(fileUri.fsPath);
// If a user has enabled diagnostics via a language server,
// then we disable running build or vet to avoid duplicate errors and warnings.
const lspConfig = buildLanguageServerConfig(goConfig);
const disableBuildAndVet = lspConfig.enabled && lspConfig.features.diagnostics;
let testPromise: Thenable<boolean>;
const testConfig: TestConfig = {
goConfig,
dir: cwd,
flags: getTestFlags(goConfig),
background: true,
applyCodeCoverage: !!goConfig['coverOnSave']
};
const runTest = () => {
if (testPromise) {
return testPromise;
}
testPromise = isModSupported(fileUri).then((isMod) => {
testConfig.isMod = isMod;
return goTest(testConfig);
});
return testPromise;
};
if (!disableBuildAndVet && !!goConfig['buildOnSave'] && goConfig['buildOnSave'] !== 'off') {
runningToolsPromises.push(
isModSupported(fileUri)
.then((isMod) => goBuild(fileUri, isMod, goConfig, goConfig['buildOnSave'] === 'workspace'))
.then((errors) => ({ diagnosticCollection: buildDiagnosticCollection, errors }))
);
}
if (goConfig['testOnSave']) {
statusBarItem.show();
statusBarItem.text = 'Tests Running';
runTest().then((success) => {
if (statusBarItem.text === '') {
return;
}
if (success) {
statusBarItem.text = 'Tests Passed';
} else {
statusBarItem.text = 'Tests Failed';
}
});
}
if (!!goConfig['lintOnSave'] && goConfig['lintOnSave'] !== 'off') {
const goplsConfig = getGoplsConfig(fileUri);
runningToolsPromises.push(
goLint(fileUri, goConfig, goplsConfig, goConfig['lintOnSave']).then((errors) => ({
diagnosticCollection: lintDiagnosticCollection,
errors
}))
);
}
if (!disableBuildAndVet && !!goConfig['vetOnSave'] && goConfig['vetOnSave'] !== 'off') {
runningToolsPromises.push(
goVet(fileUri, goConfig, goConfig['vetOnSave'] === 'workspace').then((errors) => ({
diagnosticCollection: vetDiagnosticCollection,
errors
}))
);
}
if (goConfig['coverOnSave']) {
runTest().then((success) => {
if (!success) {
return [];
}
});
}
return Promise.all(runningToolsPromises);
}