Skip to content

Commit 251be44

Browse files
committed
Complete interface and struct keywords in function parameters
Fixes #1798
1 parent 0a24603 commit 251be44

File tree

7 files changed

+42
-15
lines changed

7 files changed

+42
-15
lines changed

src/com/goide/completion/BracesInsertHandler.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 Sergey Ignatov, Alexander Zolotov
2+
* Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Mihai Toader, Florin Patan
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,17 +32,14 @@
3232

3333
public class BracesInsertHandler implements InsertHandler<LookupElement> {
3434
public static final BracesInsertHandler ONE_LINER = new BracesInsertHandler(true);
35+
public static final BracesInsertHandler INSTANCE = new BracesInsertHandler(false);
3536

3637
private final boolean myOneLine;
3738

3839
public BracesInsertHandler(boolean oneLine) {
3940
myOneLine = oneLine;
4041
}
4142

42-
public BracesInsertHandler() {
43-
this(false);
44-
}
45-
4643
@Override
4744
public void handleInsert(InsertionContext context, LookupElement item) {
4845
final Editor editor = context.getEditor();

src/com/goide/completion/GoKeywordCompletionContributor.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import static com.intellij.patterns.StandardPatterns.*;
3939

4040
public class GoKeywordCompletionContributor extends CompletionContributor implements DumbAware {
41-
private static final BracesInsertHandler ADD_BRACES_INSERT_HANDLER = new BracesInsertHandler();
4241
private static final InsertHandler<LookupElement> ADD_BRACKETS_INSERT_HANDLER = new AddBracketsInsertHandler();
4342

4443
public GoKeywordCompletionContributor() {
@@ -53,9 +52,11 @@ public GoKeywordCompletionContributor() {
5352
extend(CompletionType.BASIC, insideBlockPattern(GoTypes.IDENTIFIER),
5453
new GoKeywordCompletionProvider(GoCompletionUtil.KEYWORD_PRIORITY, EMPTY_INSERT_HANDLER, "fallthrough"));
5554
extend(CompletionType.BASIC, insideBlockPattern(GoTypes.IDENTIFIER),
56-
new GoKeywordCompletionProvider(GoCompletionUtil.KEYWORD_PRIORITY, ADD_BRACES_INSERT_HANDLER, "select"));
55+
new GoKeywordCompletionProvider(GoCompletionUtil.KEYWORD_PRIORITY, BracesInsertHandler.INSTANCE, "select"));
5756
extend(CompletionType.BASIC, typeDeclaration(),
58-
new GoKeywordCompletionProvider(GoCompletionUtil.KEYWORD_PRIORITY, ADD_BRACES_INSERT_HANDLER, "interface", "struct"));
57+
new GoKeywordCompletionProvider(GoCompletionUtil.KEYWORD_PRIORITY, BracesInsertHandler.INSTANCE, "interface", "struct"));
58+
extend(CompletionType.BASIC, typeExpression(),
59+
new GoKeywordCompletionProvider(GoCompletionUtil.KEYWORD_PRIORITY, BracesInsertHandler.ONE_LINER, "interface", "struct"));
5960
extend(CompletionType.BASIC, insideForStatement(GoTypes.IDENTIFIER),
6061
new GoKeywordCompletionProvider(GoCompletionUtil.CONTEXT_KEYWORD_PRIORITY, EMPTY_INSERT_HANDLER, "break", "continue"));
6162
extend(CompletionType.BASIC, typeExpression(), new GoKeywordCompletionProvider(GoCompletionUtil.CONTEXT_KEYWORD_PRIORITY, "chan"));
@@ -65,19 +66,20 @@ public GoKeywordCompletionContributor() {
6566
extend(CompletionType.BASIC, referenceExpression(), new GoKeywordCompletionProvider(GoCompletionUtil.CONTEXT_KEYWORD_PRIORITY,
6667
ADD_BRACKETS_INSERT_HANDLER, "map"));
6768
extend(CompletionType.BASIC, referenceExpression(), new GoKeywordCompletionProvider(GoCompletionUtil.CONTEXT_KEYWORD_PRIORITY,
68-
ADD_BRACES_INSERT_HANDLER, "struct"));
69+
BracesInsertHandler.ONE_LINER, "struct"));
6970

70-
extend(CompletionType.BASIC, afterIfBlock(GoTypes.IDENTIFIER),
71+
extend(CompletionType.BASIC, afterIfBlock(GoTypes.IDENTIFIER),
7172
new GoKeywordCompletionProvider(GoCompletionUtil.CONTEXT_KEYWORD_PRIORITY, "else"));
7273
extend(CompletionType.BASIC, afterElseKeyword(), new GoKeywordCompletionProvider(GoCompletionUtil.CONTEXT_KEYWORD_PRIORITY, "if"));
73-
extend(CompletionType.BASIC, insideSwitchStatement(), new GoKeywordCompletionProvider(GoCompletionUtil.CONTEXT_KEYWORD_PRIORITY, "case", "default"));
74+
extend(CompletionType.BASIC, insideSwitchStatement(),
75+
new GoKeywordCompletionProvider(GoCompletionUtil.CONTEXT_KEYWORD_PRIORITY, "case", "default"));
7476
// todo: "range"
7577
}
7678

7779
@Override
7880
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
7981
super.fillCompletionVariants(parameters, result);
80-
if (insideGoOrDeferStatements(GoTypes.IDENTIFIER).accepts(parameters.getPosition())) {
82+
if (insideGoOrDeferStatements().accepts(parameters.getPosition())) {
8183
InsertHandler<LookupElement> insertHandler = GoKeywordCompletionProvider.createTemplateBasedInsertHandler("go_lang_anonymous_func");
8284
result.addElement(
8385
GoKeywordCompletionProvider.createKeywordLookupElement("func", GoCompletionUtil.CONTEXT_KEYWORD_PRIORITY, insertHandler));
@@ -119,8 +121,8 @@ private static ElementPattern<? extends PsiElement> typeDeclaration() {
119121
.withParent(psiElement(GoTypeReferenceExpression.class).withParent(psiElement(GoType.class).withParent(GoSpecType.class)));
120122
}
121123

122-
private static PsiElementPattern.Capture<PsiElement> insideGoOrDeferStatements(@NotNull IElementType tokenType) {
123-
return psiElement(tokenType)
124+
private static PsiElementPattern.Capture<PsiElement> insideGoOrDeferStatements() {
125+
return psiElement(GoTypes.IDENTIFIER)
124126
.withParent(psiElement(GoExpression.class).withParent(or(psiElement(GoDeferStatement.class), psiElement(GoGoStatement.class))));
125127
}
126128

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package foo
2+
3+
func function(interf<caret>) {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package foo
2+
3+
func function(interface{<caret>}) {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package foo
2+
3+
func function(struc<caret>) {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package foo
2+
3+
func function(struct {<caret>}) {
4+
}

tests/com/goide/completion/GoCompletionTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void testExpressionKeywords() {
191191
}
192192

193193
public void testTypeKeywordsInsideParentheses() {
194-
myFixture.testCompletionVariants(getTestName(true) + ".go", "chan", "map");
194+
myFixture.testCompletionVariants(getTestName(true) + ".go", "chan", "map", "interface", "struct");
195195
}
196196

197197
public void testSelectKeywordInsertHandler() {
@@ -205,6 +205,14 @@ public void testTypeKeywordInsertBraces() {
205205
public void testTypeKeywordDoNotInsertBraces() {
206206
doTestCompletion();
207207
}
208+
209+
public void testInterfaceKeywordAsFunctionParameter() {
210+
doTestCompletion();
211+
}
212+
213+
public void testStructKeywordAsFunctionParameter() {
214+
doTestCompletion();
215+
}
208216

209217
public void testForStatementKeywords() {
210218
myFixture.testCompletionVariants(getTestName(true) + ".go", "bar", "break", "const", "continue", "defer", "for", "go", "if", "return",
@@ -258,6 +266,10 @@ public void testForStatementKeywordsDoNotInsertSpace() {
258266
public void testFunctionInDefer() {
259267
doTestCompletion();
260268
}
269+
270+
public void testFunctionInMethodInvocation() {
271+
doTestCompletion();
272+
}
261273

262274
public void testFunctionInGo() {
263275
doTestCompletion();

0 commit comments

Comments
 (0)