Search in sources :

Example 86 with ASTNode

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

the class XmlSlashTypedHandler method charTyped.

@Override
public Result charTyped(final char c, final Project project, @NotNull final Editor editor, @NotNull final PsiFile editedFile) {
    if (!WebEditorOptions.getInstance().isAutoCloseTag())
        return Result.CONTINUE;
    if (c == '/' && XmlGtTypedHandler.fileContainsXmlLanguage(editedFile)) {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null)
            return Result.CONTINUE;
        FileViewProvider provider = file.getViewProvider();
        final int offset = editor.getCaretModel().getOffset();
        PsiElement element = provider.findElementAt(offset - 1, XMLLanguage.class);
        if (element == null)
            return Result.CONTINUE;
        if (!(element.getLanguage() instanceof XMLLanguage))
            return Result.CONTINUE;
        ASTNode prevLeaf = element.getNode();
        if (prevLeaf == null)
            return Result.CONTINUE;
        final String prevLeafText = prevLeaf.getText();
        if ("</".equals(prevLeafText) && prevLeaf.getElementType() == XmlTokenType.XML_END_TAG_START) {
            XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
            if (tag != null && StringUtil.isNotEmpty(tag.getName()) && TreeUtil.findSibling(prevLeaf, XmlTokenType.XML_NAME) == null) {
                // check for template language like JSP
                if (provider instanceof MultiplePsiFilesPerDocumentFileViewProvider) {
                    PsiElement element1 = SingleRootFileViewProvider.findElementAt(file, offset - 1);
                    if (element1 != null) {
                        // case of top-level jsp tag
                        XmlTag tag1 = PsiTreeUtil.getParentOfType(element1, XmlTag.class);
                        if (shouldReplace(tag, tag1, offset)) {
                            tag = tag1;
                        } else {
                            // if we have enclosing jsp tag, actual tag to be completed will be previous sibling
                            tag1 = PsiTreeUtil.getPrevSiblingOfType(element1.getParent(), XmlTag.class);
                            if (shouldReplace(tag, tag1, offset)) {
                                tag = tag1;
                            }
                        }
                    }
                }
                EditorModificationUtil.insertStringAtCaret(editor, tag.getName() + ">", false);
                autoIndent(editor);
                return Result.STOP;
            }
        }
        if (!"/".equals(prevLeafText.trim()))
            return Result.CONTINUE;
        while ((prevLeaf = TreeUtil.prevLeaf(prevLeaf)) != null && prevLeaf.getElementType() == XmlTokenType.XML_WHITE_SPACE) ;
        if (prevLeaf instanceof OuterLanguageElement) {
            element = file.getViewProvider().findElementAt(offset - 1, file.getLanguage());
            prevLeaf = element != null ? element.getNode() : null;
            while ((prevLeaf = TreeUtil.prevLeaf(prevLeaf)) != null && prevLeaf.getElementType() == XmlTokenType.XML_WHITE_SPACE) ;
        }
        if (prevLeaf == null)
            return Result.CONTINUE;
        XmlTag tag = PsiTreeUtil.getParentOfType(prevLeaf.getPsi(), XmlTag.class);
        if (tag == null) {
            // prevLeaf maybe in one tree and element in another
            PsiElement element2 = provider.findElementAt(prevLeaf.getStartOffset(), XMLLanguage.class);
            tag = PsiTreeUtil.getParentOfType(element2, XmlTag.class);
            if (tag == null)
                return Result.CONTINUE;
        }
        final XmlToken startToken = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_START_TAG_START);
        if (startToken == null || !startToken.getText().equals("<"))
            return Result.CONTINUE;
        if (XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) != null)
            return Result.CONTINUE;
        if (XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) != null)
            return Result.CONTINUE;
        if (PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class) != null)
            return Result.CONTINUE;
        EditorModificationUtil.insertStringAtCaret(editor, ">", false);
        return Result.STOP;
    }
    return Result.CONTINUE;
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) ASTNode(com.intellij.lang.ASTNode) XMLLanguage(com.intellij.lang.xml.XMLLanguage)

Example 87 with ASTNode

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

the class XmlZenCodingGeneratorImpl method isTagClosed.

private static boolean isTagClosed(@NotNull XmlTag tag) {
    ASTNode node = tag.getNode();
    assert node != null;
    final ASTNode emptyTagEnd = XmlChildRole.EMPTY_TAG_END_FINDER.findChild(node);
    final ASTNode endTagEnd = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(node);
    return emptyTagEnd != null || endTagEnd != null;
}
Also used : ASTNode(com.intellij.lang.ASTNode)

Example 88 with ASTNode

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

the class XmlTagTreeHighlightingPass method getEndTagRange.

@Nullable
private static TextRange getEndTagRange(ASTNode tagNode) {
    final ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(tagNode);
    if (endTagStart == null) {
        return null;
    }
    ASTNode endTagEnd = endTagStart;
    while (endTagEnd != null && endTagEnd.getElementType() != XmlTokenType.XML_TAG_END) {
        endTagEnd = endTagEnd.getTreeNext();
    }
    if (endTagEnd == null) {
        return null;
    }
    return new TextRange(endTagStart.getStartOffset(), endTagEnd.getTextRange().getEndOffset());
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 89 with ASTNode

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

the class XmlEncodingReferenceProvider method xmlAttributeValueRange.

protected static TextRange xmlAttributeValueRange(final XmlAttributeValue xmlAttributeValue) {
    ASTNode valueNode = XmlChildRole.ATTRIBUTE_VALUE_VALUE_FINDER.findChild(xmlAttributeValue.getNode());
    PsiElement toHighlight = valueNode == null ? xmlAttributeValue : valueNode.getPsi();
    TextRange childRange = toHighlight.getTextRange();
    TextRange range = xmlAttributeValue.getTextRange();
    return childRange.shiftRight(-range.getStartOffset());
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 90 with ASTNode

use of com.intellij.lang.ASTNode in project kotlin by JetBrains.

the class KtWhenConditionInRange method getRangeExpression.

@Nullable
@IfNotParsed
public KtExpression getRangeExpression() {
    // Copied from JetBinaryExpression
    ASTNode node = getOperationReference().getNode().getTreeNext();
    while (node != null) {
        PsiElement psi = node.getPsi();
        if (psi instanceof KtExpression) {
            return (KtExpression) psi;
        }
        node = node.getTreeNext();
    }
    return null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

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