Skip to content

Commit

Permalink
Complete method receivers of types in namespace microsoft#168
Browse files Browse the repository at this point in the history
  • Loading branch information
grooveygr committed Nov 26, 2017
1 parent 301d444 commit ceb69e8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/goSuggest.ts
Expand Up @@ -7,7 +7,7 @@

import vscode = require('vscode');
import cp = require('child_process');
import { getBinPath, parameters, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars, guessPackageNameFromFile } from './util';
import { getBinPath, parameters, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars, guessPackageNameFromFile, goBuiltinTypes } from './util';
import { promptForMissingTool } from './goInstallTools';
import { getTextEditForAddImport } from './goImport';
import { getImportablePackages } from './goPackages';
Expand Down Expand Up @@ -75,7 +75,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
let inputText = document.getText();
let includeUnimportedPkgs = autocompleteUnimportedPackages && !inString;

return this.runGoCode(filename, inputText, offset, inString, position, lineText, currentWord, includeUnimportedPkgs).then(suggestions => {
return this.runGoCode(document, 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 => {
Expand All @@ -99,7 +99,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
offset += textToAdd.length;

// Now that we have the package imported in the inputText, run gocode again
return this.runGoCode(filename, inputText, offset, inString, position, lineText, currentWord, false).then(newsuggestions => {
return this.runGoCode(document, filename, inputText, offset, inString, position, lineText, currentWord, false).then(newsuggestions => {
// Since the new suggestions are due to the package that we imported,
// add additionalTextEdits to do the same in the actual document in the editor
// We use additionalTextEdits instead of command so that 'useCodeSnippetsOnFunctionSuggest' feature continues to work
Expand All @@ -116,7 +116,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
});
}

private runGoCode(filename: string, inputText: string, offset: number, inString: boolean, position: vscode.Position, lineText: string, currentWord: string, includeUnimportedPkgs: boolean): Thenable<vscode.CompletionItem[]> {
private runGoCode(document: vscode.TextDocument, filename: string, inputText: string, offset: number, inString: boolean, position: vscode.Position, lineText: string, currentWord: string, includeUnimportedPkgs: boolean): Thenable<vscode.CompletionItem[]> {
return new Promise<vscode.CompletionItem[]>((resolve, reject) => {
let gocode = getBinPath('gocode');

Expand Down Expand Up @@ -147,6 +147,8 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
let suggestions = [];
let suggestionSet = new Set<string>();

let wordAtPosition = document.getWordRangeAtPosition(position);

if (results[1]) {
for (let suggest of results[1]) {
if (inString && suggest.class !== 'import') continue;
Expand Down Expand Up @@ -183,6 +185,18 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
item.insertText = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')');
}

if (wordAtPosition && wordAtPosition.start.character == 0 &&
suggest.class == 'type' && !goBuiltinTypes.has(suggest.name)) {
let prefix = 'func (' + suggest.name[0].toLowerCase() + ' *' + suggest.name + ')';
let auxItem = new vscode.CompletionItem(suggest.name + " method", vscode.CompletionItemKind.Snippet);
auxItem.label = suggest.name + " (new method)";
auxItem.detail = prefix + "...";
auxItem.sortText = 'a';
let snippet = prefix + ' ${1:name}(${2:params}) ${3:retval} \{\n\t$0\n\}';
auxItem.insertText = new vscode.SnippetString(snippet);
suggestions.push(auxItem);
}

// Add same sortText to all suggestions from gocode so that they appear before the unimported packages
item.sortText = 'a';
suggestions.push(item);
Expand Down
23 changes: 23 additions & 0 deletions src/util.ts
Expand Up @@ -45,6 +45,29 @@ export const goKeywords: string[] = [
'var'
];

export const goBuiltinTypes: Set<string> = new Set<string>([
'bool',
'byte',
'complex128',
'complex64',
'error',
'float32',
'float64',
'int',
'int16',
'int32',
'int64',
'int8',
'rune',
'string',
'uint',
'uint16',
'uint32',
'uint64',
'uint8',
'uintptr'
]);

export interface SemVersion {
major: number;
minor: number;
Expand Down

0 comments on commit ceb69e8

Please sign in to comment.