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

Commit

Permalink
Add setting autocomplteUnimportedPackages, dont show imported pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Oct 6, 2016
1 parent 7b7ac12 commit e9d86d6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@
"type": "object",
"default": {},
"description": "Environment variables that will passed to the process that runs the Go tests"
},
"go.autocomplteUnimportedPackages": {
"type": "boolean",
"default": false,
"description": "Autocomplete members from unimported packages."
}
}
}
Expand Down
25 changes: 17 additions & 8 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
private pkgsList: PackageInfo[] = [];

public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> {
return this.provideCompletionItemsInternal(document, position, token, vscode.workspace.getConfiguration('go'));
}

public provideCompletionItemsInternal(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, config: vscode.WorkspaceConfiguration): Thenable<vscode.CompletionItem[]> {
return this.ensureGoCodeConfigured().then(() => {
return new Promise<vscode.CompletionItem[]>((resolve, reject) => {
let filename = document.fileName;
let lineText = document.lineAt(position.line).text;
let lineTillCurrentPosition = lineText.substr(0, position.character);
let autocompleteUnimportedPackages = config['autocomplteUnimportedPackages'] === true;

if (lineText.match(/^\s*\/\//)) {
return resolve([]);
Expand All @@ -77,6 +82,10 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
let inputText = document.getText();

return this.runGoCode(filename, inputText, offset, inString, position, lineText).then(suggestions => {
if (!autocompleteUnimportedPackages) {
return resolve(suggestions);
}

// Add importable packages matching currentword to suggestions
suggestions = suggestions.concat(this.getMatchingPackages(currentWord));

Expand Down Expand Up @@ -184,15 +193,15 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
}
// TODO: Shouldn't lib-path also be set?
private ensureGoCodeConfigured(): Thenable<void> {
let pkgPromise = listPackages().then((pkgs: string[]) => {
this.pkgsList = pkgs.map(pkg => {
let index = pkg.lastIndexOf('/');
return {
name: index === -1 ? pkg : pkg.substr(index + 1),
path: pkg
};
let pkgPromise = listPackages(true).then((pkgs: string[]) => {
this.pkgsList = pkgs.map(pkg => {
let index = pkg.lastIndexOf('/');
return {
name: index === -1 ? pkg : pkg.substr(index + 1),
path: pkg
};
});
});
});
let configPromise = new Promise<void>((resolve, reject) => {
// TODO: Since the gocode daemon is shared amongst clients, shouldn't settings be
// adjusted per-invocation to avoid conflicts from other gocode-using programs?
Expand Down
5 changes: 4 additions & 1 deletion test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ encountered.
});

test('Test Completion on unimported packages', (done) => {
let config = Object.create(vscode.workspace.getConfiguration('go'), {
'autocomplteUnimportedPackages': { value: true }
});
let provider = new GoCompletionItemProvider();
let testCases: [vscode.Position, string[]][] = [
[new vscode.Position(12, 2), ['bytes']],
Expand All @@ -113,7 +116,7 @@ encountered.
editbuilder.insert(new vscode.Position(13, 0), 'math.\n');
}).then(() => {
let promises = testCases.map(([position, expected]) =>
provider.provideCompletionItems(textDocument, position, null).then(items => {
provider.provideCompletionItemsInternal(textDocument, 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 Down

0 comments on commit e9d86d6

Please sign in to comment.