Search in sources :

Example 11 with XmlElement

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

the class XmlElementImpl method getNameFromEntityRef.

@Nullable
protected static String getNameFromEntityRef(final CompositeElement compositeElement, final IElementType xmlEntityDeclStart) {
    final ASTNode node = compositeElement.findChildByType(xmlEntityDeclStart);
    if (node == null)
        return null;
    ASTNode name = node.getTreeNext();
    if (name != null && name.getElementType() == TokenType.WHITE_SPACE) {
        name = name.getTreeNext();
    }
    if (name != null && name.getElementType() == XmlElementType.XML_ENTITY_REF) {
        final StringBuilder builder = new StringBuilder();
        ((XmlElement) name.getPsi()).processElements(new PsiElementProcessor() {

            @Override
            public boolean execute(@NotNull final PsiElement element) {
                builder.append(element.getText());
                return true;
            }
        }, name.getPsi());
        if (builder.length() > 0)
            return builder.toString();
    }
    return null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) XmlElement(com.intellij.psi.xml.XmlElement) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiElement(com.intellij.psi.PsiElement) CompositePsiElement(com.intellij.psi.impl.source.tree.CompositePsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with XmlElement

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

the class CompletionLists method getNodeTestCompletions.

public static Collection<Lookup> getNodeTestCompletions(final XPathNodeTest element) {
    if (!element.isNameTest()) {
        return Collections.emptyList();
    }
    final PrefixedName prefixedName = element.getQName();
    assert prefixedName != null;
    final String canonicalText = prefixedName.toString();
    final String suffix = canonicalText.substring(canonicalText.indexOf(INTELLIJ_IDEA_RULEZ));
    final XPathAxisSpecifier axisSpecifier = element.getStep().getAxisSpecifier();
    final ContextProvider contextProvider = ContextProvider.getContextProvider(element);
    final XmlElement context = contextProvider.getContextElement();
    final boolean insidePrefix = suffix.contains(INTELLIJ_IDEA_RULEZ + ":");
    final Set<Lookup> list = new HashSet<>();
    addNameCompletions(contextProvider, element, list);
    final String namespacePrefix = prefixedName.getPrefix();
    if (namespacePrefix == null || insidePrefix) {
        addNamespaceCompletions(contextProvider.getNamespaceContext(), list, context);
    }
    final XPathNodeTest.PrincipalType principalType = addContextNames(element, contextProvider, prefixedName, list);
    if (namespacePrefix == null && !insidePrefix) {
        if (axisSpecifier == null || axisSpecifier.isDefaultAxis()) {
            list.addAll(getAxisCompletions());
            // wow, this code sux. find a better implementation
            PsiElement sibling = element.getParent().getPrevSibling();
            while (sibling instanceof PsiWhiteSpace) {
                sibling = sibling.getPrevSibling();
            }
            boolean check = sibling != null;
            if (!check) {
                XPathLocationPath lp = null;
                do {
                    lp = PsiTreeUtil.getParentOfType(lp == null ? element : lp, XPathLocationPath.class, true);
                } while (lp != null && lp.getPrevSibling() == null);
                check = lp == null || (sibling = lp.getPrevSibling()) != null;
            }
            if (check) {
                if (sibling instanceof XPathToken && XPathTokenTypes.PATH_OPS.contains(((XPathToken) sibling).getTokenType())) {
                // xx/yy<caret> : prevSibl = /
                } else {
                    list.addAll(getFunctionCompletions(element));
                    list.addAll(getVariableCompletions(element));
                }
            }
        }
        if (principalType == XPathNodeTest.PrincipalType.ELEMENT && prefixedName.getPrefix() == null) {
            list.addAll(getNodeTypeCompletions(element));
        }
    }
    return list;
}
Also used : ContextProvider(org.intellij.lang.xpath.context.ContextProvider) XmlElement(com.intellij.psi.xml.XmlElement)

Example 13 with XmlElement

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

the class CompletionLists method addContextNames.

private static XPathNodeTest.PrincipalType addContextNames(XPathNodeTest element, ContextProvider contextProvider, PrefixedName prefixedName, Set<Lookup> list) {
    final NamespaceContext namespaceContext = contextProvider.getNamespaceContext();
    final XmlElement context = contextProvider.getContextElement();
    final XPathNodeTest.PrincipalType principalType = element.getPrincipalType();
    if (principalType == XPathNodeTest.PrincipalType.ELEMENT) {
        final Set<QName> elementNames = contextProvider.getElements(false);
        if (elementNames != null) {
            for (QName pair : elementNames) {
                if ("*".equals(pair.getLocalPart()))
                    continue;
                if (namespaceMatches(prefixedName, pair.getNamespaceURI(), namespaceContext, context, true)) {
                    if (prefixedName.getPrefix() == null && namespaceContext != null) {
                        final String p = namespaceContext.getPrefixForURI(pair.getNamespaceURI(), context);
                        list.add(new NodeLookup(makePrefix(p) + pair.getLocalPart(), XPathNodeTest.PrincipalType.ELEMENT));
                    } else {
                        list.add(new NodeLookup(pair.getLocalPart(), XPathNodeTest.PrincipalType.ELEMENT));
                    }
                }
            }
        }
    } else if (principalType == XPathNodeTest.PrincipalType.ATTRIBUTE) {
        final Set<QName> attributeNames = contextProvider.getAttributes(false);
        if (attributeNames != null) {
            for (QName pair : attributeNames) {
                if ("*".equals(pair.getLocalPart()))
                    continue;
                if (namespaceMatches(prefixedName, pair.getNamespaceURI(), namespaceContext, context, false)) {
                    if (prefixedName.getPrefix() == null && namespaceContext != null) {
                        final String p = namespaceContext.getPrefixForURI(pair.getNamespaceURI(), context);
                        list.add(new NodeLookup(makePrefix(p) + pair.getLocalPart(), XPathNodeTest.PrincipalType.ATTRIBUTE));
                    } else {
                        list.add(new NodeLookup(pair.getLocalPart(), XPathNodeTest.PrincipalType.ATTRIBUTE));
                    }
                }
            }
        }
    }
    return principalType;
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) QName(javax.xml.namespace.QName) XmlElement(com.intellij.psi.xml.XmlElement)

Example 14 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 15 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)

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