-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathNoParenthesesManyStrict.java
106 lines (89 loc) · 4.08 KB
/
NoParenthesesManyStrict.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package org.elixir_lang.inspection;
import com.intellij.codeInspection.*;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
import org.elixir_lang.psi.*;
import org.elixir_lang.psi.call.arguments.star.NoParentheses;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
/**
* Created by luke.imhoff on 12/5/14.
*/
public class NoParenthesesManyStrict extends LocalInspectionTool {
@Nls
@NotNull
@Override
public String getDisplayName() {
return "Ambiguous nested calls";
}
@Nls
@NotNull
@Override
public String getGroupDisplayName() {
return "Elixir";
}
@NotNull
@Override
public String getShortName() {
return "NoParenthesesManyStrict";
}
public boolean isEnabledByDefault() {
return true;
}
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
ProblemsHolder problemsHolder = new ProblemsHolder(manager, file, isOnTheFly);
checkFile(file, problemsHolder);
return problemsHolder.getResultsArray();
}
private static void checkFile(final PsiFile file, final ProblemsHolder problemsHolder) {
file.accept(
new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
PsiElement elementWithAmbiguousComma = null;
if (element instanceof ElixirUnqualifiedNoParenthesesManyArgumentsCall) {
PsiElement[] children = element.getChildren();
if (children.length > 1) {
PsiElement parent = element.getParent();
if (parent instanceof ElixirNoParenthesesManyStrictNoParenthesesExpression) {
elementWithAmbiguousComma = element;
}
}
} else if (element instanceof ElixirNoParenthesesOneArgument) {
PsiElement[] children = element.getChildren();
if (children.length > 1) {
PsiElement parent = element.getParent();
if (parent instanceof NoParentheses) {
PsiElement grandParent = parent.getParent();
if (grandParent instanceof Arguments) {
PsiElement[] grandParentChildren = grandParent.getChildren();
if (grandParentChildren.length > 1) {
elementWithAmbiguousComma = element;
}
} else if (grandParent instanceof ElixirKeywordPair) {
elementWithAmbiguousComma = element;
}
}
}
}
if (elementWithAmbiguousComma != null) {
int ambiguousCommaIndex = elementWithAmbiguousComma.getText().indexOf(",");
TextRange ambiguousCommaTextRange = new TextRange(
ambiguousCommaIndex,
ambiguousCommaIndex + 1
);
problemsHolder.registerProblem(
element,
"unexpected comma. Parentheses are required to solve ambiguity in nested calls.",
ProblemHighlightType.ERROR,
ambiguousCommaTextRange
);
}
super.visitElement(element);
}
}
);
}
}