Search in sources :

Example 1 with XPathVariableReference

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

the class XsltRefactoringTest method findVariable.

@Nullable
private XPathVariable findVariable() {
    final PsiElement elementAtCaret = myFixture.getElementAtCaret();
    XPathVariableReference ref = PsiTreeUtil.getParentOfType(elementAtCaret, XPathVariableReference.class, false);
    if (ref != null) {
        return ref.resolve();
    }
    XPathVariable variable = PsiTreeUtil.getParentOfType(elementAtCaret, XPathVariable.class, false);
    if (variable != null) {
        return variable;
    }
    final PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference reference : references) {
        final PsiElement var = reference.resolve();
        if (var instanceof XPathVariable) {
            return (XPathVariable) var;
        }
    }
    return null;
}
Also used : XPathVariable(org.intellij.lang.xpath.psi.XPathVariable) XPathVariableReference(org.intellij.lang.xpath.psi.XPathVariableReference) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with XPathVariableReference

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

the class XsltIntroduceVariableAction method extractImpl.

protected boolean extractImpl(XPathExpression expression, Set<XPathExpression> matchingExpressions, List<XmlTag> otherMatches, IntroduceVariableOptions dlg) {
    final XmlAttribute attribute = PsiTreeUtil.getContextOfType(expression, XmlAttribute.class, true);
    assert attribute != null;
    try {
        final String name = dlg.getName();
        final XmlTag insertionPoint = XsltCodeInsightUtil.findVariableInsertionPoint(attribute.getParent(), XsltCodeInsightUtil.getUsageBlock(expression), name, dlg.isReplaceAll() ? otherMatches.toArray(new XmlTag[otherMatches.size()]) : XmlTag.EMPTY);
        final XmlTag parentTag = insertionPoint.getParentTag();
        assert parentTag != null : "Could not locate position to create variable at";
        final XmlTag xmlTag = parentTag.createChildTag("variable", XsltSupport.XSLT_NS, null, false);
        xmlTag.setAttribute("name", name);
        xmlTag.setAttribute("select", expression.getText());
        // TODO: revisit the formatting
        final PsiElement element = parentTag.addBefore(xmlTag, insertionPoint);
        final ASTNode node1 = parentTag.getNode();
        assert node1 != null;
        final ASTNode node2 = element.getNode();
        assert node2 != null;
        CodeStyleManager.getInstance(xmlTag.getManager().getProject()).reformatNewlyAddedElement(node1, node2);
        final XPathVariableReference var = XPathChangeUtil.createVariableReference(expression, name);
        expression.replace(var);
        if (dlg.isReplaceAll()) {
            for (XPathExpression expr : matchingExpressions) {
                expr.replace(XPathChangeUtil.createVariableReference(expr, name));
            }
            return false;
        } else {
            return true;
        }
    } catch (IncorrectOperationException e) {
        Logger.getInstance(getClass().getName()).error(e);
        return false;
    }
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ASTNode(com.intellij.lang.ASTNode) XPathVariableReference(org.intellij.lang.xpath.psi.XPathVariableReference) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with XPathVariableReference

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

the class XsltVariableImpl method getType.

@NotNull
public XPathType getType() {
    final XPathType declaredType = XsltCodeInsightUtil.getDeclaredType(getTag());
    if (declaredType != null) {
        return declaredType;
    }
    final XmlAttribute attr = getTag().getAttribute("type", XsltSupport.PLUGIN_EXTENSIONS_NS);
    if (attr != null) {
        return XPathType.fromString(attr.getValue());
    }
    final XPathExpression value = getValue();
    if (value instanceof XPathVariableReference) {
        // recursive reference <xsl:variable name="foo" select="$foo" />
        final XPathVariableReference reference = (XPathVariableReference) value;
        if (reference.resolve() == this) {
            return XPathType.UNKNOWN;
        }
    }
    return value != null ? value.getType() : XPathType.UNKNOWN;
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) XPathType(org.intellij.lang.xpath.psi.XPathType) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XPathVariableReference(org.intellij.lang.xpath.psi.XPathVariableReference) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with XPathVariableReference

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

the class IntroduceParameterProcessor method performRefactoring.

protected void performRefactoring(@NotNull UsageInfo[] usageInfos) {
    XmlTag tag;
    if (myTemplate != null) {
        tag = myTemplate.getTag();
    } else if ((tag = XsltCodeInsightUtil.getTemplateTag(myExpression, true, false)) == null) {
        final XmlDocument document = PsiTreeUtil.getContextOfType(myExpression, XmlDocument.class, true);
        assert document != null;
        tag = document.getRootTag();
    }
    assert tag != null;
    final XmlTag param = tag.createChildTag("param", XsltSupport.XSLT_NS, null, false);
    try {
        param.setAttribute("name", mySettings.getName());
        if (mySettings.isCreateDefault()) {
            param.setAttribute("select", myExpression.getText());
        }
        XmlTag anchorParam = null;
        for (UsageInfo info : usageInfos) {
            if (info instanceof XPathUsageInfo) {
                final XPathUsageInfo x = (XPathUsageInfo) info;
                final XPathVariableReference variableReference = XPathChangeUtil.createVariableReference(x.getExpression(), mySettings.getName());
                final XmlAttribute attribute = x.getAttribute();
                assert attribute != null;
                x.getExpression().replace(variableReference);
                if (XsltSupport.isParam(attribute.getParent())) {
                    if (anchorParam == null) {
                        anchorParam = attribute.getParent();
                    } else if (attribute.getParent().getTextOffset() < anchorParam.getTextOffset()) {
                        anchorParam = attribute.getParent();
                    }
                }
            } else {
                final XmlTag t = (XmlTag) info.getElement();
                if (t != null) {
                    final XmlTag p = t.createChildTag("with-param", t.getNamespace(), null, false);
                    p.setAttribute("name", mySettings.getName());
                    p.setAttribute("select", myExpression.getText());
                    t.add(p);
                }
            }
        }
        if (anchorParam != null) {
            RefactoringUtil.addParameter(tag, param, PsiTreeUtil.getPrevSiblingOfType(anchorParam, XmlTag.class));
        } else {
            RefactoringUtil.addParameter(tag, param);
        }
    } catch (IncorrectOperationException e) {
        Logger.getInstance(getClass().getName()).error(e);
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XPathVariableReference(org.intellij.lang.xpath.psi.XPathVariableReference) XmlDocument(com.intellij.psi.xml.XmlDocument) IncorrectOperationException(com.intellij.util.IncorrectOperationException) UsageInfo(com.intellij.usageView.UsageInfo) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with XPathVariableReference

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

the class XsltExtractTemplateAction method extractFrom.

private boolean extractFrom(@NotNull final PsiElement start, final PsiElement end, String newName) {
    final XmlTag outerTemplate = XsltCodeInsightUtil.getTemplateTag(start, false);
    if (outerTemplate == null) {
        return false;
    }
    final XmlTag parentScope = PsiTreeUtil.getParentOfType(start, XmlTag.class, true);
    if (parentScope == null) {
        return false;
    }
    final StringBuilder sb = new StringBuilder("\n");
    final Set<String> vars = new LinkedHashSet<>();
    final int startOffset = start.getTextRange().getStartOffset();
    final int endOffset = end.getTextRange().getEndOffset();
    PsiElement e = start;
    while (e != null) {
        if (e instanceof XmlTag) {
            final XmlTag tag = (XmlTag) e;
            if (XsltSupport.isVariable(tag)) {
                final XsltVariable variable = XsltElementFactory.getInstance().wrapElement(tag, XsltVariable.class);
                final LocalSearchScope searchScope = new LocalSearchScope(parentScope);
                final Query<PsiReference> query = ReferencesSearch.search(variable, searchScope);
                for (PsiReference reference : query) {
                    final XmlElement context = PsiTreeUtil.getContextOfType(reference.getElement(), XmlElement.class, true);
                    if (context == null || context.getTextRange().getStartOffset() > endOffset) {
                        return false;
                    }
                }
            }
        }
        sb.append(e.getText());
        final List<XPathVariableReference> references = RefactoringUtil.collectVariableReferences(e);
        for (XPathVariableReference reference : references) {
            final XPathVariable variable = reference.resolve();
            if (variable instanceof XsltVariable) {
                final XmlTag var = ((XsltVariable) variable).getTag();
                if (var.getTextRange().getStartOffset() < startOffset) {
                    // don't pass through global parameters and variables
                    if (XsltCodeInsightUtil.getTemplateTag(variable, false) != null) {
                        vars.add(variable.getName());
                    }
                }
            } else if (variable == null) {
                // TODO just copy unresolved refs or fail?
                vars.add(reference.getReferencedName());
            }
        }
        if (e == end) {
            break;
        }
        e = e.getNextSibling();
    }
    sb.append("\n");
    final String s = newName == null ? Messages.showInputDialog(start.getProject(), "Template Name: ", getRefactoringName(), Messages.getQuestionIcon()) : newName;
    if (s != null) {
        new WriteCommandAction(start.getProject()) {

            protected void run(@NotNull Result result) throws Throwable {
                final PsiFile containingFile = start.getContainingFile();
                XmlTag templateTag = parentScope.createChildTag("template", XsltSupport.XSLT_NS, sb.toString(), false);
                templateTag.setAttribute("name", s);
                final PsiElement dummy = XmlElementFactory.getInstance(start.getProject()).createDisplayText(" ");
                final PsiElement outerParent = outerTemplate.getParent();
                final PsiElement element = outerParent.addAfter(dummy, outerTemplate);
                templateTag = (XmlTag) outerParent.addAfter(templateTag, element);
                final TextRange adjust = templateTag.getTextRange();
                final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(start.getProject());
                final Document doc = psiDocumentManager.getDocument(containingFile);
                assert doc != null;
                psiDocumentManager.doPostponedOperationsAndUnblockDocument(doc);
                CodeStyleManager.getInstance(start.getManager().getProject()).adjustLineIndent(containingFile, adjust);
                final PsiElement parent = start.getParent();
                XmlTag callTag = parentScope.createChildTag("call-template", XsltSupport.XSLT_NS, null, false);
                callTag.setAttribute("name", s);
                if (start instanceof XmlToken && ((XmlToken) start).getTokenType() == XmlTokenType.XML_DATA_CHARACTERS) {
                    assert start == end;
                    callTag = (XmlTag) start.replace(callTag);
                } else {
                    callTag = (XmlTag) parent.addBefore(callTag, start);
                    parent.deleteChildRange(start, end);
                }
                for (String var : vars) {
                    final XmlTag param = templateTag.createChildTag("param", XsltSupport.XSLT_NS, null, false);
                    param.setAttribute("name", var);
                    RefactoringUtil.addParameter(templateTag, param);
                    final XmlTag arg = RefactoringUtil.addWithParam(callTag);
                    arg.setAttribute("name", var);
                    arg.setAttribute("select", "$" + var);
                }
            }
        }.execute().logException(Logger.getInstance(getClass().getName()));
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XPathVariableReference(org.intellij.lang.xpath.psi.XPathVariableReference) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) XPathVariable(org.intellij.lang.xpath.psi.XPathVariable) XsltVariable(org.intellij.lang.xpath.xslt.psi.XsltVariable)

Aggregations

XPathVariableReference (org.intellij.lang.xpath.psi.XPathVariableReference)5 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 PsiElement (com.intellij.psi.PsiElement)2 XmlTag (com.intellij.psi.xml.XmlTag)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 XPathExpression (org.intellij.lang.xpath.psi.XPathExpression)2 XPathVariable (org.intellij.lang.xpath.psi.XPathVariable)2 NotNull (org.jetbrains.annotations.NotNull)2 ASTNode (com.intellij.lang.ASTNode)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiReference (com.intellij.psi.PsiReference)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 XmlDocument (com.intellij.psi.xml.XmlDocument)1 UsageInfo (com.intellij.usageView.UsageInfo)1 LinkedHashSet (java.util.LinkedHashSet)1 XPathType (org.intellij.lang.xpath.psi.XPathType)1 XsltVariable (org.intellij.lang.xpath.xslt.psi.XsltVariable)1