Search in sources :

Example 1 with PsiErrorElement

use of com.intellij.psi.PsiErrorElement in project intellij-elixir by KronicDeth.

the class CallDefinitionHead method stripGuard.

/**
     * The head without the guard clause
     *
     * @param head {@code name(arg, ...) when ...}
     * @return {@code name(arg, ...)}.  {@code head} if no guard clause.
     */
@NotNull
public static PsiElement stripGuard(@NotNull final PsiElement head) {
    PsiElement stripped = head;
    if (head instanceof ElixirMatchedWhenOperation) {
        ElixirMatchedWhenOperation whenOperation = (ElixirMatchedWhenOperation) head;
        PsiElement[] children = whenOperation.getChildren();
        int operatorIndex = operatorIndex(children);
        int onlyNonErrorIndex = -1;
        for (int i = 0; i < operatorIndex; i++) {
            if (!(children[i] instanceof PsiErrorElement)) {
                if (onlyNonErrorIndex == -1) {
                    // first
                    onlyNonErrorIndex = i;
                } else {
                    // more than one
                    onlyNonErrorIndex = -1;
                    break;
                }
            }
        }
        if (onlyNonErrorIndex != -1) {
            stripped = stripGuard(children[onlyNonErrorIndex]);
        }
    }
    return stripped;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiErrorElement

use of com.intellij.psi.PsiErrorElement in project Main by SpartanRefactoring.

the class Playground method spartanizeButtonClicked.

private void spartanizeButtonClicked() {
    if (inputArea.getText().trim().isEmpty()) {
        return;
    }
    Toolbox.getInstance().playground = true;
    PsiFile file = null;
    final boolean[] worked = { true };
    int i;
    for (i = 0; worked[0] && i < before.length; i++, worked[0] = true) {
        file = PsiFileFactory.getInstance(Utils.getProject()).createFileFromText(JavaLanguage.INSTANCE, before[i] + inputArea.getText() + after[i]);
        file.accept(new JavaRecursiveElementVisitor() {

            @Override
            public void visitErrorElement(PsiErrorElement element) {
                worked[0] = false;
                super.visitErrorElement(element);
            }
        });
        if (worked[0]) {
            break;
        }
    }
    if (i < before.length && file != null) {
        Spartanizer.spartanizeFileOnePass(file);
        outputArea.setText(fixString(file.getText(), i));
    } else {
        outputArea.setText(inputArea.getText());
    }
    Toolbox.getInstance().playground = false;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) PsiFile(com.intellij.psi.PsiFile) JavaRecursiveElementVisitor(com.intellij.psi.JavaRecursiveElementVisitor)

Example 3 with PsiErrorElement

use of com.intellij.psi.PsiErrorElement in project intellij-community by JetBrains.

the class RemoveExtraClosingTagIntentionAction method doFix.

private static void doFix(@NotNull final PsiElement element) throws IncorrectOperationException {
    final XmlToken endNameToken = (XmlToken) element;
    final PsiElement tagElement = endNameToken.getParent();
    if (!(tagElement instanceof XmlTag) && !(tagElement instanceof PsiErrorElement))
        return;
    if (tagElement instanceof PsiErrorElement) {
        tagElement.delete();
    } else {
        final ASTNode astNode = tagElement.getNode();
        if (astNode != null) {
            final ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(astNode);
            if (endTagStart != null) {
                final Document document = PsiDocumentManager.getInstance(element.getProject()).getDocument(tagElement.getContainingFile());
                if (document != null) {
                    document.deleteString(endTagStart.getStartOffset(), tagElement.getLastChild().getTextRange().getEndOffset());
                }
            }
        }
    }
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) ASTNode(com.intellij.lang.ASTNode) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Example 4 with PsiErrorElement

use of com.intellij.psi.PsiErrorElement in project kotlin by JetBrains.

the class CheckerTestUtil method getDiagnosticsIncludingSyntaxErrors.

@NotNull
public static List<ActualDiagnostic> getDiagnosticsIncludingSyntaxErrors(@NotNull BindingContext bindingContext, @NotNull PsiElement root, boolean markDynamicCalls, @Nullable List<DeclarationDescriptor> dynamicCallDescriptors, @Nullable String platform) {
    List<ActualDiagnostic> diagnostics = new ArrayList<ActualDiagnostic>();
    for (Diagnostic diagnostic : bindingContext.getDiagnostics().all()) {
        if (PsiTreeUtil.isAncestor(root, diagnostic.getPsiElement(), false)) {
            diagnostics.add(new ActualDiagnostic(diagnostic, platform));
        }
    }
    for (PsiErrorElement errorElement : AnalyzingUtils.getSyntaxErrorRanges(root)) {
        diagnostics.add(new ActualDiagnostic(new SyntaxErrorDiagnostic(errorElement), platform));
    }
    diagnostics.addAll(getDebugInfoDiagnostics(root, bindingContext, markDynamicCalls, dynamicCallDescriptors, platform));
    return diagnostics;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) Diagnostic(org.jetbrains.kotlin.diagnostics.Diagnostic) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiErrorElement

use of com.intellij.psi.PsiErrorElement in project intellij-community by JetBrains.

the class GroovySpacingProcessorBasic method getSpacing.

public static Spacing getSpacing(GroovyBlock child1, GroovyBlock child2, FormattingContext context) {
    ASTNode leftNode = child1.getNode();
    ASTNode rightNode = child2.getNode();
    final PsiElement left = leftNode.getPsi();
    final PsiElement right = rightNode.getPsi();
    IElementType leftType = leftNode.getElementType();
    IElementType rightType = rightNode.getElementType();
    final CommonCodeStyleSettings settings = context.getSettings();
    final GroovyCodeStyleSettings groovySettings = context.getGroovySettings();
    if (!(mirrorsAst(child1) && mirrorsAst(child2))) {
        return NO_SPACING;
    }
    if (child2 instanceof ClosureBodyBlock) {
        return settings.SPACE_WITHIN_BRACES ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (child1 instanceof ClosureBodyBlock) {
        return createDependentSpacingForClosure(settings, groovySettings, (GrClosableBlock) left.getParent(), false);
    }
    if (leftType == GroovyDocElementTypes.GROOVY_DOC_COMMENT) {
        return COMMON_SPACING_WITH_NL;
    }
    if (right instanceof GrTypeArgumentList) {
        return NO_SPACING_WITH_NEWLINE;
    }
    /********** punctuation marks ************/
    if (GroovyTokenTypes.mCOMMA == leftType) {
        return settings.SPACE_AFTER_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mCOMMA == rightType) {
        return settings.SPACE_BEFORE_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mSEMI == leftType) {
        return settings.SPACE_AFTER_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mSEMI == rightType) {
        return settings.SPACE_BEFORE_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    // For dots, commas etc.
    if ((TokenSets.DOTS.contains(rightType)) || (GroovyTokenTypes.mCOLON.equals(rightType) && !(right.getParent() instanceof GrConditionalExpression))) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (TokenSets.DOTS.contains(leftType)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    //todo:check it for multiple assignments
    if ((GroovyElementTypes.VARIABLE_DEFINITION.equals(leftType) || GroovyElementTypes.VARIABLE_DEFINITION.equals(rightType)) && !(leftNode.getTreeNext() instanceof PsiErrorElement)) {
        return Spacing.createSpacing(0, 0, 1, false, 100);
    }
    // For regexes
    if (leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mREGEX_LITERAL || leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
        return NO_SPACING;
    }
    // For << and >> ...
    if ((GroovyTokenTypes.mLT.equals(leftType) && GroovyTokenTypes.mLT.equals(rightType)) || (GroovyTokenTypes.mGT.equals(leftType) && GroovyTokenTypes.mGT.equals(rightType))) {
        return NO_SPACING_WITH_NEWLINE;
    }
    // Unary and postfix expressions
    if (SpacingTokens.PREFIXES.contains(leftType) || SpacingTokens.POSTFIXES.contains(rightType) || (SpacingTokens.PREFIXES_OPTIONAL.contains(leftType) && left.getParent() instanceof GrUnaryExpression)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (SpacingTokens.RANGES.contains(leftType) || SpacingTokens.RANGES.contains(rightType)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyDocTokenTypes.mGDOC_ASTERISKS == leftType && GroovyDocTokenTypes.mGDOC_COMMENT_DATA == rightType) {
        String text = rightNode.getText();
        if (!text.isEmpty() && !StringUtil.startsWithChar(text, ' ')) {
            return COMMON_SPACING;
        }
        return NO_SPACING;
    }
    if (leftType == GroovyDocTokenTypes.mGDOC_TAG_VALUE_TOKEN && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) {
        return LAZY_SPACING;
    }
    if (left instanceof GrStatement && right instanceof GrStatement && left.getParent() instanceof GrStatementOwner && right.getParent() instanceof GrStatementOwner) {
        return COMMON_SPACING_WITH_NL;
    }
    if (rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END) {
        return NO_SPACING;
    }
    if ((leftType == GroovyDocElementTypes.GDOC_INLINED_TAG && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) || (leftType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA && rightType == GroovyDocElementTypes.GDOC_INLINED_TAG)) {
        // Keep formatting between groovy doc text and groovy doc reference tag as is.
        return NO_SPACING;
    }
    if (leftType == GroovyElementTypes.CLASS_TYPE_ELEMENT && rightType == GroovyTokenTypes.mTRIPLE_DOT) {
        return NO_SPACING;
    }
    // diamonds
    if (rightType == GroovyTokenTypes.mLT || rightType == GroovyTokenTypes.mGT) {
        if (right.getParent() instanceof GrCodeReferenceElement) {
            PsiElement p = right.getParent().getParent();
            if (p instanceof GrNewExpression || p instanceof GrAnonymousClassDefinition) {
                return NO_SPACING;
            }
        }
    }
    return COMMON_SPACING;
}
Also used : GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GroovyCodeStyleSettings(org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) IElementType(com.intellij.psi.tree.IElementType) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) PsiErrorElement(com.intellij.psi.PsiErrorElement) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ASTNode(com.intellij.lang.ASTNode) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) ClosureBodyBlock(org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiErrorElement (com.intellij.psi.PsiErrorElement)20 PsiElement (com.intellij.psi.PsiElement)12 XmlTag (com.intellij.psi.xml.XmlTag)4 PsiFile (com.intellij.psi.PsiFile)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 XmlToken (com.intellij.psi.xml.XmlToken)3 NotNull (org.jetbrains.annotations.NotNull)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 HtmlTag (com.intellij.psi.html.HtmlTag)2 IElementType (com.intellij.psi.tree.IElementType)2 XmlDocument (com.intellij.psi.xml.XmlDocument)2 XmlText (com.intellij.psi.xml.XmlText)2 CfmlTag (com.intellij.coldFusion.model.psi.CfmlTag)1 EquivalenceDescriptorProvider (com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider)1 Language (com.intellij.lang.Language)1 Annotation (com.intellij.lang.annotation.Annotation)1 HTMLLanguage (com.intellij.lang.html.HTMLLanguage)1 XMLLanguage (com.intellij.lang.xml.XMLLanguage)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1