Search in sources :

Example 1 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel 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 2 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel 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)

Example 3 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class XsltExtractTemplateAction method invokeImpl.

public boolean invokeImpl(Editor editor, PsiFile file, String newName) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection()) {
        return false;
    }
    int startOffset = selectionModel.getSelectionStart();
    PsiElement start = file.findElementAt(startOffset);
    if (start instanceof PsiWhiteSpace) {
        if ((start = PsiTreeUtil.nextLeaf(start)) != null) {
            startOffset = start.getTextOffset();
        }
    }
    int endOffset = selectionModel.getSelectionEnd();
    PsiElement end = file.findElementAt(endOffset - 1);
    if (end instanceof PsiWhiteSpace) {
        if ((end = PsiTreeUtil.prevLeaf(end)) != null) {
            endOffset = end.getTextRange().getEndOffset();
        }
    }
    if (start == null || end == null) {
        return false;
    }
    final PsiElement parent = start.getParent();
    if (!(parent instanceof XmlTag || parent instanceof XmlComment)) {
        return false;
    }
    if (start == end) {
        if (start.getTextRange().getStartOffset() != startOffset) {
            return false;
        }
        if (end.getTextRange().getEndOffset() != endOffset) {
            return false;
        }
        if (extractFrom(start, end, newName)) {
            // tests dislike empty selection
            selectionModel.removeSelection();
            return true;
        }
    } else {
        final XmlElement startTag = PsiTreeUtil.getParentOfType(start, XmlTag.class, XmlComment.class);
        if (startTag == null) {
            return false;
        }
        if (startTag.getTextRange().getStartOffset() != startOffset) {
            return false;
        }
        final XmlElement endTag = PsiTreeUtil.getParentOfType(end, XmlTag.class, XmlComment.class);
        if (endTag == null) {
            return false;
        }
        if (endTag.getTextRange().getEndOffset() != endOffset) {
            return false;
        }
        if (startTag != endTag) {
            if (startTag.getParent() != endTag.getParent()) {
                return false;
            }
        }
        if (extractFrom(startTag, endTag, newName)) {
            // tests dislike empty selection
            selectionModel.removeSelection();
            return true;
        }
    }
    return false;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel)

Example 4 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class PyStatementMover method getSelectionLenContainer.

private static SelectionContainer getSelectionLenContainer(@NotNull final Editor editor, @NotNull final MyLineRange toMove) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    final PsiElement startToMove = toMove.myStartElement;
    final PsiElement endToMove = toMove.myEndElement;
    final int selectionStart = selectionModel.getSelectionStart();
    final int selectionEnd = selectionModel.getSelectionEnd();
    final TextRange range = startToMove.getTextRange();
    final int column = editor.offsetToLogicalPosition(selectionStart).column;
    final int additionalSelection = range.getStartOffset() > selectionStart ? range.getStartOffset() - selectionStart : 0;
    if (startToMove == endToMove)
        return new SelectionContainer(selectionEnd - range.getStartOffset(), additionalSelection, column == 0);
    int len = range.getStartOffset() <= selectionStart ? range.getEndOffset() - selectionStart : startToMove.getTextLength();
    PsiElement tmp = startToMove.getNextSibling();
    while (tmp != endToMove && tmp != null) {
        if (!(tmp instanceof PsiWhiteSpace))
            len += tmp.getTextLength();
        tmp = tmp.getNextSibling();
    }
    len = len + selectionEnd - endToMove.getTextOffset();
    return new SelectionContainer(len, additionalSelection, column == 0);
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange)

Example 5 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class PyStatementMover method checkAvailable.

@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
    if (!(file instanceof PyFile))
        return false;
    final int offset = editor.getCaretModel().getOffset();
    final SelectionModel selectionModel = editor.getSelectionModel();
    final Document document = editor.getDocument();
    final int lineNumber = document.getLineNumber(offset);
    int start = getLineStartSafeOffset(document, lineNumber);
    final int lineEndOffset = document.getLineEndOffset(lineNumber);
    int end = lineEndOffset == 0 ? 0 : lineEndOffset - 1;
    if (selectionModel.hasSelection()) {
        start = selectionModel.getSelectionStart();
        final int selectionEnd = selectionModel.getSelectionEnd();
        end = selectionEnd == 0 ? 0 : selectionEnd - 1;
    }
    PsiElement elementToMove1 = PyUtil.findNonWhitespaceAtOffset(file, start);
    PsiElement elementToMove2 = PyUtil.findNonWhitespaceAtOffset(file, end);
    if (elementToMove1 == null || elementToMove2 == null)
        return false;
    if (ifInsideString(document, lineNumber, elementToMove1, down))
        return false;
    elementToMove1 = getCommentOrStatement(document, elementToMove1);
    elementToMove2 = getCommentOrStatement(document, elementToMove2);
    if (PsiTreeUtil.isAncestor(elementToMove1, elementToMove2, false)) {
        elementToMove2 = elementToMove1;
    } else if (PsiTreeUtil.isAncestor(elementToMove2, elementToMove1, false)) {
        elementToMove1 = elementToMove2;
    }
    info.toMove = new MyLineRange(elementToMove1, elementToMove2);
    info.toMove2 = getDestinationScope(file, editor, down ? elementToMove2 : elementToMove1, down);
    info.indentTarget = false;
    info.indentSource = false;
    return true;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) Document(com.intellij.openapi.editor.Document)

Aggregations

SelectionModel (com.intellij.openapi.editor.SelectionModel)76 TextRange (com.intellij.openapi.util.TextRange)21 Document (com.intellij.openapi.editor.Document)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)16 Editor (com.intellij.openapi.editor.Editor)14 Nullable (org.jetbrains.annotations.Nullable)11 CaretModel (com.intellij.openapi.editor.CaretModel)10 PsiFile (com.intellij.psi.PsiFile)8 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)3 SurroundDescriptor (com.intellij.lang.surroundWith.SurroundDescriptor)3 Pass (com.intellij.openapi.util.Pass)3 List (java.util.List)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 EditorWindow (com.intellij.injected.editor.EditorWindow)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2