Search in sources :

Example 1 with XPathVariable

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

the class XsltVariableContext method resolveInner.

@Nullable
private XPathVariable resolveInner(XPathVariableReference reference) {
    final XmlTag context = getContextTagImpl(reference);
    final VariableResolveProcessor processor = new VariableResolveProcessor(reference.getReferencedName(), context);
    final XPathVariable variable = (XPathVariable) ResolveUtil.treeWalkUp(processor, context);
    if (variable != null) {
        return variable;
    }
    if (!processForwardGlobals(context, processor)) {
        final XmlFile file = PsiTreeUtil.getParentOfType(context, XmlFile.class, true);
        if (file != null) {
            XsltIncludeIndex.processBackwardDependencies(file, xmlFile -> {
                processor.processExternalFile(xmlFile, context);
                return processor.shouldContinue();
            });
        }
    }
    return (XPathVariable) processor.getResult();
}
Also used : XPathVariable(org.intellij.lang.xpath.psi.XPathVariable) XmlFile(com.intellij.psi.xml.XmlFile) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with XPathVariable

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

the class XsltVariableContext method isReferenceTo.

public boolean isReferenceTo(PsiElement element, XPathVariableReference reference) {
    if (element instanceof XsltParameter) {
        final XsltTemplate template = XsltCodeInsightUtil.getTemplate(element, false);
        if (template == null || template.getMatchExpression() == null)
            return false;
        final XPathVariable t = reference.resolve();
        final PsiReference[] references = element.getReferences();
        for (PsiReference r : references) {
            if (r.isReferenceTo(t))
                return true;
        }
    }
    return false;
}
Also used : XPathVariable(org.intellij.lang.xpath.psi.XPathVariable) PsiReference(com.intellij.psi.PsiReference) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) XsltTemplate(org.intellij.lang.xpath.xslt.psi.XsltTemplate)

Example 3 with XPathVariable

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

the class XsltRefactoringTest method doInlineVariable.

private void doInlineVariable() throws Throwable {
    myFixture.configureByFile(getTestFileName() + ".xsl");
    final XPathVariable variable = findVariable();
    assertNotNull(variable);
    VariableInlineHandler.invoke(variable, myFixture.getEditor());
    myFixture.checkResultByFile(getTestFileName() + "_after.xsl");
}
Also used : XPathVariable(org.intellij.lang.xpath.psi.XPathVariable)

Example 4 with XPathVariable

use of org.intellij.lang.xpath.psi.XPathVariable 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 5 with XPathVariable

use of org.intellij.lang.xpath.psi.XPathVariable 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

XPathVariable (org.intellij.lang.xpath.psi.XPathVariable)5 PsiReference (com.intellij.psi.PsiReference)2 XPathVariableReference (org.intellij.lang.xpath.psi.XPathVariableReference)2 Nullable (org.jetbrains.annotations.Nullable)2 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 PsiElement (com.intellij.psi.PsiElement)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 LinkedHashSet (java.util.LinkedHashSet)1 XsltParameter (org.intellij.lang.xpath.xslt.psi.XsltParameter)1 XsltTemplate (org.intellij.lang.xpath.xslt.psi.XsltTemplate)1 XsltVariable (org.intellij.lang.xpath.xslt.psi.XsltVariable)1 NotNull (org.jetbrains.annotations.NotNull)1