Skip to content

Commit bc7d469

Browse files
committed
use gopls PackageImports instead of manually using fs
1 parent acf6939 commit bc7d469

File tree

3 files changed

+8
-36
lines changed

3 files changed

+8
-36
lines changed

src/goDocumentSymbols.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,5 @@ async function listImports(
102102
]
103103
};
104104
const resp = await languageClient?.sendRequest(ExecuteCommandRequest.type, params);
105-
return resp.Imports;
105+
return resp.PackageImports;
106106
}

src/goTest/resolve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { getCurrentGoPath } from '../util';
2424
import { getGoConfig } from '../config';
2525
import { dispose, disposeIfEmpty, FileSystem, GoTest, GoTestKind, findModuleName, isInTest, Workspace } from './utils';
2626
import { walk, WalkStop } from './walk';
27-
import { isPackageTestify } from '../testUtils';
27+
import { importsTestify } from '../testUtils';
2828

2929
export type ProvideSymbols = (doc: TextDocument, token?: CancellationToken) => Thenable<DocumentSymbol[]>;
3030

@@ -205,7 +205,7 @@ export class GoTestResolver {
205205
const seen = new Set<string>();
206206
const item = await this.getFile(doc.uri);
207207
const symbols = await this.provideDocumentSymbols(doc);
208-
const testify = await isPackageTestify(this.provideDocumentSymbols, doc);
208+
const testify = importsTestify(symbols);
209209
for (const symbol of symbols) {
210210
await this.processSymbol(doc, item, seen, testify, symbol);
211211
}

src/testUtils.ts

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import cp = require('child_process');
1111
import path = require('path');
1212
import util = require('util');
13-
import fs = require('fs');
1413
import vscode = require('vscode');
1514

1615
import { applyCodeCoverageToAllEditors } from './goCover';
@@ -150,8 +149,6 @@ export async function getTestFunctions(
150149
token?: vscode.CancellationToken
151150
): Promise<vscode.DocumentSymbol[] | undefined> {
152151
const documentSymbolProvider = GoDocumentSymbolProvider(goCtx, true);
153-
154-
const testify = await isPackageTestify(documentSymbolProvider.provideDocumentSymbols, doc, token);
155152
const symbols = await documentSymbolProvider.provideDocumentSymbols(doc, token);
156153
if (!symbols || symbols.length === 0) {
157154
return;
@@ -161,6 +158,10 @@ export async function getTestFunctions(
161158
return;
162159
}
163160
const children = symbol.children;
161+
162+
// symbols got the imports of all the package.
163+
const testify = importsTestify(symbols);
164+
164165
return children.filter(
165166
(sym) =>
166167
(sym.kind === vscode.SymbolKind.Function || sym.kind === vscode.SymbolKind.Method) &&
@@ -588,35 +589,6 @@ export function cancelRunningTests(): Thenable<boolean> {
588589
});
589590
}
590591

591-
export type ProvideSymbols = (
592-
doc: vscode.TextDocument,
593-
token?: vscode.CancellationToken
594-
) => Thenable<vscode.DocumentSymbol[]>;
595-
596-
/**
597-
* Returns whether a file in the package imports testify.
598-
*/
599-
export async function isPackageTestify(
600-
symbolsProvier: ProvideSymbols,
601-
doc: vscode.TextDocument,
602-
token?: vscode.CancellationToken | undefined
603-
) {
604-
const fsReaddir = util.promisify(fs.readdir);
605-
606-
// Get all the package documents.
607-
const packageDir = path.parse(doc.fileName).dir;
608-
const packageFilenames = await fsReaddir(packageDir);
609-
const packageDocs = await Promise.all(
610-
packageFilenames.map((e) => path.join(packageDir, e)).map(vscode.workspace.openTextDocument)
611-
);
612-
613-
// Parse the symbols of each document.
614-
const docSymbols = await Promise.all(packageDocs.map((e) => symbolsProvier(e, token)));
615-
616-
// One file is enough.
617-
return docSymbols.some(importsTestify);
618-
}
619-
620592
/**
621593
* Get the test target arguments.
622594
*
@@ -671,7 +643,7 @@ function removeRunFlag(flags: string[]): void {
671643
}
672644
}
673645

674-
function importsTestify(syms: vscode.DocumentSymbol[]): boolean {
646+
export function importsTestify(syms: vscode.DocumentSymbol[]): boolean {
675647
if (!syms || syms.length === 0 || !syms[0]) {
676648
return false;
677649
}

0 commit comments

Comments
 (0)