Search in sources :

Example 1 with XPathExpression

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;
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with XPathExpression

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;
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) XPathFile(org.intellij.lang.xpath.XPathFile) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with XPathExpression

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;
    }
}
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 4 with XPathExpression

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);
    }
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) EditorWindow(com.intellij.injected.editor.EditorWindow) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with XPathExpression

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;
}
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)

Aggregations

XPathExpression (org.intellij.lang.xpath.psi.XPathExpression)13 PsiElement (com.intellij.psi.PsiElement)4 NotNull (org.jetbrains.annotations.NotNull)4 XmlTag (com.intellij.psi.xml.XmlTag)3 XPathBinaryExpression (org.intellij.lang.xpath.psi.XPathBinaryExpression)3 EditorWindow (com.intellij.injected.editor.EditorWindow)2 Editor (com.intellij.openapi.editor.Editor)2 SelectionModel (com.intellij.openapi.editor.SelectionModel)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiFile (com.intellij.psi.PsiFile)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XPathFile (org.intellij.lang.xpath.XPathFile)2 XPathType (org.intellij.lang.xpath.psi.XPathType)2 XPathVariableReference (org.intellij.lang.xpath.psi.XPathVariableReference)2 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 ASTNode (com.intellij.lang.ASTNode)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)1 PsiReference (com.intellij.psi.PsiReference)1