use of org.intellij.lang.xpath.psi.XPathExpression in project intellij-community by JetBrains.
the class XPath2IfImpl method getType.
@NotNull
public XPathType getType() {
final XPathExpression then = getThenBranch();
final XPathExpression value = then != null ? then : getElseBranch();
return value != null ? value.getType() : XPathType.UNKNOWN;
}
use of org.intellij.lang.xpath.psi.XPathExpression 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;
}
use of org.intellij.lang.xpath.psi.XPathExpression 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;
}
}
use of org.intellij.lang.xpath.psi.XPathExpression in project intellij-community by JetBrains.
the class BaseIntroduceAction method extractFromExpression.
private void extractFromExpression(Editor e, final XPathExpression expression) {
final Editor editor = (e instanceof EditorWindow) ? ((EditorWindow) e).getDelegate() : e;
final HighlightManager highlightManager = HighlightManager.getInstance(expression.getProject());
final Set<XPathExpression> matchingExpressions = RefactoringUtil.collectMatchingExpressions(expression);
final List<XmlTag> otherMatches = new ArrayList<>(matchingExpressions.size());
final ArrayList<RangeHighlighter> highlighters = new ArrayList<>(matchingExpressions.size() + 1);
if (matchingExpressions.size() > 0) {
final SelectionModel selectionModel = editor.getSelectionModel();
highlightManager.addRangeHighlight(editor, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
for (XPathExpression expr : matchingExpressions) {
final TextRange range = XsltCodeInsightUtil.getRangeInsideHostingFile(expr);
highlightManager.addRangeHighlight(editor, range.getStartOffset(), range.getEndOffset(), EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
final XmlTag tag = PsiTreeUtil.getContextOfType(expr, XmlTag.class, true);
assert tag != null;
otherMatches.add(tag);
}
}
final Settings dlg = getSettings(expression, matchingExpressions);
if (dlg == null || dlg.isCanceled())
return;
if (getCommandName() != null) {
new WriteCommandAction.Simple(e.getProject(), getCommandName()) {
protected void run() throws Throwable {
if (extractImpl(expression, matchingExpressions, otherMatches, dlg)) {
for (RangeHighlighter highlighter : highlighters) {
highlighter.dispose();
}
}
}
}.execute();
} else {
extractImpl(expression, matchingExpressions, otherMatches, dlg);
}
}
use of org.intellij.lang.xpath.psi.XPathExpression 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;
}
Aggregations