Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Commit 3a693e5

Browse files
segevfinerramya-rao-a
authored andcommitted
Add item documentation to completions (#2054)
* Add item documentation to completions Using `go doc` instead of `godoc` due to golang/go#25443 `go doc` doesn't seem to support giving the code of an unsaved file via stdin like `gocode`, so it won't display documentation for unsaved changes. Fixes #194 * Code review fixes for #2054 * Refactoring and comments * Always resolve the item * Prompt for updating gocode if the package property is missing
1 parent cb6257a commit 3a693e5

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

src/goSuggest.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ function vscodeKindFromGoCodeClass(kind: string, type: string): vscode.Completio
4141

4242
interface GoCodeSuggestion {
4343
class: string;
44+
package?: string;
4445
name: string;
4546
type: string;
4647
}
4748

49+
class ExtendedCompletionItem extends vscode.CompletionItem {
50+
package?: string;
51+
fileName: string;
52+
}
53+
4854
const lineCommentRegex = /^\s*\/\/\s+/;
4955
const exportedMemberRegex = /(const|func|type|var)(\s+\(.*\))?\s+([A-Z]\w*)/;
5056
const gocodeNoSupportForgbMsgKey = 'dontshowNoSupportForgb';
@@ -75,6 +81,49 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
7581
});
7682
}
7783

84+
public resolveCompletionItem(item: vscode.CompletionItem, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CompletionItem> {
85+
const goRuntimePath = getBinPath('go');
86+
if (!goRuntimePath || !(item instanceof ExtendedCompletionItem)) {
87+
return;
88+
}
89+
90+
if (typeof item.package === 'undefined') {
91+
promptForUpdatingTool('gocode');
92+
return;
93+
}
94+
95+
const env = getToolsEnvVars();
96+
const cwd = path.dirname(item.fileName);
97+
98+
return new Promise<vscode.CompletionItem>(resolve => {
99+
const args = ['doc', '-c', '-cmd', '-u'];
100+
if (item.package) {
101+
args.push(item.package, item.label);
102+
} else {
103+
// item.package would be empty for symbols from the current package
104+
args.push(item.label);
105+
}
106+
107+
cp.execFile(goRuntimePath, args, { cwd, env }, (err, stdout) => {
108+
if (err) {
109+
console.log(err);
110+
return resolve(item);
111+
}
112+
113+
let doc = '';
114+
const goDocLines = stdout.toString().split('\n');
115+
// i = 1 to skip the func signature line
116+
for (let i = 1; i < goDocLines.length && goDocLines[i].startsWith(' '); i++) {
117+
doc += goDocLines[i].substring(4) + '\n';
118+
}
119+
120+
item.documentation = doc;
121+
resolve(item);
122+
});
123+
});
124+
125+
}
126+
78127
public provideCompletionItemsInternal(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, config: vscode.WorkspaceConfiguration): Thenable<vscode.CompletionItem[] | vscode.CompletionList> {
79128
return this.ensureGoCodeConfigured(document.uri).then(() => {
80129
return new Promise<vscode.CompletionItem[] | vscode.CompletionList>((resolve, reject) => {
@@ -228,9 +277,11 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
228277
if (results && results[1]) {
229278
for (let suggest of results[1]) {
230279
if (inString && suggest.class !== 'import') continue;
231-
let item = new vscode.CompletionItem(suggest.name);
280+
let item = new ExtendedCompletionItem(suggest.name);
232281
item.kind = vscodeKindFromGoCodeClass(suggest.class, suggest.type);
233282
item.detail = suggest.type;
283+
item.package = suggest.package;
284+
item.fileName = document.fileName;
234285
if (inString && suggest.class === 'import') {
235286
item.textEdit = new vscode.TextEdit(
236287
new vscode.Range(
@@ -454,4 +505,4 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
454505
});
455506
return matchingPackages;
456507
}
457-
}
508+
}

0 commit comments

Comments
 (0)