From 8b53ac34eb45ef829327fa6a00178adb3f376a99 Mon Sep 17 00:00:00 2001 From: Luis G Date: Mon, 28 May 2018 23:35:03 -0300 Subject: [PATCH 1/3] Avoid duplicate function parameter body on function suggest snippets Avoid adding snippet for function suggest when cursor is followed by (). Fixes #1655 --- src/goSuggest.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/goSuggest.ts b/src/goSuggest.ts index b8aa4f7c6..82a31c336 100644 --- a/src/goSuggest.ts +++ b/src/goSuggest.ts @@ -199,7 +199,17 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider { paramSnippets.push('${' + (i + 1) + ':' + param + '}'); } } - item.insertText = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')'); + let currentWordIndex = lineText.toLowerCase().lastIndexOf(currentWord.toLowerCase()); + let nextSymbolIndex = currentWordIndex + currentWord.length; + let newSnippetString = null; + // Avoid adding snippet for function suggest when cursor is followed by () + // i.e: met() -> method()() + if ((nextSymbolIndex < lineText.length) && (lineText.charAt(nextSymbolIndex) === '(')) { + newSnippetString = new vscode.SnippetString(suggest.name); + } else { + newSnippetString = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')'); + } + item.insertText = newSnippetString; } if (config['useCodeSnippetsOnFunctionSuggest'] && suggest.class === 'type' && suggest.type.startsWith('func(')) { let { params, returnType } = getParametersAndReturnType(suggest.type.substring(4)); From 4a97f725edc005d84eeee975c0d5b957403bb2e9 Mon Sep 17 00:00:00 2001 From: Luis G Date: Sun, 3 Jun 2018 01:11:27 -0300 Subject: [PATCH 2/3] Review feedback. Simplify logic --- src/goSuggest.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/goSuggest.ts b/src/goSuggest.ts index 82a31c336..085393ac3 100644 --- a/src/goSuggest.ts +++ b/src/goSuggest.ts @@ -199,12 +199,10 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider { paramSnippets.push('${' + (i + 1) + ':' + param + '}'); } } - let currentWordIndex = lineText.toLowerCase().lastIndexOf(currentWord.toLowerCase()); - let nextSymbolIndex = currentWordIndex + currentWord.length; let newSnippetString = null; // Avoid adding snippet for function suggest when cursor is followed by () // i.e: met() -> method()() - if ((nextSymbolIndex < lineText.length) && (lineText.charAt(nextSymbolIndex) === '(')) { + if (lineText.substr(position.character, 2) === '()') { newSnippetString = new vscode.SnippetString(suggest.name); } else { newSnippetString = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')'); From 6583eae9adf492754c1a605e8607604e27e18f64 Mon Sep 17 00:00:00 2001 From: Luis G Date: Sun, 3 Jun 2018 16:50:33 -0300 Subject: [PATCH 3/3] Review feedback. Simplify logic --- src/goSuggest.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/goSuggest.ts b/src/goSuggest.ts index 085393ac3..8a70a7d4f 100644 --- a/src/goSuggest.ts +++ b/src/goSuggest.ts @@ -199,15 +199,11 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider { paramSnippets.push('${' + (i + 1) + ':' + param + '}'); } } - let newSnippetString = null; // Avoid adding snippet for function suggest when cursor is followed by () // i.e: met() -> method()() - if (lineText.substr(position.character, 2) === '()') { - newSnippetString = new vscode.SnippetString(suggest.name); - } else { - newSnippetString = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')'); + if (lineText.substr(position.character, 2) !== '()') { + item.insertText = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')'); } - item.insertText = newSnippetString; } if (config['useCodeSnippetsOnFunctionSuggest'] && suggest.class === 'type' && suggest.type.startsWith('func(')) { let { params, returnType } = getParametersAndReturnType(suggest.type.substring(4));