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 646
/
Copy pathgoSuggest.ts
330 lines (291 loc) · 13.1 KB
/
goSuggest.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
/*---------------------------------------------------------
* 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 cp = require('child_process');
import { getBinPath, parameters, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars, guessPackageNameFromFile, goBuiltinTypes, byteOffsetAt } from './util';
import { promptForMissingTool } from './goInstallTools';
import { getTextEditForAddImport } from './goImport';
import { getImportablePackages } from './goPackages';
function vscodeKindFromGoCodeClass(kind: string): vscode.CompletionItemKind {
switch (kind) {
case 'const':
return vscode.CompletionItemKind.Constant;
case 'package':
return vscode.CompletionItemKind.Module;
case 'type':
return vscode.CompletionItemKind.Class;
case 'func':
return vscode.CompletionItemKind.Function;
case 'var':
return vscode.CompletionItemKind.Variable;
case 'import':
return vscode.CompletionItemKind.Module;
}
return vscode.CompletionItemKind.Property; // TODO@EG additional mappings needed?
}
interface GoCodeSuggestion {
class: string;
name: string;
type: string;
}
export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
private pkgsList = new Map<string, string>();
public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> {
return this.provideCompletionItemsInternal(document, position, token, vscode.workspace.getConfiguration('go', document.uri));
}
public provideCompletionItemsInternal(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, config: vscode.WorkspaceConfiguration): Thenable<vscode.CompletionItem[]> {
return this.ensureGoCodeConfigured().then(() => {
return new Promise<vscode.CompletionItem[]>((resolve, reject) => {
let filename = document.fileName;
let lineText = document.lineAt(position.line).text;
let lineTillCurrentPosition = lineText.substr(0, position.character);
let autocompleteUnimportedPackages = config['autocompleteUnimportedPackages'] === true && !lineText.match(/^(\s)*(import|package)(\s)+/);
if (lineText.match(/^\s*\/\//)) {
return resolve([]);
}
let inString = isPositionInString(document, position);
if (!inString && lineTillCurrentPosition.endsWith('\"')) {
return resolve([]);
}
// get current word
let wordAtPosition = document.getWordRangeAtPosition(position);
let currentWord = '';
if (wordAtPosition && wordAtPosition.start.character < position.character) {
let word = document.getText(wordAtPosition);
currentWord = word.substr(0, position.character - wordAtPosition.start.character);
}
if (currentWord.match(/^\d+$/)) {
return resolve([]);
}
let offset = byteOffsetAt(document, position);
let inputText = document.getText();
let includeUnimportedPkgs = autocompleteUnimportedPackages && !inString;
return this.runGoCode(document, filename, inputText, offset, inString, position, lineText, currentWord, includeUnimportedPkgs).then(suggestions => {
// gocode does not suggest keywords, so we have to do it
if (currentWord.length > 0) {
goKeywords.forEach(keyword => {
if (keyword.startsWith(currentWord)) {
suggestions.push(new vscode.CompletionItem(keyword, vscode.CompletionItemKind.Keyword));
}
});
}
// If no suggestions and cursor is at a dot, then check if preceeding word is a package name
// If yes, then import the package in the inputText and run gocode again to get suggestions
if (suggestions.length === 0 && lineTillCurrentPosition.endsWith('.')) {
let pkgPath = this.getPackagePathFromLine(lineTillCurrentPosition);
if (pkgPath) {
// Now that we have the package path, import it right after the "package" statement
let { imports, pkg } = parseFilePrelude(vscode.window.activeTextEditor.document.getText());
let posToAddImport = document.offsetAt(new vscode.Position(pkg.start + 1, 0));
let textToAdd = `import "${pkgPath}"\n`;
inputText = inputText.substr(0, posToAddImport) + textToAdd + inputText.substr(posToAddImport);
offset += textToAdd.length;
// Now that we have the package imported in the inputText, run gocode again
return this.runGoCode(document, filename, inputText, offset, inString, position, lineText, currentWord, false).then(newsuggestions => {
// Since the new suggestions are due to the package that we imported,
// add additionalTextEdits to do the same in the actual document in the editor
// We use additionalTextEdits instead of command so that 'useCodeSnippetsOnFunctionSuggest' feature continues to work
newsuggestions.forEach(item => {
item.additionalTextEdits = getTextEditForAddImport(pkgPath);
});
resolve(newsuggestions);
});
}
}
resolve(suggestions);
});
});
});
}
private runGoCode(document: vscode.TextDocument, filename: string, inputText: string, offset: number, inString: boolean, position: vscode.Position, lineText: string, currentWord: string, includeUnimportedPkgs: boolean): Thenable<vscode.CompletionItem[]> {
return new Promise<vscode.CompletionItem[]>((resolve, reject) => {
let gocode = getBinPath('gocode');
// Unset GOOS and GOARCH for the `gocode` process to ensure that GOHOSTOS and GOHOSTARCH
// are used as the target operating system and architecture. `gocode` is unable to provide
// autocompletion when the Go environment is configured for cross compilation.
let env = Object.assign({}, getToolsEnvVars(), { GOOS: '', GOARCH: '' });
let stdout = '';
let stderr = '';
// Spawn `gocode` process
let p = cp.spawn(gocode, ['-f=json', 'autocomplete', filename, '' + offset], { env });
p.stdout.on('data', data => stdout += data);
p.stderr.on('data', data => stderr += data);
p.on('error', err => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gocode');
return reject();
}
return reject(err);
});
p.on('close', code => {
try {
if (code !== 0) {
return reject(stderr);
}
let results = <[number, GoCodeSuggestion[]]>JSON.parse(stdout.toString());
let suggestions = [];
let suggestionSet = new Set<string>();
let wordAtPosition = document.getWordRangeAtPosition(position);
if (results[1]) {
for (let suggest of results[1]) {
if (inString && suggest.class !== 'import') continue;
let item = new vscode.CompletionItem(suggest.name);
item.kind = vscodeKindFromGoCodeClass(suggest.class);
item.detail = suggest.type;
if (inString && suggest.class === 'import') {
item.textEdit = new vscode.TextEdit(
new vscode.Range(
position.line,
lineText.substring(0, position.character).lastIndexOf('"') + 1,
position.line,
position.character),
suggest.name
);
}
let conf = vscode.workspace.getConfiguration('go', vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri : null);
if ((conf.get('useCodeSnippetsOnFunctionSuggest') || conf.get('useCodeSnippetsOnFunctionSuggestWithoutType')) && suggest.class === 'func') {
let params = parameters(suggest.type.substring(4));
let paramSnippets = [];
for (let i = 0; i < params.length; i++) {
let param = params[i].trim();
if (param) {
param = param.replace('${', '\\${').replace('}', '\\}');
if (conf.get('useCodeSnippetsOnFunctionSuggestWithoutType')) {
if (param.includes(' ')) {
// Separate the variable name from the type
param = param.substr(0, param.indexOf(' '));
}
}
paramSnippets.push('${' + (i + 1) + ':' + param + '}');
}
}
item.insertText = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')');
}
if (wordAtPosition && wordAtPosition.start.character === 0 &&
suggest.class === 'type' && !goBuiltinTypes.has(suggest.name)) {
let auxItem = new vscode.CompletionItem(suggest.name + ' method', vscode.CompletionItemKind.Snippet);
auxItem.label = 'func (*' + suggest.name + ')';
auxItem.filterText = suggest.name;
auxItem.detail = 'Method snippet';
auxItem.sortText = 'b';
let prefix = 'func (' + suggest.name[0].toLowerCase() + ' *' + suggest.name + ')';
let snippet = prefix + ' ${1:methodName}(${2}) ${3} \{\n\t$0\n\}';
auxItem.insertText = new vscode.SnippetString(snippet);
suggestions.push(auxItem);
}
// Add same sortText to all suggestions from gocode so that they appear before the unimported packages
item.sortText = 'a';
suggestions.push(item);
suggestionSet.add(item.label);
};
}
// Add importable packages matching currentword to suggestions
let importablePkgs = includeUnimportedPkgs ? this.getMatchingPackages(currentWord, suggestionSet) : [];
suggestions = suggestions.concat(importablePkgs);
// 'Smart Snippet' for package clause
// TODO: Factor this out into a general mechanism
if (!inputText.match(/package\s+(\w+)/)) {
return guessPackageNameFromFile(filename).then((pkgNames: string[]) => {
pkgNames.forEach(pkgName => {
let packageItem = new vscode.CompletionItem('package ' + pkgName);
packageItem.kind = vscode.CompletionItemKind.Snippet;
packageItem.insertText = 'package ' + pkgName + '\r\n\r\n';
suggestions.push(packageItem);
});
resolve(suggestions);
}, () => resolve(suggestions));
}
resolve(suggestions);
} catch (e) {
reject(e);
}
});
p.stdin.end(inputText);
});
}
// TODO: Shouldn't lib-path also be set?
private ensureGoCodeConfigured(): Thenable<void> {
let setPkgsList = getImportablePackages(vscode.window.activeTextEditor.document.fileName, true).then(pkgMap => this.pkgsList = pkgMap);
let setGocodeProps = new Promise<void>((resolve, reject) => {
let gocode = getBinPath('gocode');
let goConfig = vscode.workspace.getConfiguration('go', vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri : null);
let env = getToolsEnvVars();
cp.execFile(gocode, ['set'], { env }, (err, stdout, stderr) => {
const existingOptions = stdout.split(/\r\n|\n/);
const optionsToSet: string[][] = [];
const setOption = () => {
const [name, value] = optionsToSet.pop();
cp.execFile(gocode, ['set', name, value], { env }, (err, stdout, stderr) => {
if (optionsToSet.length) {
setOption();
} else {
resolve();
}
});
};
if (existingOptions.indexOf('propose-builtins true') === -1) {
optionsToSet.push(['propose-builtins', 'true']);
}
if (existingOptions.indexOf(`autobuild ${goConfig['gocodeAutoBuild']}`) === -1) {
optionsToSet.push(['autobuild', goConfig['gocodeAutoBuild']]);
}
if (existingOptions.indexOf(`package-lookup-mode ${goConfig['gocodePackageLookupMode']}`) === -1) {
optionsToSet.push(['package-lookup-mode', goConfig['gocodePackageLookupMode']]);
}
if (!optionsToSet.length) {
return resolve();
}
setOption();
});
});
return Promise.all([setPkgsList, setGocodeProps]).then(() => {
return;
});
}
// Return importable packages that match given word as Completion Items
private getMatchingPackages(word: string, suggestionSet: Set<string>): vscode.CompletionItem[] {
if (!word) return [];
let completionItems = [];
this.pkgsList.forEach((pkgName: string, pkgPath: string) => {
if (pkgName.startsWith(word) && !suggestionSet.has(pkgName)) {
let item = new vscode.CompletionItem(pkgName, vscode.CompletionItemKind.Keyword);
item.detail = pkgPath;
item.documentation = 'Imports the package';
item.insertText = pkgName;
item.command = {
title: 'Import Package',
command: 'go.import.add',
arguments: [pkgPath]
};
item.kind = vscode.CompletionItemKind.Module;
// Add same sortText to the unimported packages so that they appear after the suggestions from gocode
const isStandardPackage = !item.detail.includes('.');
item.sortText = isStandardPackage ? 'za' : 'zb';
completionItems.push(item);
}
});
return completionItems;
}
// Given a line ending with dot, return the word preceeding the dot if it is a package name that can be imported
private getPackagePathFromLine(line: string): string {
let pattern = /(\w+)\.$/g;
let wordmatches = pattern.exec(line);
if (!wordmatches) {
return;
}
let [_, pkgNameFromWord] = wordmatches;
// Word is isolated. Now check pkgsList for a match
let matchingPackages = [];
this.pkgsList.forEach((pkgName: string, pkgPath: string) => {
if (pkgNameFromWord === pkgName) {
matchingPackages.push(pkgPath);
}
});
if (matchingPackages && matchingPackages.length === 1) {
return matchingPackages[0];
}
}
}