Search in sources :

Example 66 with SelectionModel

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

the class SelectedTextFormatter method getSelectedRange.

@NotNull
TextRange getSelectedRange() {
    SelectionModel model = myEditor.getSelectionModel();
    int start = model.getSelectionStart();
    int end = model.getSelectionEnd();
    return TextRange.create(start, end);
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) NotNull(org.jetbrains.annotations.NotNull)

Example 67 with SelectionModel

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

the class PyStatementMover method restoreCaretAndSelection.

private static void restoreCaretAndSelection(@NotNull final PsiFile file, @NotNull final Editor editor, boolean selectionStartAtCaret, boolean hasSelection, @NotNull final SelectionContainer selectionContainer, int shift, int offset, @NotNull final MyLineRange toMove) {
    final Document document = editor.getDocument();
    final SelectionModel selectionModel = editor.getSelectionModel();
    final CaretModel caretModel = editor.getCaretModel();
    Integer selectionLen = selectionContainer.myLen;
    final PsiElement at = file.findElementAt(offset);
    if (at != null) {
        final PsiElement added = getCommentOrStatement(document, at);
        int size = toMove.size;
        if (size > 1) {
            PsiElement tmp = added.getNextSibling();
            while (size > 1 && tmp != null) {
                if (tmp instanceof PsiWhiteSpace) {
                    if (!selectionStartAtCaret)
                        shift += tmp.getTextLength();
                    selectionLen += tmp.getTextLength();
                }
                tmp = tmp.getNextSibling();
                size -= 1;
            }
        }
        if (shift < 0)
            shift = 0;
        final int column = editor.offsetToLogicalPosition(added.getTextRange().getStartOffset()).column;
        if (selectionContainer.myAtTheBeginning || column < selectionContainer.myAdditional) {
            selectionLen += column;
        } else {
            selectionLen += selectionContainer.myAdditional;
        }
        if (selectionContainer.myAtTheBeginning && selectionStartAtCaret)
            shift = -column;
    }
    final int documentLength = document.getTextLength();
    int newCaretOffset = offset + shift;
    if (newCaretOffset >= documentLength)
        newCaretOffset = documentLength;
    caretModel.moveToOffset(newCaretOffset);
    if (hasSelection) {
        if (selectionStartAtCaret) {
            int newSelectionEnd = newCaretOffset + selectionLen;
            selectionModel.setSelection(newCaretOffset, newSelectionEnd);
        } else {
            int newSelectionStart = newCaretOffset - selectionLen;
            selectionModel.setSelection(newSelectionStart, newCaretOffset);
        }
    }
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) Document(com.intellij.openapi.editor.Document)

Example 68 with SelectionModel

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

the class PyMoveSymbolDelegate method collectAllMovableElementsInSelection.

@NotNull
private static List<PyElement> collectAllMovableElementsInSelection(@NotNull Editor editor, @NotNull PyFile pyFile) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    final TextRange selectionRange = new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
    final List<PyElement> members = PyMoveModuleMembersHelper.getTopLevelModuleMembers(pyFile);
    return ContainerUtil.filter(members, member -> {
        final PsiElement body = PyMoveModuleMembersHelper.expandNamedElementBody((PsiNamedElement) member);
        return body != null && selectionRange.contains(body.getTextRange());
    });
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 69 with SelectionModel

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

the class PyMoveSymbolDelegate method selectionSpansMultipleLines.

private static boolean selectionSpansMultipleLines(@NotNull Editor editor) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    final Document document = editor.getDocument();
    return document.getLineNumber(selectionModel.getSelectionStart()) != document.getLineNumber(selectionModel.getSelectionEnd());
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) Document(com.intellij.openapi.editor.Document)

Example 70 with SelectionModel

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

the class PyUnwrapDescriptor method findTargetElement.

@Override
@Nullable
protected PsiElement findTargetElement(Editor editor, PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    PsiElement endElement = PyUtil.findElementAtOffset(file, offset);
    SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection() && selectionModel.getSelectionStart() < offset) {
        PsiElement startElement = PyUtil.findElementAtOffset(file, selectionModel.getSelectionStart());
        if (startElement != null && startElement != endElement && startElement.getTextRange().getEndOffset() == offset) {
            return startElement;
        }
    }
    return endElement;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

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