9
9
10
10
import cp = require( 'child_process' ) ;
11
11
import vscode = require( 'vscode' ) ;
12
+ import { ExecuteCommandRequest , ExecuteCommandParams } from 'vscode-languageserver-protocol' ;
12
13
import { toolExecutionEnvironment } from './goEnv' ;
13
14
import { promptForMissingTool } from './goInstallTools' ;
15
+ import { languageClient } from './goLanguageServer' ;
14
16
import { documentSymbols , GoOutlineImportsOptions } from './goOutline' ;
15
17
import { getImportablePackages } from './goPackages' ;
16
18
import { getBinPath , getImportPath , parseFilePrelude } from './util' ;
@@ -39,6 +41,31 @@ export async function listPackages(excludeImportedPkgs = false): Promise<string[
39
41
return [ ...stdLibs . sort ( ) , ...nonStdLibs . sort ( ) ] ;
40
42
}
41
43
44
+ async function golist ( ) : Promise < string [ ] > {
45
+ if ( languageClient ) {
46
+ try {
47
+ const uri = languageClient . code2ProtocolConverter . asTextDocumentIdentifier (
48
+ vscode . window . activeTextEditor . document
49
+ ) . uri ;
50
+ const params : ExecuteCommandParams = {
51
+ command : 'gopls.list_known_packages' ,
52
+ arguments : [
53
+ {
54
+ URI : uri
55
+ }
56
+ ]
57
+ } ;
58
+ const resp = await languageClient . sendRequest ( ExecuteCommandRequest . type , params ) ;
59
+ return resp . Packages ;
60
+ } catch ( e ) {
61
+ console . log ( `error with gopls.list_known_packages: ${ e } ` ) ;
62
+ }
63
+ }
64
+
65
+ // fallback to calling listPackages
66
+ return listPackages ( true ) ;
67
+ }
68
+
42
69
/**
43
70
* Returns the imported packages in the given file
44
71
*
@@ -64,7 +91,7 @@ async function getImports(document: vscode.TextDocument): Promise<string[]> {
64
91
65
92
async function askUserForImport ( ) : Promise < string | undefined > {
66
93
try {
67
- const packages = await listPackages ( true ) ;
94
+ const packages = await golist ( ) ;
68
95
return vscode . window . showQuickPick ( packages ) ;
69
96
} catch ( err ) {
70
97
if ( typeof err === 'string' && err . startsWith ( missingToolMsg ) ) {
@@ -123,17 +150,40 @@ export function getTextEditForAddImport(arg: string): vscode.TextEdit[] {
123
150
}
124
151
}
125
152
126
- export function addImport ( arg : { importPath : string ; from : string } ) {
153
+ export function addImport ( arg : { importPath : string } ) {
127
154
const editor = vscode . window . activeTextEditor ;
128
155
if ( ! editor ) {
129
156
vscode . window . showErrorMessage ( 'No active editor found to add imports.' ) ;
130
157
return ;
131
158
}
132
159
const p = arg && arg . importPath ? Promise . resolve ( arg . importPath ) : askUserForImport ( ) ;
133
- p . then ( ( imp ) => {
160
+ p . then ( async ( imp ) => {
134
161
if ( ! imp ) {
135
162
return ;
136
163
}
164
+
165
+ if ( languageClient ) {
166
+ try {
167
+ const uri = languageClient . code2ProtocolConverter . asTextDocumentIdentifier (
168
+ vscode . window . activeTextEditor . document
169
+ ) . uri ;
170
+ const params : ExecuteCommandParams = {
171
+ command : 'gopls.add_import' ,
172
+ arguments : [
173
+ {
174
+ ImportPath : imp ,
175
+ URI : uri
176
+ }
177
+ ]
178
+ } ;
179
+ await languageClient . sendRequest ( ExecuteCommandRequest . type , params ) ;
180
+ return ;
181
+ } catch ( e ) {
182
+ console . log ( `error executing gopls.add_import: ${ e } ` ) ;
183
+ }
184
+ }
185
+
186
+ // fallback to adding imports directly from client
137
187
const edits = getTextEditForAddImport ( imp ) ;
138
188
if ( edits && edits . length > 0 ) {
139
189
const edit = new vscode . WorkspaceEdit ( ) ;
0 commit comments