Skip to content

Commit e63a250

Browse files
committed
Merge branch 'func-exit-points-highlighting' of https://github.com/ilinum/go-lang-idea-plugin into ilinum-func-exit-points-highlighting
2 parents d5e657e + 72b391d commit e63a250

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

src/com/goide/highlighting/GoHighlightExitPointsHandlerFactory.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ protected void selectTargets(List<PsiElement> targets, @NotNull Consumer<List<Ps
6363

6464
@Override
6565
public void computeUsages(List<PsiElement> targets) {
66+
if (myTarget instanceof LeafPsiElement && ((LeafPsiElement)myTarget).getElementType() == GoTypes.FUNC) {
67+
addOccurrence(myTarget);
68+
}
6669
new GoRecursiveVisitor() {
6770
@Override
6871
public void visitFunctionLit(@NotNull GoFunctionLit literal) {
@@ -85,12 +88,20 @@ public void visitCallExpr(@NotNull GoCallExpr o) {
8588
public static MyHandler createForElement(@NotNull Editor editor, PsiFile file, PsiElement element) {
8689
GoTypeOwner function = PsiTreeUtil.getParentOfType(element, GoFunctionLit.class, GoFunctionOrMethodDeclaration.class);
8790
if (function == null) return null;
88-
if (element instanceof LeafPsiElement && ((LeafPsiElement)element).getElementType() == GoTypes.RETURN || isPanicCall(element)) {
91+
if (shouldCreateMyHandler(element)) {
8992
return new MyHandler(editor, file, element, function);
9093
}
9194
return null;
9295
}
9396

97+
private static boolean shouldCreateMyHandler(PsiElement element) {
98+
if (element instanceof LeafPsiElement) {
99+
LeafPsiElement leaf = (LeafPsiElement)element;
100+
return leaf.getElementType() == GoTypes.RETURN || leaf.getElementType() == GoTypes.FUNC || isPanicCall(leaf);
101+
}
102+
return false;
103+
}
104+
94105
private static boolean isPanicCall(@NotNull PsiElement e) {
95106
PsiElement parent = e.getParent();
96107
if (parent instanceof GoReferenceExpression) {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Florin Patan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.goide.editor;
18+
19+
import com.goide.GoCodeInsightFixtureTestCase;
20+
import com.intellij.codeInsight.highlighting.HighlightUsagesHandler;
21+
import com.intellij.codeInsight.highlighting.HighlightUsagesHandlerBase;
22+
import com.intellij.openapi.util.TextRange;
23+
import com.intellij.psi.PsiElement;
24+
import com.intellij.testFramework.LightProjectDescriptor;
25+
import org.jetbrains.annotations.NotNull;
26+
27+
import java.util.ArrayList;
28+
import java.util.Arrays;
29+
import java.util.List;
30+
31+
32+
public class GoExitPointsHighlightingTest extends GoCodeInsightFixtureTestCase {
33+
@Override
34+
protected void setUp() throws Exception {
35+
super.setUp();
36+
setUpProjectSdk();
37+
}
38+
39+
protected LightProjectDescriptor getProjectDescriptor() {
40+
return createMockProjectDescriptor();
41+
}
42+
43+
public void testBasicExitPoints() {
44+
String text = "package main;\n" +
45+
"func bar(x int) int {\n" +
46+
" if (x < 10) {\n" +
47+
" retur<caret>n -1" +
48+
" }" +
49+
" return x\n" +
50+
"}";
51+
doTest(text, "return -1", "return x");
52+
}
53+
54+
public void testCaretOnFuncWithReturnAndPanic() {
55+
String text = "package main;\n" +
56+
"fun<caret>c bar(x int) int {\n" +
57+
" if (9 < 10) {\n" +
58+
" return -1" +
59+
" }" +
60+
" panic(x)\n" +
61+
"}";
62+
doTest(text, "func", "return -1", "panic(x)");
63+
}
64+
65+
public void testCaretOnFuncWithNoExitPoints() {
66+
String text = "package main\n" +
67+
"import \"fmt\"\n" +
68+
"f<caret>unc main() {\n" +
69+
" fmt.Println(\"Hello, world!\"\n" +
70+
"}";
71+
doTest(text, "func");
72+
}
73+
74+
public void testCaretOnFuncOnType() {
75+
String text = "package main\n" +
76+
"\n" +
77+
"type Demo struct {}\n" +
78+
"\n" +
79+
"f<caret>unc (a Demo) demo() int {\n" +
80+
" return -1\n" +
81+
"}";
82+
doTest(text, "func", "return -1");
83+
}
84+
85+
private void doTest(@NotNull String text, String... usages) {
86+
myFixture.configureByText("foo.go", text);
87+
@SuppressWarnings("unchecked")
88+
HighlightUsagesHandlerBase<PsiElement> handler = HighlightUsagesHandler.createCustomHandler(myFixture.getEditor(), myFixture.getFile());
89+
assertNotNull(handler);
90+
List<PsiElement> targets = handler.getTargets();
91+
assertEquals(1, targets.size());
92+
93+
handler.computeUsages(targets);
94+
List<TextRange> readUsages = handler.getReadUsages();
95+
assertEquals(usages.length, readUsages.size());
96+
97+
List<String> textUsages = new ArrayList<String>();
98+
for (TextRange usage : readUsages) {
99+
String usageText = myFixture.getFile().getText().substring(usage.getStartOffset(), usage.getEndOffset());
100+
textUsages.add(usageText);
101+
}
102+
assertSameElements(textUsages, Arrays.asList(usages));
103+
}
104+
}

0 commit comments

Comments
 (0)