Skip to content

Commit a1d1329

Browse files
marwan-at-workhyangah
authored andcommitted
src/goImport: use gopls methods to list and add contextual imports
This CL uses the recently merged https://go-review.googlesource.com/c/tools/+/281412 to list known packages relative to the open file as well as asking gopls to add the user selected import path. Fixes #43351 Change-Id: I8af24e61e651fd7150a7dd4a8a7a19bd52d1a9ab Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/322169 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]> Trust: Hyang-Ah Hana Kim <[email protected]> Trust: Rebecca Stambler <[email protected]>
1 parent 03a8315 commit a1d1329

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

src/goImport.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
import cp = require('child_process');
1111
import vscode = require('vscode');
12+
import { ExecuteCommandRequest, ExecuteCommandParams } from 'vscode-languageserver-protocol';
1213
import { toolExecutionEnvironment } from './goEnv';
1314
import { promptForMissingTool } from './goInstallTools';
15+
import { languageClient } from './goLanguageServer';
1416
import { documentSymbols, GoOutlineImportsOptions } from './goOutline';
1517
import { getImportablePackages } from './goPackages';
1618
import { getBinPath, getImportPath, parseFilePrelude } from './util';
@@ -39,6 +41,31 @@ export async function listPackages(excludeImportedPkgs = false): Promise<string[
3941
return [...stdLibs.sort(), ...nonStdLibs.sort()];
4042
}
4143

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+
4269
/**
4370
* Returns the imported packages in the given file
4471
*
@@ -64,7 +91,7 @@ async function getImports(document: vscode.TextDocument): Promise<string[]> {
6491

6592
async function askUserForImport(): Promise<string | undefined> {
6693
try {
67-
const packages = await listPackages(true);
94+
const packages = await golist();
6895
return vscode.window.showQuickPick(packages);
6996
} catch (err) {
7097
if (typeof err === 'string' && err.startsWith(missingToolMsg)) {
@@ -123,17 +150,40 @@ export function getTextEditForAddImport(arg: string): vscode.TextEdit[] {
123150
}
124151
}
125152

126-
export function addImport(arg: { importPath: string; from: string }) {
153+
export function addImport(arg: { importPath: string }) {
127154
const editor = vscode.window.activeTextEditor;
128155
if (!editor) {
129156
vscode.window.showErrorMessage('No active editor found to add imports.');
130157
return;
131158
}
132159
const p = arg && arg.importPath ? Promise.resolve(arg.importPath) : askUserForImport();
133-
p.then((imp) => {
160+
p.then(async (imp) => {
134161
if (!imp) {
135162
return;
136163
}
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
137187
const edits = getTextEditForAddImport(imp);
138188
if (edits && edits.length > 0) {
139189
const edit = new vscode.WorkspaceEdit();

0 commit comments

Comments
 (0)