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

Complete method receivers of types in namespace #168 #1368

Merged
merged 3 commits into from
Dec 16, 2017
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
23 changes: 19 additions & 4 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
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,19 @@ 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 auxItem = new vscode.CompletionItem(suggest.name + ' method', vscode.CompletionItemKind.Snippet);
auxItem.label = 'func (*' + suggest.name + ')';
auxItem.filterText = suggest.name;
auxItem.detail = 'Method snippet';
auxItem.sortText = 'a';
let prefix = 'func (' + suggest.name[0].toLowerCase() + ' *' + suggest.name + ')';
let snippet = prefix + ' ${1:methodName}(${2}) ${3} \{\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
Original file line number Diff line number Diff line change
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