@@ -41,10 +41,16 @@ function vscodeKindFromGoCodeClass(kind: string, type: string): vscode.Completio
41
41
42
42
interface GoCodeSuggestion {
43
43
class : string ;
44
+ package ?: string ;
44
45
name : string ;
45
46
type : string ;
46
47
}
47
48
49
+ class ExtendedCompletionItem extends vscode . CompletionItem {
50
+ package ?: string ;
51
+ fileName : string ;
52
+ }
53
+
48
54
const lineCommentRegex = / ^ \s * \/ \/ \s + / ;
49
55
const exportedMemberRegex = / ( c o n s t | f u n c | t y p e | v a r ) ( \s + \( .* \) ) ? \s + ( [ A - Z ] \w * ) / ;
50
56
const gocodeNoSupportForgbMsgKey = 'dontshowNoSupportForgb' ;
@@ -75,6 +81,49 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
75
81
} ) ;
76
82
}
77
83
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
+
78
127
public provideCompletionItemsInternal ( document : vscode . TextDocument , position : vscode . Position , token : vscode . CancellationToken , config : vscode . WorkspaceConfiguration ) : Thenable < vscode . CompletionItem [ ] | vscode . CompletionList > {
79
128
return this . ensureGoCodeConfigured ( document . uri ) . then ( ( ) => {
80
129
return new Promise < vscode . CompletionItem [ ] | vscode . CompletionList > ( ( resolve , reject ) => {
@@ -228,9 +277,11 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
228
277
if ( results && results [ 1 ] ) {
229
278
for ( let suggest of results [ 1 ] ) {
230
279
if ( inString && suggest . class !== 'import' ) continue ;
231
- let item = new vscode . CompletionItem ( suggest . name ) ;
280
+ let item = new ExtendedCompletionItem ( suggest . name ) ;
232
281
item . kind = vscodeKindFromGoCodeClass ( suggest . class , suggest . type ) ;
233
282
item . detail = suggest . type ;
283
+ item . package = suggest . package ;
284
+ item . fileName = document . fileName ;
234
285
if ( inString && suggest . class === 'import' ) {
235
286
item . textEdit = new vscode . TextEdit (
236
287
new vscode . Range (
@@ -454,4 +505,4 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
454
505
} ) ;
455
506
return matchingPackages ;
456
507
}
457
- }
508
+ }
0 commit comments