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 1 commit
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
22 changes: 18 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,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)';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of 'func (*' + suggest.name + ') for label and Method snippet for detail?

Copy link
Contributor Author

@grooveygr grooveygr Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I experimented with your idea and found that the vscode engine does not show suggestions in some cases where the suggest.name is not a prefix of the label. More specifically, when completing an unexported type the suggestion is not shown.
For now I changed the label to be exactly suggest.name and kept your suggestion for the label.
Let me know what you think!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get around that by adding auxItem.filterText = suggest.name

By default the label is used for displaying the item as well as filtering the item based on what is typed. If filterText is set, then that will be used for filtering instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Learned something new.
Done.

auxItem.detail = prefix + '...';
auxItem.sortText = 'a';
let snippet = prefix + ' ${1:name}(${2:params}) ${3:retval} \{\n\t$0\n\}';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since params and retval can be empty, why not have them as simple tabstops instead of placeholders?

i.e ${1:methodName}(${2}) ${3} instead of ${1:name}(${2:params}) ${3:retvall}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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