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();
}
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;
}
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");
}
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;
}
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;
}
Aggregations