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

Add keywords to completion list #865

Merged
merged 1 commit into from
Mar 15, 2017
Merged
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
13 changes: 11 additions & 2 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import vscode = require('vscode');
import cp = require('child_process');
import { dirname, basename } from 'path';
import { getBinPath, parameters, parseFilePrelude, isPositionInString } from './util';
import { getBinPath, parameters, parseFilePrelude, isPositionInString, goKeywords } from './util';
import { promptForMissingTool } from './goInstallTools';
import { listPackages, getTextEditForAddImport } from './goImport';

Expand Down Expand Up @@ -82,14 +82,23 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
let includeUnimportedPkgs = autocompleteUnimportedPackages && !inString;

return this.runGoCode(filename, inputText, offset, inString, position, lineText, currentWord, includeUnimportedPkgs).then(suggestions => {
// gocode does not suggest keywords, so we have to do it
if (currentWord.length > 0) {
goKeywords.forEach(keyword => {
if (keyword.startsWith(currentWord)) {
suggestions.push(new vscode.CompletionItem(keyword, vscode.CompletionItemKind.Keyword));
}
});
}

// If no suggestions and cursor is at a dot, then check if preceeding word is a package name
// If yes, then import the package in the inputText and run gocode again to get suggestions
if (suggestions.length === 0 && lineTillCurrentPosition.endsWith('.')) {

let pkgPath = this.getPackagePathFromLine(lineTillCurrentPosition);
if (pkgPath) {
// Now that we have the package path, import it right after the "package" statement
let {imports, pkg} = parseFilePrelude(vscode.window.activeTextEditor.document.getText());
let { imports, pkg } = parseFilePrelude(vscode.window.activeTextEditor.document.getText());
let posToAddImport = document.offsetAt(new vscode.Position(pkg.start + 1, 0));
let textToAdd = `import "${pkgPath}"\n`;
inputText = inputText.substr(0, posToAddImport) + textToAdd + inputText.substr(posToAddImport);
Expand Down