This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 651
/
goMain.ts
369 lines (321 loc) · 15 KB
/
goMain.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import vscode = require('vscode');
import { GoCompletionItemProvider } from './goSuggest';
import { GoHoverProvider } from './goExtraInfo';
import { GoDefinitionProvider } from './goDeclaration';
import { GoReferenceProvider } from './goReferences';
import { GoDocumentFormattingEditProvider, Formatter } from './goFormat';
import { GoRenameProvider } from './goRename';
import { GoDocumentSymbolProvider } from './goOutline';
import { GoRunTestCodeLensProvider } from './goRunTestCodelens';
import { GoSignatureHelpProvider } from './goSignature';
import { GoWorkspaceSymbolProvider } from './goSymbol';
import { GoCodeActionProvider } from './goCodeAction';
import { check, ICheckResult, removeTestStatus } from './goCheck';
import { updateGoPathGoRootFromConfig, offerToInstallTools } from './goInstallTools';
import { GO_MODE } from './goMode';
import { showHideStatus } from './goStatus';
import { coverageCurrentPackage, getCodeCoverage, removeCodeCoverage } from './goCover';
import { testAtCursor, testCurrentPackage, testCurrentFile, testPrevious, showTestOutput, testWorkspace } from './goTest';
import * as goGenerateTests from './goGenerateTests';
import { addImport } from './goImport';
import { installAllTools, checkLanguageServer } from './goInstallTools';
import { isGoPathSet, getBinPath, sendTelemetryEvent } from './util';
import { LanguageClient } from 'vscode-languageclient';
import { clearCacheForTools } from './goPath';
import { addTags, removeTags } from './goModifytags';
import { parseLiveFile } from './goLiveErrors';
import { GoCodeLensProvider } from './goCodelens';
export let errorDiagnosticCollection: vscode.DiagnosticCollection;
let warningDiagnosticCollection: vscode.DiagnosticCollection;
export function activate(ctx: vscode.ExtensionContext): void {
let useLangServer = vscode.workspace.getConfiguration('go')['useLanguageServer'];
let langServerFlags: string[] = vscode.workspace.getConfiguration('go')['languageServerFlags'] || [];
let toolsGopath = vscode.workspace.getConfiguration('go')['toolsGopath'];
updateGoPathGoRootFromConfig().then(() => {
offerToInstallTools();
let langServerAvailable = checkLanguageServer();
if (langServerAvailable) {
let langServerFlags: string[] = vscode.workspace.getConfiguration('go')['languageServerFlags'] || [];
const c = new LanguageClient(
'go-langserver',
{
command: getBinPath('go-langserver'),
args: ['-mode=stdio', ...langServerFlags],
},
{
documentSelector: ['go'],
uriConverters: {
// Apply file:/// scheme to all file paths.
code2Protocol: (uri: vscode.Uri): string => (uri.scheme ? uri : uri.with({ scheme: 'file' })).toString(),
protocol2Code: (uri: string) => vscode.Uri.parse(uri),
},
}
);
ctx.subscriptions.push(c.start());
} else {
ctx.subscriptions.push(vscode.languages.registerHoverProvider(GO_MODE, new GoHoverProvider()));
ctx.subscriptions.push(vscode.languages.registerDefinitionProvider(GO_MODE, new GoDefinitionProvider()));
ctx.subscriptions.push(vscode.languages.registerReferenceProvider(GO_MODE, new GoReferenceProvider()));
ctx.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(GO_MODE, new GoDocumentSymbolProvider()));
ctx.subscriptions.push(vscode.languages.registerWorkspaceSymbolProvider(new GoWorkspaceSymbolProvider()));
ctx.subscriptions.push(vscode.languages.registerSignatureHelpProvider(GO_MODE, new GoSignatureHelpProvider(), '(', ','));
}
if (vscode.window.activeTextEditor && isGoPathSet()) {
runBuilds(vscode.window.activeTextEditor.document, vscode.workspace.getConfiguration('go'));
}
});
ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(GO_MODE, new GoDocumentFormattingEditProvider()));
ctx.subscriptions.push(vscode.languages.registerRenameProvider(GO_MODE, new GoRenameProvider()));
ctx.subscriptions.push(vscode.languages.registerCodeActionsProvider(GO_MODE, new GoCodeActionProvider()));
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, new GoRunTestCodeLensProvider()));
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, new GoCodeLensProvider()));
errorDiagnosticCollection = vscode.languages.createDiagnosticCollection('go-error');
ctx.subscriptions.push(errorDiagnosticCollection);
warningDiagnosticCollection = vscode.languages.createDiagnosticCollection('go-warning');
ctx.subscriptions.push(warningDiagnosticCollection);
vscode.workspace.onDidChangeTextDocument(removeCodeCoverage, null, ctx.subscriptions);
vscode.workspace.onDidChangeTextDocument(removeTestStatus, null, ctx.subscriptions);
vscode.window.onDidChangeActiveTextEditor(showHideStatus, null, ctx.subscriptions);
vscode.window.onDidChangeActiveTextEditor(getCodeCoverage, null, ctx.subscriptions);
vscode.workspace.onDidChangeTextDocument(parseLiveFile, null, ctx.subscriptions);
startBuildOnSaveWatcher(ctx.subscriptions);
ctx.subscriptions.push(vscode.commands.registerCommand('go.gopath', () => {
let gopath = process.env['GOPATH'];
let wasInfered = vscode.workspace.getConfiguration('go')['inferGopath'];
// not only if it was configured, but if it was successful.
if (wasInfered && vscode.workspace.rootPath.indexOf(gopath) === 0) {
vscode.window.showInformationMessage('Current GOPATH is inferred from workspace root: ' + gopath);
} else {
vscode.window.showInformationMessage('Current GOPATH: ' + gopath);
}
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.add.tags', (args) => {
addTags(args);
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.remove.tags', (args) => {
removeTags(args);
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.cursor', (args) => {
let goConfig = vscode.workspace.getConfiguration('go');
testAtCursor(goConfig, args);
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.package', (args) => {
let goConfig = vscode.workspace.getConfiguration('go');
testCurrentPackage(goConfig, args);
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.file', (args) => {
let goConfig = vscode.workspace.getConfiguration('go');
testCurrentFile(goConfig, args);
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.workspace', (args) => {
let goConfig = vscode.workspace.getConfiguration('go');
testWorkspace(goConfig, args);
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.previous', () => {
testPrevious();
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.coverage', () => {
coverageCurrentPackage();
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.showOutput', () => {
showTestOutput();
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.import.add', (arg: string) => {
return addImport(typeof arg === 'string' ? arg : null);
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.tools.install', () => {
installAllTools();
}));
ctx.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => {
let updatedGoConfig = vscode.workspace.getConfiguration('go');
sendTelemetryEventForConfig(updatedGoConfig);
updateGoPathGoRootFromConfig();
// If there was a change in "useLanguageServer" setting, then ask the user to reload VS Code.
if (process.platform !== 'win32'
&& didLangServerConfigChange(useLangServer, langServerFlags, updatedGoConfig)
&& (!updatedGoConfig['useLanguageServer'] || checkLanguageServer())) {
vscode.window.showInformationMessage('Reload VS Code window for the change in usage of language server to take effect', 'Reload').then(selected => {
if (selected === 'Reload') {
vscode.commands.executeCommand('workbench.action.reloadWindow');
}
});
}
useLangServer = updatedGoConfig['useLanguageServer'];
// If there was a change in "toolsGopath" setting, then clear cache for go tools
if (toolsGopath !== updatedGoConfig['toolsGopath']) {
clearCacheForTools();
toolsGopath = updatedGoConfig['toolsGopath'];
}
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.generate.package', () => {
goGenerateTests.generateTestCurrentPackage();
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.generate.file', () => {
goGenerateTests.generateTestCurrentFile();
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.generate.function', () => {
goGenerateTests.generateTestCurrentFunction();
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.toggle.test.file', () => {
goGenerateTests.toggleTestFile();
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.debug.startSession', config => {
if (!config.request) { // if 'request' is missing interpret this as a missing launch.json
config = Object.assign(config, {
'name': 'Launch',
'type': 'go',
'request': 'launch',
'mode': 'debug',
'program': '${file}',
'env': {
'GOPATH': process.env['GOPATH']
}
});
}
vscode.commands.executeCommand('vscode.startDebug', config);
}));
vscode.languages.setLanguageConfiguration(GO_MODE.language, {
indentationRules: {
// ^(.*\*/)?\s*\}.*$
decreaseIndentPattern: /^(.*\*\/)?\s*\}.*$/,
// ^.*\{[^}'']*$
increaseIndentPattern: /^.*\{[^}'']*$/
},
wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,
});
sendTelemetryEventForConfig(vscode.workspace.getConfiguration('go'));
}
function deactivate() {
}
function runBuilds(document: vscode.TextDocument, goConfig: vscode.WorkspaceConfiguration) {
function mapSeverityToVSCodeSeverity(sev: string) {
switch (sev) {
case 'error': return vscode.DiagnosticSeverity.Error;
case 'warning': return vscode.DiagnosticSeverity.Warning;
default: return vscode.DiagnosticSeverity.Error;
}
}
if (document.languageId !== 'go') {
return;
}
let uri = document.uri;
check(uri.fsPath, goConfig).then(errors => {
errorDiagnosticCollection.clear();
warningDiagnosticCollection.clear();
let diagnosticMap: Map<string, Map<vscode.DiagnosticSeverity, vscode.Diagnostic[]>> = new Map();
errors.forEach(error => {
let canonicalFile = vscode.Uri.file(error.file).toString();
let startColumn = 0;
let endColumn = 1;
if (document && document.uri.toString() === canonicalFile) {
let range = new vscode.Range(error.line - 1, 0, error.line - 1, document.lineAt(error.line - 1).range.end.character + 1);
let text = document.getText(range);
let [_, leading, trailing] = /^(\s*).*(\s*)$/.exec(text);
startColumn = leading.length;
endColumn = text.length - trailing.length;
}
let range = new vscode.Range(error.line - 1, startColumn, error.line - 1, endColumn);
let severity = mapSeverityToVSCodeSeverity(error.severity);
let diagnostic = new vscode.Diagnostic(range, error.msg, severity);
let diagnostics = diagnosticMap.get(canonicalFile);
if (!diagnostics) {
diagnostics = new Map<vscode.DiagnosticSeverity, vscode.Diagnostic[]>();
}
if (!diagnostics[severity]) {
diagnostics[severity] = [];
}
diagnostics[severity].push(diagnostic);
diagnosticMap.set(canonicalFile, diagnostics);
});
diagnosticMap.forEach((diagMap, file) => {
errorDiagnosticCollection.set(vscode.Uri.parse(file), diagMap[vscode.DiagnosticSeverity.Error]);
warningDiagnosticCollection.set(vscode.Uri.parse(file), diagMap[vscode.DiagnosticSeverity.Warning]);
});
}).catch(err => {
vscode.window.showInformationMessage('Error: ' + err);
});
}
function startBuildOnSaveWatcher(subscriptions: vscode.Disposable[]) {
// TODO: This is really ugly. I'm not sure we can do better until
// Code supports a pre-save event where we can do the formatting before
// the file is written to disk.
let ignoreNextSave = new WeakSet<vscode.TextDocument>();
vscode.workspace.onDidSaveTextDocument(document => {
if (document.languageId !== 'go' || ignoreNextSave.has(document)) {
return;
}
let goConfig = vscode.workspace.getConfiguration('go');
let textEditor = vscode.window.activeTextEditor;
let formatPromise: PromiseLike<void> = Promise.resolve();
if (goConfig['formatOnSave'] && textEditor.document === document) {
let formatter = new Formatter();
formatPromise = formatter.formatDocument(document).then(edits => {
return textEditor.edit(editBuilder => {
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
});
}).then(applied => {
ignoreNextSave.add(document);
return document.save();
}).then(() => {
ignoreNextSave.delete(document);
}, () => {
// Catch any errors and ignore so that we still trigger
// the file save.
});
}
formatPromise.then(() => {
runBuilds(document, goConfig);
});
}, null, subscriptions);
}
function sendTelemetryEventForConfig(goConfig: vscode.WorkspaceConfiguration) {
sendTelemetryEvent('goConfig', {
buildOnSave: goConfig['buildOnSave'] + '',
buildFlags: goConfig['buildFlags'],
buildTags: goConfig['buildTags'],
formatOnSave: goConfig['formatOnSave'] + '',
formatTool: goConfig['formatTool'],
formatFlags: goConfig['formatFlags'],
lintOnSave: goConfig['lintOnSave'] + '',
lintFlags: goConfig['lintFlags'],
lintTool: goConfig['lintTool'],
vetOnSave: goConfig['vetOnSave'] + '',
vetFlags: goConfig['vetFlags'],
testOnSave: goConfig['testOnSave'] + '',
testFlags: goConfig['testFlags'],
coverOnSave: goConfig['coverOnSave'] + '',
useDiffForFormatting: goConfig['useDiffForFormatting'] + '',
gopath: goConfig['gopath'] ? 'set' : '',
goroot: goConfig['goroot'] ? 'set' : '',
inferGopath: goConfig['inferGopath'] + '',
toolsGopath: goConfig['toolsGopath'] ? 'set' : '',
gocodeAutoBuild: goConfig['gocodeAutoBuild'] + '',
useCodeSnippetsOnFunctionSuggest: goConfig['useCodeSnippetsOnFunctionSuggest'] + '',
autocompleteUnimportedPackages: goConfig['autocompleteUnimportedPackages'] + '',
docsTool: goConfig['docsTool'],
useLanguageServer: goConfig['useLanguageServer'] + '',
includeImports: goConfig['gotoSymbol'] && goConfig['gotoSymbol']['includeImports'] + '',
addTags: JSON.stringify(goConfig['addTags']),
removeTags: JSON.stringify(goConfig['removeTags']),
editorContextMenuCommands: JSON.stringify(goConfig['editorContextMenuCommands']),
liveErrors: JSON.stringify(goConfig['liveErrors'])
});
}
function didLangServerConfigChange(useLangServer: boolean, langServerFlags: string[], newconfig: vscode.WorkspaceConfiguration) {
let newLangServerFlags = newconfig['languageServerFlags'] || [];
if (useLangServer !== newconfig['useLanguageServer'] || langServerFlags.length !== newLangServerFlags.length) {
return true;
}
for (let i = 0; i < langServerFlags.length; i++) {
if (newLangServerFlags[i] !== langServerFlags[i]) {
return true;
}
}
return false;
}