Search in sources :

Example 1 with XmlElement

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

the class XPathEvalAction method execute.

private void execute(Editor editor) {
    final Project project = editor.getProject();
    final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (psiFile == null) {
        return;
    }
    InputExpressionDialog.Context input;
    XmlElement contextNode = null;
    final Config cfg = XPathAppComponent.getInstance().getConfig();
    do {
        RangeHighlighter contextHighlighter = null;
        if (cfg.isUseContextAtCursor()) {
            // find out current context node
            contextNode = MyPsiUtil.findContextNode(psiFile, editor);
            if (contextNode != null) {
                contextHighlighter = HighlighterUtil.highlightNode(editor, contextNode, cfg.getContextAttributes(), cfg);
            }
        }
        if (contextNode == null) {
            // in XPath data model, / is the document itself, including comments, PIs and the root element
            contextNode = ((XmlFile) psiFile).getDocument();
            if (contextNode == null) {
                FileViewProvider fileViewProvider = psiFile.getViewProvider();
                if (fileViewProvider instanceof TemplateLanguageFileViewProvider) {
                    Language dataLanguage = ((TemplateLanguageFileViewProvider) fileViewProvider).getTemplateDataLanguage();
                    PsiFile templateDataFile = fileViewProvider.getPsi(dataLanguage);
                    if (templateDataFile instanceof XmlFile)
                        contextNode = ((XmlFile) templateDataFile).getDocument();
                }
            }
        }
        input = inputXPathExpression(project, contextNode);
        if (contextHighlighter != null) {
            contextHighlighter.dispose();
        }
        if (input == null) {
            return;
        }
        HighlighterUtil.clearHighlighters(editor);
    } while (contextNode != null && evaluateExpression(input, contextNode, editor, cfg));
}
Also used : Project(com.intellij.openapi.project.Project) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) XmlFile(com.intellij.psi.xml.XmlFile) XmlElement(com.intellij.psi.xml.XmlElement) PsiFile(com.intellij.psi.PsiFile) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) InputExpressionDialog(org.intellij.plugins.xpathView.ui.InputExpressionDialog)

Example 2 with XmlElement

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

the class QNameUtil method createQName.

public static QName createQName(@NotNull String qname, @NotNull PsiElement context) {
    final String[] strings = qname.split(":", 2);
    if (strings.length == 1) {
        return new QName(null, qname);
    }
    final XmlElement ctx = PsiTreeUtil.getParentOfType(context, XmlElement.class, false);
    if (ctx == null)
        return UNRESOLVED;
    final String uri = XsltNamespaceContext.getNamespaceUriStatic(strings[0], ctx);
    if (uri == null)
        return UNRESOLVED;
    return new QName(uri, strings[1], strings[0]);
}
Also used : QName(javax.xml.namespace.QName) XmlElement(com.intellij.psi.xml.XmlElement)

Example 3 with XmlElement

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

the class RngDocumentationProvider method generateDoc.

@Override
@Nullable
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    final XmlElement c = PsiTreeUtil.getParentOfType(originalElement, XmlTag.class, XmlAttribute.class);
    if (c != null && c.getManager() == null) {
        LOG.warn("Invalid context element passed to generateDoc()", new Throwable("<stack trace>"));
        return null;
    }
    if (c instanceof XmlTag) {
        final XmlTag xmlElement = (XmlTag) c;
        final XmlElementDescriptor descriptor = xmlElement.getDescriptor();
        if (descriptor instanceof CompositeDescriptor) {
            final StringBuilder sb = new StringBuilder();
            final CompositeDescriptor d = (CompositeDescriptor) descriptor;
            final DElementPattern[] patterns = d.getElementPatterns();
            final THashSet<PsiElement> elements = ContainerUtil.newIdentityTroveSet();
            for (DElementPattern pattern : patterns) {
                final PsiElement psiElement = d.getDeclaration(pattern.getLocation());
                if (psiElement instanceof XmlTag && elements.add(psiElement)) {
                    if (sb.length() > 0) {
                        sb.append("<hr>");
                    }
                    sb.append(getDocumentationFromTag((XmlTag) psiElement, xmlElement.getLocalName(), "Element"));
                }
            }
            return makeDocumentation(sb);
        } else if (descriptor instanceof RngElementDescriptor) {
            final RngElementDescriptor d = (RngElementDescriptor) descriptor;
            final PsiElement declaration = d.getDeclaration();
            if (declaration instanceof XmlTag) {
                return makeDocumentation(getDocumentationFromTag((XmlTag) declaration, xmlElement.getLocalName(), "Element"));
            }
        }
    } else if (c instanceof XmlAttribute) {
        final XmlAttribute attribute = (XmlAttribute) c;
        final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
        if (descriptor instanceof RngXmlAttributeDescriptor) {
            final RngXmlAttributeDescriptor d = (RngXmlAttributeDescriptor) descriptor;
            final StringBuilder sb = new StringBuilder();
            final Collection<PsiElement> declaration = ContainerUtil.newIdentityTroveSet(d.getDeclarations());
            for (PsiElement psiElement : declaration) {
                if (psiElement instanceof XmlTag) {
                    if (sb.length() > 0) {
                        sb.append("<hr>");
                    }
                    sb.append(getDocumentationFromTag((XmlTag) psiElement, d.getName(), "Attribute"));
                }
            }
            return makeDocumentation(sb);
        }
    } else if (element instanceof XmlTag) {
        return makeDocumentation(getDocumentationFromTag((XmlTag) element, ((XmlTag) element).getLocalName(), "Element"));
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) RngXmlAttributeDescriptor(org.intellij.plugins.relaxNG.model.descriptors.RngXmlAttributeDescriptor) RngElementDescriptor(org.intellij.plugins.relaxNG.model.descriptors.RngElementDescriptor) RngXmlAttributeDescriptor(org.intellij.plugins.relaxNG.model.descriptors.RngXmlAttributeDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlElement(com.intellij.psi.xml.XmlElement) CompositeDescriptor(org.intellij.plugins.relaxNG.model.descriptors.CompositeDescriptor) Collection(java.util.Collection) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) DElementPattern(org.kohsuke.rngom.digested.DElementPattern) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with XmlElement

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

the class XsltExtractFunctionAction method getSettings.

protected RefactoringOptions getSettings(XPathExpression expression, Set<XPathExpression> matchingExpressions) {
    final String name = Messages.showInputDialog(expression.getProject(), "Function Name: ", getRefactoringName(), Messages.getQuestionIcon());
    final boolean[] b = new boolean[] { false };
    if (name != null) {
        final String[] parts = name.split(":", 2);
        if (parts.length < 2) {
            Messages.showMessageDialog(expression.getProject(), "Custom functions require a prefixed name", "Error", Messages.getErrorIcon());
            b[0] = true;
        }
        final XmlElement context = PsiTreeUtil.getContextOfType(expression, XmlElement.class);
        final NamespaceContext namespaceContext = expression.getXPathContext().getNamespaceContext();
        if (namespaceContext != null && context != null && namespaceContext.resolve(parts[0], context) == null) {
            Messages.showMessageDialog(expression.getProject(), "Prefix '" + parts[0] + "' is not defined", "Error", Messages.getErrorIcon());
            b[0] = true;
        }
    }
    return new RefactoringOptions() {

        @Override
        public boolean isCanceled() {
            return b[0];
        }

        @Override
        public String getName() {
            return name;
        }
    };
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) XmlElement(com.intellij.psi.xml.XmlElement)

Example 5 with XmlElement

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

the class DomAnchorImpl method createAnchor.

public static <T extends DomElement> DomAnchor<T> createAnchor(@NotNull T t, boolean usePsi) {
    DomInvocationHandler handler = DomManagerImpl.getNotNullHandler(t);
    if (handler.getStub() != null) {
        return new StubAnchor<>(handler);
    }
    if (usePsi) {
        final XmlElement element = t.getXmlElement();
        if (element != null) {
            return new PsiBasedDomAnchor<>(PsiAnchor.create(element), element.getProject());
        }
    }
    final DomElement parent = t.getParent();
    if (parent == null) {
        LOG.error("Parent null: " + t);
    }
    if (parent instanceof DomFileElementImpl) {
        final DomFileElementImpl fileElement = (DomFileElementImpl) parent;
        //noinspection unchecked
        return new RootAnchor<>(fileElement.getFile(), fileElement.getRootElementClass());
    }
    final DomAnchor<DomElement> parentAnchor = createAnchor(parent);
    final String name = t.getGenericInfo().getElementName(t);
    final AbstractDomChildrenDescription description = t.getChildDescription();
    final List<? extends DomElement> values = description.getValues(parent);
    if (name != null) {
        int i = 0;
        for (DomElement value : values) {
            if (value.equals(t)) {
                return new NamedAnchor<>(parentAnchor, description, name, i);
            }
            if (name.equals(value.getGenericInfo().getElementName(value))) {
                i++;
            }
        }
    }
    final int index = values.indexOf(t);
    if (index < 0) {
        diagnoseNegativeIndex2(t, parent, description, values);
    }
    return new IndexedAnchor<>(parentAnchor, description, index);
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) AbstractDomChildrenDescription(com.intellij.util.xml.reflect.AbstractDomChildrenDescription)

Aggregations

XmlElement (com.intellij.psi.xml.XmlElement)69 PsiElement (com.intellij.psi.PsiElement)16 NotNull (org.jetbrains.annotations.NotNull)16 Nullable (org.jetbrains.annotations.Nullable)11 XmlTag (com.intellij.psi.xml.XmlTag)10 PsiFile (com.intellij.psi.PsiFile)7 PsiReference (com.intellij.psi.PsiReference)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)6 XmlFile (com.intellij.psi.xml.XmlFile)6 Manifest (org.jetbrains.android.dom.manifest.Manifest)5 Module (com.intellij.openapi.module.Module)4 Project (com.intellij.openapi.project.Project)4 DomElement (com.intellij.util.xml.DomElement)4 ArrayList (java.util.ArrayList)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 CompositePsiElement (com.intellij.psi.impl.source.tree.CompositePsiElement)3 SmartList (com.intellij.util.SmartList)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 ResourceType (com.android.resources.ResourceType)2 Result (com.intellij.openapi.application.Result)2