Search in sources :

Example 1 with PsiRecursiveElementWalkingVisitor

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;
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) PsiRecursiveElementWalkingVisitor(com.intellij.psi.PsiRecursiveElementWalkingVisitor) HashMap(java.util.HashMap) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiRecursiveElementWalkingVisitor

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();
}
Also used : ConvertKeywordPairToTypeOperation(org.elixir_lang.local_quick_fix.ConvertKeywordPairToTypeOperation) PsiRecursiveElementWalkingVisitor(com.intellij.psi.PsiRecursiveElementWalkingVisitor) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PsiRecursiveElementWalkingVisitor

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);
        }
    });
}
Also used : PsiRecursiveElementWalkingVisitor(com.intellij.psi.PsiRecursiveElementWalkingVisitor) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) NoParentheses(org.elixir_lang.psi.call.arguments.star.NoParentheses)

Example 4 with PsiRecursiveElementWalkingVisitor

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();
}
Also used : Infix(org.elixir_lang.psi.operation.Infix) TextRange(com.intellij.openapi.util.TextRange) Match(org.elixir_lang.psi.operation.Match) PsiRecursiveElementWalkingVisitor(com.intellij.psi.PsiRecursiveElementWalkingVisitor) ConvertMatchToTypeOperation(org.elixir_lang.local_quick_fix.ConvertMatchToTypeOperation) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiRecursiveElementWalkingVisitor

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;
}
Also used : THashMap(gnu.trove.THashMap) PsiRecursiveElementWalkingVisitor(com.intellij.psi.PsiRecursiveElementWalkingVisitor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)6 PsiRecursiveElementWalkingVisitor (com.intellij.psi.PsiRecursiveElementWalkingVisitor)6 NotNull (org.jetbrains.annotations.NotNull)3 TextRange (com.intellij.openapi.util.TextRange)2 ASTNode (com.intellij.lang.ASTNode)1 WebReference (com.intellij.openapi.paths.WebReference)1 PsiReference (com.intellij.psi.PsiReference)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 THashMap (gnu.trove.THashMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ConvertKeywordPairToTypeOperation (org.elixir_lang.local_quick_fix.ConvertKeywordPairToTypeOperation)1 ConvertMatchToTypeOperation (org.elixir_lang.local_quick_fix.ConvertMatchToTypeOperation)1 NoParentheses (org.elixir_lang.psi.call.arguments.star.NoParentheses)1 Infix (org.elixir_lang.psi.operation.Infix)1 Match (org.elixir_lang.psi.operation.Match)1 NamedArgumentDescriptor (org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor)1 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)1