Search in sources :

Example 36 with SelectionModel

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

the class SortHandler method getLineRange.

@NotNull
private LineRange getLineRange(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) {
    final LineRange range = cmd.getLineRange(editor, context);
    final LineRange normalizedRange;
    // Something like "30,20sort" gets converted to "20,30sort"
    if (range.getEndLine() < range.getStartLine()) {
        normalizedRange = new LineRange(range.getEndLine(), range.getStartLine());
    } else {
        normalizedRange = range;
    }
    // If we don't have a range, we either have "sort", a selection, or a block
    if (normalizedRange.getEndLine() - normalizedRange.getStartLine() == 0) {
        // If we have a selection.
        final SelectionModel selectionModel = editor.getSelectionModel();
        if (selectionModel.hasSelection()) {
            final int start = selectionModel.getSelectionStart();
            final int end = selectionModel.getSelectionEnd();
            final int startLine = editor.offsetToLogicalPosition(start).line;
            final int endLine = editor.offsetToLogicalPosition(end).line;
            return new LineRange(startLine, endLine);
        } else // If we have a generic selection, i.e. "sort" entire document
        {
            return new LineRange(0, editor.getDocument().getLineCount() - 1);
        }
    }
    return normalizedRange;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) LineRange(com.maddyhome.idea.vim.ex.LineRange) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with SelectionModel

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

the class PermissionUsageQuickFixRunnable method run.

@Override
public void run() {
    final Document document = editor.getDocument();
    PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
    assert psiFile != null;
    SelectionModel selectionModel = editor.getSelectionModel();
    setLineSelection(document, selectionModel);
    selectionModel = editor.getSelectionModel();
    document.insertString(selectionModel.getSelectionEnd(), TEXT_AFTER);
    document.insertString(selectionModel.getSelectionStart(), TEXT_BEFORE);
    PsiDocumentManager.getInstance(project).commitDocument(document);
    CodeStyleManager.getInstance(project).reformat(psiFile.getNavigationElement());
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(psiFile.getNavigationElement());
    selectionModel.removeSelection(true);
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document)

Example 38 with SelectionModel

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

the class AndroidBaseXmlRefactoringAction method getNonEmptySelectionRange.

@Nullable
private static TextRange getNonEmptySelectionRange(Editor editor) {
    if (editor != null) {
        final SelectionModel model = editor.getSelectionModel();
        if (model.hasSelection()) {
            final int start = model.getSelectionStart();
            final int end = model.getSelectionEnd();
            if (start < end) {
                return TextRange.create(start, end);
            }
        }
    }
    return null;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) Nullable(org.jetbrains.annotations.Nullable)

Example 39 with SelectionModel

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

the class DartServerExtractLocalVariableDialog method perform.

public void perform() {
    final SelectionModel selectionModel = editor.getSelectionModel();
    final int offset = selectionModel.getSelectionStart();
    final int length = selectionModel.getSelectionEnd() - offset;
    // create refactoring
    createRefactoring(offset, length);
    if (refactoring == null) {
        return;
    }
    // prepare expressions
    final java.util.List<DartExpression> expressions;
    {
        final int[] offsets = refactoring.getCoveringExpressionOffsets();
        final int[] lengths = refactoring.getCoveringExpressionLengths();
        expressions = getDartExpressions(offsets, lengths);
        if (expressions == null) {
            return;
        }
    }
    // select the expression to extract
    if (expressions.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
        performOnExpression(expressions.get(0));
    } else if (expressions.size() > 1) {
        IntroduceTargetChooser.showChooser(editor, expressions, new Pass<DartExpression>() {

            @Override
            public void pass(DartExpression expression) {
                performOnExpression(expression);
            }
        }, PsiElement::getText);
    }
}
Also used : Pass(com.intellij.openapi.util.Pass) SelectionModel(com.intellij.openapi.editor.SelectionModel) DartExpression(com.jetbrains.lang.dart.psi.DartExpression)

Example 40 with SelectionModel

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

the class DartIntroduceHandler method performAction.

public void performAction(DartIntroduceOperation operation) {
    final PsiFile file = operation.getFile();
    if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) {
        return;
    }
    final Editor editor = operation.getEditor();
    if (editor.getSettings().isVariableInplaceRenameEnabled()) {
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(operation.getEditor());
        if (templateState != null && !templateState.isFinished()) {
            return;
        }
    }
    PsiElement element1 = null;
    PsiElement element2 = null;
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        element1 = file.findElementAt(selectionModel.getSelectionStart());
        element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
        if (element1 instanceof PsiWhiteSpace) {
            int startOffset = element1.getTextRange().getEndOffset();
            element1 = file.findElementAt(startOffset);
        }
        if (element2 instanceof PsiWhiteSpace) {
            int endOffset = element2.getTextRange().getStartOffset();
            element2 = file.findElementAt(endOffset - 1);
        }
    } else {
        if (smartIntroduce(operation)) {
            return;
        }
        final CaretModel caretModel = editor.getCaretModel();
        final Document document = editor.getDocument();
        int lineNumber = document.getLineNumber(caretModel.getOffset());
        if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
            element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
            element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
        }
    }
    final Project project = operation.getProject();
    if (element1 == null || element2 == null) {
        showCannotPerformError(project, editor);
        return;
    }
    element1 = DartRefactoringUtil.getSelectedExpression(project, file, element1, element2);
    if (element1 == null) {
        showCannotPerformError(project, editor);
        return;
    }
    if (!checkIntroduceContext(file, editor, element1)) {
        return;
    }
    operation.setElement(element1);
    performActionOnElement(operation);
}
Also used : Project(com.intellij.openapi.project.Project) CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

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