Search in sources :

Example 51 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class GrLabelBlock method createTextRange.

private static TextRange createTextRange(List<ASTNode> subStatements) {
    ASTNode first = subStatements.get(0);
    ASTNode last = subStatements.get(subStatements.size() - 1);
    return new TextRange(first.getTextRange().getStartOffset(), last.getTextRange().getEndOffset());
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange)

Example 52 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class GroovyBlock method getChildAttributes.

@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
    ASTNode astNode = getNode();
    final PsiElement psiParent = astNode.getPsi();
    if (psiParent instanceof GroovyFileBase) {
        return new ChildAttributes(Indent.getNoneIndent(), null);
    }
    if (psiParent instanceof GrSwitchStatement) {
        List<Block> subBlocks = getSubBlocks();
        if (newChildIndex > 0) {
            Block block = subBlocks.get(newChildIndex - 1);
            if (block instanceof GroovyBlock) {
                PsiElement anchorPsi = ((GroovyBlock) block).getNode().getPsi();
                if (anchorPsi instanceof GrCaseSection) {
                    for (GrStatement statement : ((GrCaseSection) anchorPsi).getStatements()) {
                        if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrReturnStatement || statement instanceof GrThrowStatement) {
                            final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
                            return new ChildAttributes(indent, null);
                        }
                    }
                    int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
                    final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
                    return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
                }
            }
        }
    }
    if (psiParent instanceof GrCaseLabel) {
        return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
    }
    if (psiParent instanceof GrCaseSection) {
        return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
    }
    if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
        return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
    }
    if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
        return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
    }
    if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
        final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
        return new ChildAttributes(indent, null);
    }
    return new ChildAttributes(Indent.getNoneIndent(), null);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrContinueStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) ASTNode(com.intellij.lang.ASTNode) GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrAnnotationArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 53 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class GantUtils method isPlainIdentifier.

public static boolean isPlainIdentifier(final GrArgumentLabel label) {
    final PsiElement elem = label.getNameElement();
    final ASTNode node = elem.getNode();
    if (node == null)
        return false;
    return node.getElementType() == GroovyTokenTypes.mIDENT;
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement)

Example 54 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class ImportReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    if (element instanceof XmlProcessingInstruction) {
        final ASTNode importNode = element.getNode().findChildByType(XmlTokenType.XML_TAG_CHARACTERS);
        if (importNode != null) {
            final PsiElement importInstr = importNode.getPsi();
            final String instructionTarget = JavaFxPsiUtil.getInstructionTarget("import", (XmlProcessingInstruction) element);
            if (instructionTarget != null && instructionTarget.equals(importInstr.getText())) {
                final PsiReference[] references = FxmlReferencesContributor.CLASS_REFERENCE_PROVIDER.getReferencesByString(instructionTarget, element, importInstr.getStartOffsetInParent());
                if (instructionTarget.endsWith(".*")) {
                    return ArrayUtil.remove(references, references.length - 1);
                } else {
                    return references;
                }
            }
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : XmlProcessingInstruction(com.intellij.psi.xml.XmlProcessingInstruction) ASTNode(com.intellij.lang.ASTNode) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 55 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class JavaFxAnnotator method annotate.

@Override
public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
    final PsiFile containingFile = element.getContainingFile();
    if (!JavaFxFileTypeFactory.isFxml(containingFile))
        return;
    if (element instanceof XmlAttributeValue) {
        final String value = ((XmlAttributeValue) element).getValue();
        if (!JavaFxPsiUtil.isExpressionBinding(value) && !JavaFxPsiUtil.isIncorrectExpressionBinding(value)) {
            final PsiReference[] references = element.getReferences();
            for (PsiReference reference : references) {
                if (reference instanceof JavaFxColorReference) {
                    attachColorIcon(element, holder, StringUtil.unquoteString(element.getText()));
                    continue;
                }
                final PsiElement resolve = reference.resolve();
                if (resolve instanceof PsiMember) {
                    if (!JavaFxPsiUtil.isVisibleInFxml((PsiMember) resolve)) {
                        final String symbolPresentation = "'" + SymbolPresentationUtil.getSymbolPresentableText(resolve) + "'";
                        final Annotation annotation = holder.createErrorAnnotation(element, symbolPresentation + (resolve instanceof PsiClass ? " should be public" : " should be public or annotated with @FXML"));
                        if (!(resolve instanceof PsiClass)) {
                            annotation.registerUniversalFix(new AddAnnotationFix(JavaFxCommonNames.JAVAFX_FXML_ANNOTATION, (PsiMember) resolve, ArrayUtil.EMPTY_STRING_ARRAY), null, null);
                        }
                    }
                }
            }
        }
    } else if (element instanceof XmlAttribute) {
        final XmlAttribute attribute = (XmlAttribute) element;
        final String attributeName = attribute.getName();
        if (!FxmlConstants.FX_BUILT_IN_ATTRIBUTES.contains(attributeName) && !attribute.isNamespaceDeclaration() && JavaFxPsiUtil.isReadOnly(attributeName, attribute.getParent())) {
            holder.createErrorAnnotation(element.getNavigationElement(), "Property '" + attributeName + "' is read-only");
        }
        if (FxmlConstants.SOURCE.equals(attributeName)) {
            final XmlAttributeValue valueElement = attribute.getValueElement();
            if (valueElement != null) {
                final XmlTag xmlTag = attribute.getParent();
                if (xmlTag != null) {
                    final XmlTag referencedTag = JavaFxBuiltInTagDescriptor.getReferencedTag(xmlTag);
                    if (referencedTag != null) {
                        if (referencedTag.getTextOffset() > xmlTag.getTextOffset()) {
                            holder.createErrorAnnotation(valueElement.getValueTextRange(), valueElement.getValue() + " not found");
                        } else if (xmlTag.getParentTag() == referencedTag.getParentTag()) {
                            final Annotation annotation = holder.createErrorAnnotation(valueElement.getValueTextRange(), "Duplicate child added");
                            annotation.registerFix(new JavaFxWrapWithDefineIntention(referencedTag, valueElement.getValue()));
                        }
                    }
                }
            }
        }
    } else if (element instanceof XmlTag) {
        if (FxmlConstants.FX_SCRIPT.equals(((XmlTag) element).getName())) {
            final XmlTagValue tagValue = ((XmlTag) element).getValue();
            if (!StringUtil.isEmptyOrSpaces(tagValue.getText())) {
                final List<String> langs = JavaFxPsiUtil.parseInjectedLanguages((XmlFile) element.getContainingFile());
                if (langs.isEmpty()) {
                    final ASTNode openTag = element.getNode().findChildByType(XmlTokenType.XML_NAME);
                    final Annotation annotation = holder.createErrorAnnotation(openTag != null ? openTag.getPsi() : element, "Page language not specified.");
                    annotation.registerFix(new JavaFxInjectPageLanguageIntention());
                }
            }
        }
    }
}
Also used : JavaFxInjectPageLanguageIntention(org.jetbrains.plugins.javaFX.fxml.codeInsight.intentions.JavaFxInjectPageLanguageIntention) Annotation(com.intellij.lang.annotation.Annotation) JavaFxWrapWithDefineIntention(org.jetbrains.plugins.javaFX.fxml.codeInsight.intentions.JavaFxWrapWithDefineIntention) ASTNode(com.intellij.lang.ASTNode) AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix)

Aggregations

ASTNode (com.intellij.lang.ASTNode)748 PsiElement (com.intellij.psi.PsiElement)142 NotNull (org.jetbrains.annotations.NotNull)132 IElementType (com.intellij.psi.tree.IElementType)127 Nullable (org.jetbrains.annotations.Nullable)99 TextRange (com.intellij.openapi.util.TextRange)91 ArrayList (java.util.ArrayList)53 IncorrectOperationException (com.intellij.util.IncorrectOperationException)35 PsiFile (com.intellij.psi.PsiFile)31 Annotation (com.intellij.lang.annotation.Annotation)22 CharTable (com.intellij.util.CharTable)22 Document (com.intellij.openapi.editor.Document)18 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)17 XmlTag (com.intellij.psi.xml.XmlTag)17 Project (com.intellij.openapi.project.Project)16 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)16 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)16 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)15 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)15 PsiReference (com.intellij.psi.PsiReference)14