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

Move to DocumentSymbol #1795

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
"request": "launch",
// path to VSCode executable
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "npm"
},
{
Expand All @@ -19,9 +23,13 @@
"protocol": "inspector",
"request": "launch",
"program": "${workspaceFolder}/out/src/debugAdapter/goDebug.js",
"args": ["--server=4711"],
"args": [
"--server=4711"
],
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "npm"
},
{
Expand All @@ -32,11 +40,15 @@
// the workspace path should be GOPATH
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test"
"--extensionTestsPath=${workspaceFolder}/out/test",
"--timeout",
"999999",
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "npm"
},
{
Expand All @@ -58,7 +70,10 @@
"compounds": [
{
"name": "Extension + Debug server",
"configurations": ["Launch Extension", "Launch as server"]
"configurations": [
"Launch Extension",
"Launch as server"
]
}
]
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"vscode": "^1.1.26"
},
"engines": {
"vscode": "^1.23.0"
"vscode": "^1.25.0"
},
"activationEvents": [
"onLanguage:go",
Expand Down
57 changes: 23 additions & 34 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function toggleTestFile(): void {
vscode.commands.executeCommand('vscode.open', vscode.Uri.file(targetFilePath));
}

export function generateTestCurrentPackage(): Thenable<boolean> {
export function generateTestCurrentPackage(): Promise<boolean> {
let editor = checkActiveEditor();
if (!editor) {
return;
Expand All @@ -74,7 +74,7 @@ export function generateTestCurrentPackage(): Thenable<boolean> {
vscode.workspace.getConfiguration('go', editor.document.uri));
}

export function generateTestCurrentFile(): Thenable<boolean> {
export function generateTestCurrentFile(): Promise<boolean> {
let editor = checkActiveEditor();
if (!editor) {
return;
Expand All @@ -83,32 +83,25 @@ export function generateTestCurrentFile(): Thenable<boolean> {
vscode.workspace.getConfiguration('go', editor.document.uri));
}

export function generateTestCurrentFunction(): Thenable<boolean> {
let editor = checkActiveEditor();
export async function generateTestCurrentFunction(): Promise<boolean> {
const editor = checkActiveEditor();
if (!editor) {
return;
}

return getFunctions(editor.document).then(functions => {
let currentFunction: vscode.SymbolInformation;
for (let func of functions) {
let selection = editor.selection;
if (selection && func.location.range.contains(selection.start)) {
currentFunction = func;
break;
}
}
if (!currentFunction) {
vscode.window.showInformationMessage('No function found at cursor.');
return Promise.resolve(false);
}
let funcName = currentFunction.name;
if (funcName.includes('.')) {
funcName = funcName.split('.')[1];
}
return generateTests({ dir: editor.document.uri.fsPath, func: funcName },
vscode.workspace.getConfiguration('go', editor.document.uri));
});
const functions = await getFunctions(editor.document);
const selection = editor.selection;
const currentFunction: vscode.DocumentSymbol = functions.find(func => selection && func.range.contains(selection.start));

if (!currentFunction) {
vscode.window.showInformationMessage('No function found at cursor.');
return Promise.resolve(false);
}
let funcName = currentFunction.name;
if (funcName.includes('.')) {
funcName = funcName.split('.')[1];
}
return generateTests({ dir: editor.document.uri.fsPath, func: funcName }, vscode.workspace.getConfiguration('go', editor.document.uri));
}

/**
Expand All @@ -120,12 +113,12 @@ interface Config {
*/
dir: string;
/**
* Specific function names to generate tests squeleton.
* Specific function names to generate tests skeleton.
*/
func?: string;
}

function generateTests(conf: Config, goConfig: vscode.WorkspaceConfiguration): Thenable<boolean> {
function generateTests(conf: Config, goConfig: vscode.WorkspaceConfiguration): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
let cmd = getBinPath('gotests');
let args = ['-w'];
Expand Down Expand Up @@ -193,12 +186,8 @@ function generateTests(conf: Config, goConfig: vscode.WorkspaceConfiguration): T
});
}

function getFunctions(doc: vscode.TextDocument): Thenable<vscode.SymbolInformation[]> {
let documentSymbolProvider = new GoDocumentSymbolProvider();
return documentSymbolProvider
.provideDocumentSymbols(doc, null)
.then(symbols =>
symbols.filter(sym =>
sym.kind === vscode.SymbolKind.Function)
);
async function getFunctions(doc: vscode.TextDocument): Promise<vscode.DocumentSymbol[]> {
const documentSymbolProvider = new GoDocumentSymbolProvider();
const symbols = await documentSymbolProvider.provideDocumentSymbols(doc, null);
return symbols[0].children.filter(sym => sym.kind === vscode.SymbolKind.Function);
}
56 changes: 29 additions & 27 deletions src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import { getImportablePackages } from './goPackages';

const missingToolMsg = 'Missing tool: ';

export function listPackages(excludeImportedPkgs: boolean = false): Thenable<string[]> {
let importsPromise = excludeImportedPkgs && vscode.window.activeTextEditor ? getImports(vscode.window.activeTextEditor.document) : Promise.resolve([]);
let pkgsPromise = getImportablePackages(vscode.window.activeTextEditor.document.fileName, true);
return Promise.all([pkgsPromise, importsPromise]).then(([pkgMap, importedPkgs]) => {
importedPkgs.forEach(pkg => {
pkgMap.delete(pkg);
});
return Array.from(pkgMap.keys()).sort();
});
export async function listPackages(excludeImportedPkgs: boolean = false): Promise<string[]> {
const importedPkgs = excludeImportedPkgs && vscode.window.activeTextEditor
? await getImports(vscode.window.activeTextEditor.document)
: [];
const pkgMap = await getImportablePackages(vscode.window.activeTextEditor.document.fileName, true);

return Array.from(pkgMap.keys())
.filter(pkg => !importedPkgs.some(imported => imported === pkg))
.sort();
}

/**
Expand All @@ -31,26 +31,28 @@ export function listPackages(excludeImportedPkgs: boolean = false): Thenable<str
* @param document TextDocument whose imports need to be returned
* @returns Array of imported package paths wrapped in a promise
*/
function getImports(document: vscode.TextDocument): Promise<string[]> {
let options = { fileName: document.fileName, importsOption: GoOutlineImportsOptions.Only, document, skipRanges: true };
return documentSymbols(options, null).then(symbols => {
if (!symbols || !symbols.length) {
return [];
}
// import names will be of the form "math", so strip the quotes in the beginning and the end
let imports = symbols.filter(x => x.kind === vscode.SymbolKind.Namespace).map(x => x.name.substr(1, x.name.length - 2));
return imports;
});
async function getImports(document: vscode.TextDocument): Promise<string[]> {
const options = { fileName: document.fileName, importsOption: GoOutlineImportsOptions.Only, document };
const symbols = await documentSymbols(options, null);
if (!symbols || !symbols.length) {
return [];
}
// import names will be of the form "math", so strip the quotes in the beginning and the end
const imports = symbols[0].children
.filter((x: any) => x.kind === vscode.SymbolKind.Namespace)
.map((x: any) => x.name.substr(1, x.name.length - 2));
return imports;
}

function askUserForImport(): Thenable<string> {
return listPackages(true).then(packages => {
async function askUserForImport(): Promise<string> {
try {
const packages = await listPackages(true);
return vscode.window.showQuickPick(packages);
}, err => {
} catch (err) {
if (typeof err === 'string' && err.startsWith(missingToolMsg)) {
promptForMissingTool(err.substr(missingToolMsg.length));
}
});
}
}

export function getTextEditForAddImport(arg: string): vscode.TextEdit[] {
Expand Down Expand Up @@ -98,9 +100,9 @@ export function getTextEditForAddImport(arg: string): vscode.TextEdit[] {
}

export function addImport(arg: string) {
let p = arg ? Promise.resolve(arg) : askUserForImport();
const p = arg ? Promise.resolve(arg) : askUserForImport();
p.then(imp => {
let edits = getTextEditForAddImport(imp);
const edits = getTextEditForAddImport(imp);
if (edits && edits.length > 0) {
const edit = new vscode.WorkspaceEdit();
edit.set(vscode.window.activeTextEditor.document.uri, edits);
Expand Down Expand Up @@ -132,7 +134,7 @@ export function addImportToWorkspace() {

if (importPath === '') {
// Failing that use the current line
let selectedText = editor.document.lineAt(selection.active.line).text;
const selectedText = editor.document.lineAt(selection.active.line).text;
importPath = getImportPath(selectedText);
}

Expand All @@ -145,7 +147,7 @@ export function addImportToWorkspace() {
const env = getToolsEnvVars();

cp.execFile(goRuntimePath, ['list', '-f', '{{.Dir}}', importPath], { env }, (err, stdout, stderr) => {
let dirs = (stdout || '').split('\n');
const dirs = (stdout || '').split('\n');
if (!dirs.length || !dirs[0].trim()) {
vscode.window.showErrorMessage(`Could not find package ${importPath}`);
return;
Expand Down
Loading