Search in sources :

Example 1 with ConvertMatchToTypeOperation

use of org.elixir_lang.local_quick_fix.ConvertMatchToTypeOperation 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)

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiRecursiveElementWalkingVisitor (com.intellij.psi.PsiRecursiveElementWalkingVisitor)1 ConvertMatchToTypeOperation (org.elixir_lang.local_quick_fix.ConvertMatchToTypeOperation)1 Infix (org.elixir_lang.psi.operation.Infix)1 Match (org.elixir_lang.psi.operation.Match)1 NotNull (org.jetbrains.annotations.NotNull)1