Search in sources :

Example 16 with XmlText

use of com.intellij.psi.xml.XmlText in project android by JetBrains.

the class AndroidBaseXmlRefactoringAction method getExtractableRange.

@Nullable
private static Pair<PsiElement, PsiElement> getExtractableRange(PsiFile file, int start, int end) {
    PsiElement startElement = file.findElementAt(start);
    PsiElement parent = startElement != null ? startElement.getParent() : null;
    while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getStartOffset() == startElement.getTextRange().getStartOffset()) {
        startElement = parent;
        parent = parent.getParent();
    }
    PsiElement endElement = file.findElementAt(end - 1);
    parent = endElement != null ? endElement.getParent() : null;
    while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getEndOffset() == endElement.getTextRange().getEndOffset()) {
        endElement = parent;
        parent = parent.getParent();
    }
    if (startElement == null || endElement == null) {
        return null;
    }
    final PsiElement commonParent = startElement.getParent();
    if (commonParent == null || !(commonParent instanceof XmlTag) || commonParent != endElement.getParent()) {
        return null;
    }
    PsiElement e = startElement;
    boolean containTag = false;
    while (e != null) {
        if (!(e instanceof XmlText) && !(e instanceof XmlTag) && !(e instanceof PsiWhiteSpace) && !(e instanceof PsiComment)) {
            return null;
        }
        if (e instanceof XmlTag) {
            containTag = true;
        }
        if (e == endElement) {
            break;
        }
        e = e.getNextSibling();
    }
    return e != null && containTag ? Pair.create(startElement, endElement) : null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiFile(com.intellij.psi.PsiFile) XmlText(com.intellij.psi.xml.XmlText) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with XmlText

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

the class SuppressInspectionAction method invoke.

public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final XmlTag anchor = getAnchor(element);
    if (anchor == null)
        return;
    PsiElement prevSibling = anchor.getPrevSibling();
    while (prevSibling instanceof PsiWhiteSpace || prevSibling instanceof XmlText) {
        prevSibling = prevSibling.getPrevSibling();
    }
    if (prevSibling instanceof XmlProlog) {
        prevSibling = prevSibling.getLastChild();
        if (prevSibling != null && !(prevSibling instanceof XmlComment)) {
            prevSibling = PsiTreeUtil.getPrevSiblingOfType(prevSibling, XmlComment.class);
        }
    }
    if (prevSibling instanceof XmlComment) {
        final XmlComment comment = (XmlComment) prevSibling;
        final String text = XmlUtil.getCommentText(comment);
        if (text != null && InspectionUtil.SUPPRESSION_PATTERN.matcher(text).matches()) {
            final String s = text.trim() + ", " + myToolId;
            final XmlComment newComment = createComment(project, s);
            CodeStyleManager.getInstance(PsiManager.getInstance(project).getProject()).reformat(comment.replace(newComment));
        } else {
            addNoinspectionComment(project, anchor);
        }
    } else {
        addNoinspectionComment(project, anchor);
    }
}
Also used : XmlComment(com.intellij.psi.xml.XmlComment) XmlText(com.intellij.psi.xml.XmlText) XmlProlog(com.intellij.psi.xml.XmlProlog) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 18 with XmlText

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

the class InlineXslAttribute method invoke.

public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final int offset = editor.getCaretModel().getOffset();
    final PsiElement element = file.findElementAt(offset);
    final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
    assert tag != null;
    final StringBuilder sb = new StringBuilder();
    final PsiElement[] children = tag.getChildren();
    for (PsiElement child : children) {
        if (child instanceof XmlText) {
            final XmlText text = (XmlText) child;
            if (text.getText().trim().length() > 0) {
                sb.append(text.getText().replaceAll("\"", "&quot;"));
            }
        } else if (child instanceof XmlTag) {
            final XmlTag t = (XmlTag) child;
            if (XsltSupport.isXsltTag(t)) {
                if ("text".equals(t.getLocalName())) {
                    sb.append(t.getValue().getText().replaceAll("\"", "&quot;"));
                } else if ("value-of".equals(t.getLocalName())) {
                    sb.append("{").append(t.getAttributeValue("select")).append("}");
                } else {
                    assert false;
                }
            }
        }
    }
    final XmlTag p = findParent(tag);
    if (p != null) {
        final String value = tag.getAttributeValue("name");
        p.setAttribute(value, sb.toString());
        // TODO: deal with namespace prefix mapping
        tag.delete();
    }
}
Also used : XmlText(com.intellij.psi.xml.XmlText) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 19 with XmlText

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

the class InlineXslAttribute method isAvailable.

public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (!XsltSupport.isXsltFile(file))
        return false;
    final int offset = editor.getCaretModel().getOffset();
    final PsiElement element = file.findElementAt(offset);
    final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
    if (tag == null) {
        return false;
    }
    if (!XsltSupport.isXsltTag(tag)) {
        return false;
    } else if (!"attribute".equals(tag.getLocalName())) {
        return false;
    } else if (tag.getAttribute("name") == null) {
        return false;
    } else if (findParent(tag) == null) {
        // we cannot "inline" anything that isn't clearly the child of either an xsl:element or a literal result element
        return false;
    }
    final ASTNode node = tag.getNode();
    if (node == null)
        return false;
    final ASTNode nameNode = XmlChildRole.START_TAG_NAME_FINDER.findChild(node);
    if (nameNode == null || !nameNode.getTextRange().contains(offset)) {
        return false;
    }
    final XmlTag[] texts = tag.findSubTags("text", XsltSupport.XSLT_NS);
    final XmlTag[] exprs = tag.findSubTags("value-of", XsltSupport.XSLT_NS);
    final PsiElement[] children = tag.getChildren();
    for (PsiElement child : children) {
        if (child instanceof XmlText) {
            final XmlText text = (XmlText) child;
            if (text.getText().trim().length() == 0) {
                if (texts.length == 0 && exprs.length == 0) {
                    return false;
                }
            }
        } else if (child instanceof XmlTag) {
            final XmlTag t = (XmlTag) child;
            if (XsltSupport.isXsltTag(t)) {
                if ("text".equals(t.getLocalName())) {
                } else if ("value-of".equals(t.getLocalName())) {
                    if (t.getAttribute("select") == null) {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }
    return true;
}
Also used : ASTNode(com.intellij.lang.ASTNode) XmlText(com.intellij.psi.xml.XmlText) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 20 with XmlText

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

the class MavenDependenciesCompletionProvider method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiElement xmlText = parameters.getPosition().getParent();
    if (!(xmlText instanceof XmlText))
        return;
    PsiElement eDependencyTag = xmlText.getParent();
    if (!(eDependencyTag instanceof XmlTag))
        return;
    XmlTag dependencyTag = (XmlTag) eDependencyTag;
    if (!"dependency".equals(dependencyTag.getName()))
        return;
    if (!PsiImplUtil.isLeafElementOfType(xmlText.getPrevSibling(), XmlTokenType.XML_TAG_END) || !PsiImplUtil.isLeafElementOfType(xmlText.getNextSibling(), XmlTokenType.XML_END_TAG_START)) {
        return;
    }
    Project project = dependencyTag.getProject();
    DomElement domElement = DomManager.getDomManager(project).getDomElement(dependencyTag);
    if (!(domElement instanceof MavenDomDependency)) {
        return;
    }
    MavenProjectIndicesManager indicesManager = MavenProjectIndicesManager.getInstance(project);
    for (String groupId : indicesManager.getGroupIds()) {
        for (String artifactId : indicesManager.getArtifactIds(groupId)) {
            LookupElement builder = LookupElementBuilder.create(groupId + ':' + artifactId).withIcon(AllIcons.Nodes.PpLib).withInsertHandler(MavenDependencyInsertHandler.INSTANCE);
            result.addElement(builder);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) MavenProjectIndicesManager(org.jetbrains.idea.maven.indices.MavenProjectIndicesManager) DomElement(com.intellij.util.xml.DomElement) XmlText(com.intellij.psi.xml.XmlText) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency)

Aggregations

XmlText (com.intellij.psi.xml.XmlText)24 XmlTag (com.intellij.psi.xml.XmlTag)16 PsiElement (com.intellij.psi.PsiElement)12 ASTNode (com.intellij.lang.ASTNode)5 TextRange (com.intellij.openapi.util.TextRange)3 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)3 XmlFile (com.intellij.psi.xml.XmlFile)3 Nullable (org.jetbrains.annotations.Nullable)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 Language (com.intellij.lang.Language)2 Project (com.intellij.openapi.project.Project)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 XmlDocument (com.intellij.psi.xml.XmlDocument)2 XmlTagValue (com.intellij.psi.xml.XmlTagValue)2 DomElement (com.intellij.util.xml.DomElement)2 NotNull (org.jetbrains.annotations.NotNull)2 MavenProjectIndicesManager (org.jetbrains.idea.maven.indices.MavenProjectIndicesManager)2 TypographyDetector (com.android.tools.lint.checks.TypographyDetector)1 NegatingComparable (com.intellij.codeInsight.completion.impl.NegatingComparable)1 LookupElementWeigher (com.intellij.codeInsight.lookup.LookupElementWeigher)1