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

Commit

Permalink
Use -imports-only option from go-outline (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a authored Oct 26, 2016
1 parent 5d27d80 commit 708b2ad
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ export function listPackages(excludeImportedPkgs: boolean = false): Thenable<str
* @param fileName File system path of the file whose imports need to be returned
* @returns Array of imported package paths wrapped in a promise
*/
export function getImports(fileName: string): Promise<string[]> {
return documentSymbols(fileName).then(symbols => {
function getImports(fileName: string): Promise<string[]> {
let options = { fileName: fileName, importsOnly: true };
return documentSymbols(options).then(symbols => {
if (!symbols || !symbols[0] || !symbols[0].children) {
return [];
}
Expand Down
14 changes: 14 additions & 0 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface SemVersion {

let goVersion: SemVersion = null;
let vendorSupport: boolean = null;
let updatesDeclinedTools: string[] = [];

function getTools(): { [key: string]: string } {
let goConfig = vscode.workspace.getConfiguration('go');
Expand Down Expand Up @@ -76,7 +77,20 @@ export function promptForMissingTool(tool: string) {
}
});
});
}

export function promptForUpdatingTool(tool: string) {
// If user has declined to update, then don't prompt
if (updatesDeclinedTools.indexOf(tool) > -1) {
return;
}
vscode.window.showInformationMessage(`The Go extension is better with the latest version of "${tool}". Use "go get -u -v ${getTools()[tool]}" to update`, 'Update').then(selected => {
if (selected === 'Update') {
installTools([tool]);
} else {
updatesDeclinedTools.push(tool);
}
});
}

/**
Expand Down
26 changes: 21 additions & 5 deletions src/goOutline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');
import { getBinPath } from './goPath';
import { promptForMissingTool } from './goInstallTools';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';

// Keep in sync with https://github.com/lukehoban/go-outline
interface GoOutlineRange {
Expand All @@ -29,15 +29,31 @@ interface GoOutlineDeclaration {
comment?: GoOutlineRange;
}

export function documentSymbols(filename: string): Promise<GoOutlineDeclaration[]> {
interface GoOutlineOptions {
fileName: string;
importsOnly?: boolean;
}

export function documentSymbols(options: GoOutlineOptions): Promise<GoOutlineDeclaration[]> {
return new Promise<GoOutlineDeclaration[]>((resolve, reject) => {
let gooutline = getBinPath('go-outline');
let gooutlineFlags = ['-f', options.fileName];
if (options.importsOnly) {
gooutlineFlags.push('-imports-only');
}
// Spawn `go-outline` process
let p = cp.execFile(gooutline, ['-f', filename], {}, (err, stdout, stderr) => {
let p = cp.execFile(gooutline, gooutlineFlags, {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('go-outline');
}
if (stderr && stderr.startsWith('flag provided but not defined: -imports-only')) {
promptForUpdatingTool('go-outline');
options.importsOnly = false;
return documentSymbols(options).then(results => {
return resolve(results);
});
}
if (err) return resolve(null);
let result = stdout.toString();
let decls = <GoOutlineDeclaration[]>JSON.parse(result);
Expand Down Expand Up @@ -79,8 +95,8 @@ export class GoDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
}

public provideDocumentSymbols(document: vscode.TextDocument, token: vscode.CancellationToken): Thenable<vscode.SymbolInformation[]> {

return documentSymbols(document.fileName).then(decls => {
let options = { fileName: document.fileName };
return documentSymbols(options).then(decls => {
let symbols: vscode.SymbolInformation[] = [];
this.convertToCodeSymbols(document, decls, symbols, '');
return symbols;
Expand Down
44 changes: 35 additions & 9 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ encountered.
vscode.workspace.openTextDocument(uri).then((textDocument) => {
return vscode.window.showTextDocument(textDocument).then(editor => {
let promises = testCases.map(([position, expected]) =>
provider.provideCompletionItems(textDocument, position, null).then(items => {
provider.provideCompletionItems(editor.document, position, null).then(items => {
let labels = items.map(x => x.label);
for (let entry of expected) {
if (labels.indexOf(entry) < 0) {
Expand All @@ -97,6 +97,9 @@ encountered.
})
);
return Promise.all(promises);
}).then(() => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
return Promise.resolve();
});
}, (err) => {
assert.ok(false, `error in OpenTextDocument ${err}`);
Expand All @@ -115,13 +118,13 @@ encountered.
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));

vscode.workspace.openTextDocument(uri).then((textDocument) => {
return vscode.window.showTextDocument(textDocument).then((editor => {
return vscode.window.showTextDocument(textDocument).then(editor => {
return editor.edit(editbuilder => {
editbuilder.insert(new vscode.Position(12, 0), 'by\n');
editbuilder.insert(new vscode.Position(13, 0), 'math.\n');
}).then(() => {
let promises = testCases.map(([position, expected]) =>
provider.provideCompletionItemsInternal(textDocument, position, null, config).then(items => {
provider.provideCompletionItemsInternal(editor.document, position, null, config).then(items => {
let labels = items.map(x => x.label);
for (let entry of expected) {
assert.equal(labels.indexOf(entry) > -1, true, `missing expected item in competion list: ${entry} Actual: ${labels}`);
Expand All @@ -130,7 +133,7 @@ encountered.
);
return Promise.all(promises);
});
})).then(() => {
}).then(() => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
return Promise.resolve();
});
Expand Down Expand Up @@ -181,9 +184,7 @@ encountered.
}
assert.equal(sortedDiagnostics.length, expected.length, `too many errors ${JSON.stringify(sortedDiagnostics)}`);
});

}).then(() => done(), done);

});

test('Test Generate unit tests squeleton for file', (done) => {
Expand All @@ -194,13 +195,16 @@ encountered.
}

let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
vscode.workspace.openTextDocument(uri).then(document => {
return vscode.workspace.openTextDocument(uri).then(document => {
return vscode.window.showTextDocument(document).then(editor => {
return generateTestCurrentFile().then((result: boolean) => {
assert.equal(result, true);
return Promise.resolve();
});
});
}).then(() => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
return Promise.resolve();
});
}).then(() => done(), done);
});
Expand All @@ -213,13 +217,16 @@ encountered.
}

let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
vscode.workspace.openTextDocument(uri).then(document => {
return vscode.workspace.openTextDocument(uri).then(document => {
return vscode.window.showTextDocument(document).then(editor => {
return generateTestCurrentPackage().then((result: boolean) => {
assert.equal(result, true);
return Promise.resolve();
});
});
}).then(() => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
return Promise.resolve();
});
}).then(() => done(), done);
});
Expand Down Expand Up @@ -370,7 +377,8 @@ encountered.

test('Test Outline', (done) => {
let filePath = path.join(fixturePath, 'test.go');
documentSymbols(filePath).then(outlines => {
let options = { fileName: filePath };
documentSymbols(options).then(outlines => {
let packageOutline = outlines[0];
let symbols = packageOutline.children;
let imports = symbols.filter(x => x.type === 'import');
Expand All @@ -385,6 +393,24 @@ encountered.
}, done);
});

test('Test Outline imports only', (done) => {
let filePath = path.join(fixturePath, 'test.go');
let options = { fileName: filePath, importsOnly: true };
documentSymbols(options).then(outlines => {
let packageOutline = outlines[0];
let symbols = packageOutline.children;
let imports = symbols.filter(x => x.type === 'import');
let functions = symbols.filter(x => x.type === 'function');

assert.equal(packageOutline.type, 'package');
assert.equal(packageOutline.label, 'main');
assert.equal(imports[0].label, '"fmt"');
assert.equal(functions.length, 0);
assert.equal(imports.length, 1);
done();
}, done);
});

test('Test listPackages', (done) => {
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
vscode.workspace.openTextDocument(uri).then(document => {
Expand Down

0 comments on commit 708b2ad

Please sign in to comment.