This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 645
Complete method receivers of types in namespace #168 #1368
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 => { | ||
|
@@ -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 | ||
|
@@ -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'); | ||
|
||
|
@@ -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; | ||
|
@@ -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\}'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since i.e There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 + ')
forlabel
andMethod snippet
fordetail
?There was a problem hiding this comment.
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 thelabel
. More specifically, when completing an unexported type the suggestion is not shown.For now I changed the
label
to be exactlysuggest.name
and kept your suggestion for thelabel
.Let me know what you think!
There was a problem hiding this comment.
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. IffilterText
is set, then that will be used for filtering insteadThere was a problem hiding this comment.
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.