Search in sources :

Example 1 with XPathFile

use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.

the class BaseIntroduceAction method actionPerformedImpl.

protected boolean actionPerformedImpl(PsiFile file, Editor editor, XmlAttribute context, int offset) {
    if (!(file instanceof XPathFile))
        return false;
    // pattern attribute may not reference variables
    if (XsltSupport.isPatternAttribute(context))
        return false;
    final SelectionModel selectionModel = editor.getSelectionModel();
    final boolean hasSelection = selectionModel.hasSelection();
    final int start = selectionModel.getSelectionStart();
    final int end = selectionModel.getSelectionEnd();
    if (hasSelection) {
        final PsiElement xpathElement = file.findElementAt(start);
        if (xpathElement != null) {
            XPathExpression expression = PsiTreeUtil.getParentOfType(xpathElement, XPathExpression.class);
            while (expression != null) {
                if (expression.getTextRange().getStartOffset() == start) {
                    final int diff = expression.getTextRange().getEndOffset() - end;
                    if (diff == 0) {
                        extractFromExpression(editor, expression);
                        return true;
                    } else if (diff > 0) {
                        break;
                    }
                }
                expression = PsiTreeUtil.getParentOfType(expression, XPathExpression.class);
            }
        }
    } else {
        final XPathExpression expression = PsiTreeUtil.getChildOfType(file, XPathExpression.class);
        if (expression != null) {
            final PsiFile containingFile = expression.getContainingFile();
            assert containingFile != null;
            final TextRange range = expression.getTextRange();
            editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
            extractFromExpression(editor, expression);
            return true;
        }
    }
    return false;
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) XPathFile(org.intellij.lang.xpath.XPathFile) SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 2 with XPathFile

use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.

the class CompletionLists method addNameCompletions.

private static void addNameCompletions(ContextProvider contextProvider, final XPathNodeTest element, final Set<Lookup> list) {
    final PrefixedName prefixedName = element.getQName();
    final XPathNodeTest.PrincipalType principalType = element.getPrincipalType();
    final Set<PsiFile> files = new HashSet<>();
    final XPathFile file = (XPathFile) element.getContainingFile();
    files.add(file);
    ContainerUtil.addAll(files, contextProvider.getRelatedFiles(file));
    for (PsiFile xpathFile : files) {
        xpathFile.accept(new PsiRecursiveElementVisitor() {

            public void visitElement(PsiElement e) {
                if (e instanceof XPathNodeTest) {
                    final XPathNodeTest nodeTest = (XPathNodeTest) e;
                    final XPathNodeTest.PrincipalType _principalType = nodeTest.getPrincipalType();
                    if (_principalType == principalType) {
                        final PrefixedName _prefixedName = nodeTest.getQName();
                        if (_prefixedName != null && prefixedName != null) {
                            final String localName = _prefixedName.getLocalName();
                            if (!"*".equals(localName) && !localName.contains(INTELLIJ_IDEA_RULEZ)) {
                                if (Comparing.equal(_prefixedName.getPrefix(), prefixedName.getPrefix())) {
                                    list.add(new NodeLookup(localName, _principalType));
                                } else if (prefixedName.getPrefix() == null) {
                                    list.add(new NodeLookup(_prefixedName.toString(), _principalType));
                                }
                            }
                        }
                    }
                }
                super.visitElement(e);
            }
        });
    }
}
Also used : XPathFile(org.intellij.lang.xpath.XPathFile)

Example 3 with XPathFile

use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.

the class XPathChangeUtil method createExpression.

@NotNull
public static XPathExpression createExpression(PsiElement context, String text) {
    final XPathFile file = createXPathFile(context, text);
    final XPathExpression child = PsiTreeUtil.getChildOfType(file, XPathExpression.class);
    assert child != null;
    return child;
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) XPathFile(org.intellij.lang.xpath.XPathFile) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with XPathFile

use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.

the class XsltXmlAnnotator method visitXmlAttributeValue.

@Override
public void visitXmlAttributeValue(final XmlAttributeValue value) {
    final PsiElement parent = value.getParent();
    if (parent instanceof XmlAttribute) {
        if (!XsltSupport.isXsltFile(parent.getContainingFile())) {
            return;
        }
        final String s = value.getValue();
        if (s == null || s.length() == 0) {
            if (XsltSupport.isXPathAttribute((XmlAttribute) parent)) {
                InjectedLanguageUtil.enumerate(value, new PsiLanguageInjectionHost.InjectedPsiVisitor() {

                    @Override
                    public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
                        if (injectedPsi instanceof XPathFile) {
                            if (injectedPsi.getTextLength() == 0) {
                                myHolder.createErrorAnnotation(value, "Empty XPath expression");
                            }
                        }
                    }
                });
            }
        } else if (XsltSupport.mayBeAVT((XmlAttribute) parent)) {
            final List<Integer> singleBraces = collectClosingBraceOffsets(s);
            if (singleBraces != null) {
                boolean enumerated = InjectedLanguageUtil.enumerate(value, new PsiLanguageInjectionHost.InjectedPsiVisitor() {

                    @Override
                    public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
                        if (injectedPsi instanceof XPathFile) {
                            for (PsiLanguageInjectionHost.Shred place : places) {
                                final TextRange range = place.getRangeInsideHost();
                                for (Iterator<Integer> iterator = singleBraces.iterator(); iterator.hasNext(); ) {
                                    final Integer brace = iterator.next();
                                    if (range.contains(brace)) {
                                        iterator.remove();
                                    }
                                }
                            }
                        }
                    }
                });
                if (enumerated) {
                    for (Integer brace : singleBraces) {
                        myHolder.createErrorAnnotation(TextRange.from(value.getTextOffset() + brace, 1), "Invalid single closing brace. Escape as '}}'");
                    }
                }
            }
        }
    }
    super.visitXmlAttributeValue(value);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) XPathFile(org.intellij.lang.xpath.XPathFile) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) SmartList(com.intellij.util.SmartList) PsiElement(com.intellij.psi.PsiElement)

Example 5 with XPathFile

use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.

the class CompletionLists method getFunctionCompletions.

public static Collection<Lookup> getFunctionCompletions(XPathElement element) {
    final XPathFile xpathFile = (XPathFile) element.getContainingFile();
    final ContextProvider contextProvider = ContextProvider.getContextProvider(xpathFile);
    final NamespaceContext nsContext = contextProvider.getNamespaceContext();
    String uri;
    PrefixedName qn = null;
    if (element instanceof QNameElement) {
        qn = ((QNameElement) element).getQName();
        if (qn != null) {
            QName qName = contextProvider.getQName(qn, element);
            if (qn.getPrefix() != null) {
                if (qName != null) {
                    uri = qName.getNamespaceURI();
                } else {
                    return Collections.emptySet();
                }
            } else {
                uri = null;
            }
        } else {
            uri = null;
        }
    } else {
        uri = null;
    }
    final Map<Pair<QName, Integer>, ? extends Function> functions = contextProvider.getFunctionContext().getFunctions();
    final List<Lookup> lookups = new ArrayList<>(functions.size());
    for (Map.Entry<Pair<QName, Integer>, ? extends Function> entry : functions.entrySet()) {
        final Function functionDecl = entry.getValue();
        final QName f = entry.getKey().first;
        final String p;
        if (nsContext != null) {
            String namespaceURI = f.getNamespaceURI();
            if (uri != null && !namespaceURI.equals(uri)) {
                continue;
            }
            final String prefixForURI = nsContext.getPrefixForURI(namespaceURI, PsiTreeUtil.getContextOfType(element, XmlElement.class, true));
            if (prefixForURI == null && namespaceURI.length() > 0) {
                continue;
            }
            p = qn == null || qn.getPrefix() == null ? makePrefix(prefixForURI) : "";
        } else {
            p = "";
        }
        lookups.add(FunctionLookup.newFunctionLookup(p + f.getLocalPart(), functionDecl));
    }
    return lookups;
}
Also used : QName(javax.xml.namespace.QName) ContextProvider(org.intellij.lang.xpath.context.ContextProvider) Function(org.intellij.lang.xpath.context.functions.Function) XPathFile(org.intellij.lang.xpath.XPathFile) NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) XmlElement(com.intellij.psi.xml.XmlElement) Pair(com.intellij.openapi.util.Pair)

Aggregations

XPathFile (org.intellij.lang.xpath.XPathFile)6 NotNull (org.jetbrains.annotations.NotNull)3 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 XPathExpression (org.intellij.lang.xpath.psi.XPathExpression)2 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 Pair (com.intellij.openapi.util.Pair)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlElement (com.intellij.psi.xml.XmlElement)1 SmartList (com.intellij.util.SmartList)1 List (java.util.List)1 QName (javax.xml.namespace.QName)1 ContextProvider (org.intellij.lang.xpath.context.ContextProvider)1 NamespaceContext (org.intellij.lang.xpath.context.NamespaceContext)1 Function (org.intellij.lang.xpath.context.functions.Function)1