use of com.intellij.psi.PsiRecursiveElementWalkingVisitor in project intellij-community by JetBrains.
the class MapArgumentCompletionProvider method findOtherNamedArgumentsInFile.
@NotNull
private static Map<String, NamedArgumentDescriptor> findOtherNamedArgumentsInFile(PsiElement mapOrArgumentList) {
final Map<String, NamedArgumentDescriptor> map = new HashMap<>();
mapOrArgumentList.getContainingFile().accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof GrArgumentLabel) {
final String name = ((GrArgumentLabel) element).getName();
if (GroovyNamesUtil.isIdentifier(name)) {
map.put(name, NamedArgumentDescriptor.SIMPLE_UNLIKELY);
}
}
super.visitElement(element);
}
});
return map;
}
use of com.intellij.psi.PsiRecursiveElementWalkingVisitor in project intellij-elixir by KronicDeth.
the class KeywordPairColonInsteadOfTypeOperator method checkFile.
/*
* Instance Methods
*/
@NotNull
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
final ProblemsHolder problemsHolder = new ProblemsHolder(manager, file, isOnTheFly);
file.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(@NotNull final PsiElement element) {
// See org.elixir_lang.annotator.ModuleAttribute.annotate for path of checks
if (element instanceof AtUnqualifiedNoParenthesesCall) {
visitAtUnqualifiedNoParenthesesCall((AtUnqualifiedNoParenthesesCall) element);
}
super.visitElement(element);
}
private void visitAtUnqualifiedNoParenthesesCall(@NotNull final AtUnqualifiedNoParenthesesCall atUnqualifiedNoParenthesesCall) {
ElixirAtIdentifier atIdentifier = atUnqualifiedNoParenthesesCall.getAtIdentifier();
String identifier = identifierName(atIdentifier);
if (isTypeName(identifier)) {
PsiElement child = atUnqualifiedNoParenthesesCall.getNoParenthesesOneArgument();
PsiElement[] grandChildren = child.getChildren();
if (grandChildren.length == 1) {
PsiElement grandChild = grandChildren[0];
if (grandChild instanceof QuotableKeywordList) {
QuotableKeywordList quotableKeywordList = (QuotableKeywordList) grandChild;
List<QuotableKeywordPair> quotableKeywordPairList = quotableKeywordList.quotableKeywordPairList();
if (quotableKeywordPairList.size() == 1) {
QuotableKeywordPair quotableKeywordPair = quotableKeywordPairList.get(0);
Quotable keywordKey = quotableKeywordPair.getKeywordKey();
PsiElement keywordPairColon = keywordKey.getNextSibling();
LocalQuickFix localQuickFix = new ConvertKeywordPairToTypeOperation(/* Can't be KEYWORD_PAIR_COLON because caret is never on the single
character in editor mode, only to left or right */
keywordPairColon);
problemsHolder.registerProblem(keywordPairColon, "Type specifications separate the name from the definition using `::`, not `:`", ProblemHighlightType.ERROR, localQuickFix);
}
}
}
}
}
});
return problemsHolder.getResultsArray();
}
use of com.intellij.psi.PsiRecursiveElementWalkingVisitor in project intellij-elixir by KronicDeth.
the class KeywordsNotAtEnd method checkFile.
private static void checkFile(final PsiFile file, final ProblemsHolder problemsHolder) {
file.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
PsiElement keywordsElementNotAtEnd = null;
PsiElement listElement = null;
if (element instanceof ElixirNoParenthesesKeywords) {
ElixirNoParenthesesKeywords keywordsElement = (ElixirNoParenthesesKeywords) element;
PsiElement parent = element.getParent();
if (parent instanceof ElixirMatchedWhenOperation) {
PsiElement grandParent = parent.getParent();
if (grandParent instanceof ElixirNoParenthesesOneArgument) {
listElement = grandParent;
keywordsElementNotAtEnd = findKeywordsElementNotAtEnd(listElement, parent, keywordsElement);
}
} else if (parent instanceof ElixirNoParenthesesOneArgument) {
PsiElement grandParent = parent.getParent();
if (grandParent instanceof NoParentheses) {
PsiElement greatGrandParent = grandParent.getParent();
if (greatGrandParent instanceof ElixirNoParenthesesOneArgument) {
listElement = greatGrandParent;
keywordsElementNotAtEnd = findKeywordsElementNotAtEnd(listElement, grandParent, keywordsElement);
} else if (greatGrandParent instanceof ElixirParenthesesArguments) {
listElement = greatGrandParent;
keywordsElementNotAtEnd = findKeywordsElementNotAtEnd(listElement, grandParent, keywordsElement);
}
}
} else if (parent instanceof ElixirUnmatchedWhenOperation) {
PsiElement grandParent = parent.getParent();
if (grandParent instanceof ElixirParenthesesArguments) {
listElement = grandParent;
keywordsElementNotAtEnd = findKeywordsElementNotAtEnd(listElement, parent, keywordsElement);
}
} else if (parent instanceof NoParentheses) {
PsiElement grandParent = parent.getParent();
if (grandParent instanceof ElixirNoParenthesesManyStrictNoParenthesesExpression) {
PsiElement greatGrandParent = grandParent.getParent();
if (greatGrandParent instanceof ElixirNoParenthesesOneArgument) {
listElement = greatGrandParent;
keywordsElementNotAtEnd = findKeywordsElementNotAtEnd(listElement, grandParent, keywordsElement);
}
}
}
}
if (keywordsElementNotAtEnd != null) {
TextRange listElementTextRange = listElement.getTextRange();
TextRange keywordsTextRange = keywordsElementNotAtEnd.getTextRange();
int listElementStartOffset = listElementTextRange.getStartOffset();
TextRange relativeTextRange = new TextRange(keywordsTextRange.getStartOffset() - listElementStartOffset, keywordsTextRange.getEndOffset() - listElementStartOffset);
problemsHolder.registerProblem(listElement, "Keywords appear before the end of list. Move keywords after positional arguments.", ProblemHighlightType.ERROR, relativeTextRange);
}
super.visitElement(element);
}
});
}
use of com.intellij.psi.PsiRecursiveElementWalkingVisitor in project intellij-elixir by KronicDeth.
the class MatchOperatorInsteadOfTypeOperator method checkFile.
/*
* Instance Methods
*/
@NotNull
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
final ProblemsHolder problemsHolder = new ProblemsHolder(manager, file, isOnTheFly);
file.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(@NotNull final PsiElement element) {
// See org.elixir_lang.annotator.ModuleAttribute.annotate for path of checks
if (element instanceof AtUnqualifiedNoParenthesesCall) {
visitAtUnqualifiedNoParenthesesCall((AtUnqualifiedNoParenthesesCall) element);
}
super.visitElement(element);
}
private void visitAtUnqualifiedNoParenthesesCall(@NotNull final AtUnqualifiedNoParenthesesCall atUnqualifiedNoParenthesesCall) {
ElixirAtIdentifier atIdentifier = atUnqualifiedNoParenthesesCall.getAtIdentifier();
String identifier = identifierName(atIdentifier);
if (isTypeName(identifier)) {
PsiElement child = atUnqualifiedNoParenthesesCall.getNoParenthesesOneArgument();
PsiElement[] grandChildren = child.getChildren();
if (grandChildren.length == 1) {
PsiElement grandChild = grandChildren[0];
if (grandChild instanceof Match) {
Infix infix = (Infix) grandChild;
Operator operator = infix.operator();
int elementStartOffset = operator.getTextOffset();
ASTNode astNode = operatorTokenNode(operator);
int nodeStartOffset = astNode.getStartOffset();
int nodeTextLength = astNode.getTextLength();
int relativeStart = nodeStartOffset - elementStartOffset;
TextRange relativeTextRange = new TextRange(relativeStart, relativeStart + nodeTextLength);
LocalQuickFix localQuickFix = new ConvertMatchToTypeOperation(astNode);
problemsHolder.registerProblem(operator, "Type specifications separate the name from the definition using `::`, not `=`", ProblemHighlightType.ERROR, relativeTextRange, localQuickFix);
}
}
}
}
});
return problemsHolder.getResultsArray();
}
use of com.intellij.psi.PsiRecursiveElementWalkingVisitor in project intellij-community by JetBrains.
the class ShuffleNamesAction method shuffleIds.
private static boolean shuffleIds(PsiFile file, Editor editor) {
final Map<String, String> map = new THashMap<>();
final StringBuilder sb = new StringBuilder();
final StringBuilder quote = new StringBuilder();
final ArrayList<String> split = new ArrayList<>(100);
file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof LeafPsiElement) {
String type = ((LeafPsiElement) element).getElementType().toString();
String text = element.getText();
if (text.isEmpty())
return;
for (int i = 0, len = text.length(); i < len / 2; i++) {
char c = text.charAt(i);
if (c == text.charAt(len - i - 1) && !Character.isLetter(c)) {
quote.append(c);
} else
break;
}
boolean isQuoted = quote.length() > 0;
boolean isNumber = false;
if (isQuoted || type.equals("ID") || type.contains("IDENT") && !"ts".equals(text) || (isNumber = text.matches("[0-9]+"))) {
String replacement = map.get(text);
if (replacement == null) {
split.addAll(Arrays.asList((isQuoted ? text.substring(quote.length(), text.length() - quote.length()).replace("''", "") : text).split("")));
if (!isNumber) {
for (ListIterator<String> it = split.listIterator(); it.hasNext(); ) {
String s = it.next();
if (s.isEmpty()) {
it.remove();
continue;
}
int c = s.charAt(0);
int cap = c & 32;
c &= ~cap;
c = (char) ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 7) % 26 + 'A') : c) | cap;
it.set(String.valueOf((char) c));
}
}
Collections.shuffle(split);
if (isNumber && "0".equals(split.get(0))) {
split.set(0, "1");
}
replacement = StringUtil.join(split, "");
if (isQuoted) {
replacement = quote + replacement + quote.reverse();
}
map.put(text, replacement);
}
text = replacement;
}
sb.append(text);
quote.setLength(0);
split.clear();
}
super.visitElement(element);
}
});
editor.getDocument().setText(sb.toString());
return true;
}
Aggregations