Search in sources :

Example 16 with SelectionModel

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

the class ReplaceOctalEscapeWithUnicodeEscapeIntention method processIntention.

@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        // does not check if octal escape is inside char or string literal (garbage in, garbage out)
        final Document document = editor.getDocument();
        final int start = selectionModel.getSelectionStart();
        final int end = selectionModel.getSelectionEnd();
        final String text = document.getText(new TextRange(start, end));
        final int textLength = end - start;
        final StringBuilder replacement = new StringBuilder(textLength);
        int anchor = 0;
        while (true) {
            final int index = indexOfOctalEscape(text, anchor + 1);
            if (index < 0) {
                break;
            }
            replacement.append(text.substring(anchor, index));
            anchor = appendUnicodeEscape(text, index, replacement);
        }
        replacement.append(text.substring(anchor, textLength));
        document.replaceString(start, end, replacement);
    } else if (element instanceof PsiLiteralExpression) {
        final PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
        final String text = literalExpression.getText();
        final CaretModel model = editor.getCaretModel();
        final int offset = model.getOffset() - literalExpression.getTextOffset();
        final StringBuilder newLiteralText = new StringBuilder();
        final int index1 = indexOfOctalEscape(text, offset);
        final int index2 = indexOfOctalEscape(text, offset + 1);
        final int escapeStart = index2 == offset ? index2 : index1;
        newLiteralText.append(text.substring(0, escapeStart));
        final int escapeEnd = appendUnicodeEscape(text, escapeStart, newLiteralText);
        newLiteralText.append(text.substring(escapeEnd, text.length()));
        PsiReplacementUtil.replaceExpression(literalExpression, newLiteralText.toString());
    }
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document)

Example 17 with SelectionModel

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

the class IterateOverIterableIntention method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final TemplateImpl template = getTemplate();
    SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection()) {
        final PsiExpression iterableExpression = getIterableExpression(editor, file);
        LOG.assertTrue(iterableExpression != null);
        TextRange textRange = iterableExpression.getTextRange();
        selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
    }
    new InvokeTemplateAction(template, editor, project, new HashSet<>()).perform();
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) InvokeTemplateAction(com.intellij.codeInsight.template.impl.InvokeTemplateAction) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) HashSet(java.util.HashSet)

Example 18 with SelectionModel

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

the class AbstractRegionToKillRingTest method parse.

/**
   * Checks current editor and returns tuple of <code>(selected text; text over than selected)</code>.
   * 
   * @return    tuple of <code>(selected text; text over than selected)</code>.
   */
@NotNull
protected static Pair<String, String> parse() {
    SelectionModel selectionModel = myEditor.getSelectionModel();
    if (!selectionModel.hasSelection()) {
        return new Pair<>(null, myEditor.getDocument().getText());
    }
    CharSequence text = myEditor.getDocument().getCharsSequence();
    String selectedText = text.subSequence(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()).toString();
    StringBuilder nonSelectedText = new StringBuilder();
    nonSelectedText.append(text.subSequence(0, selectionModel.getSelectionStart())).append(text.subSequence(selectionModel.getSelectionEnd(), text.length()));
    return new Pair<>(selectedText, nonSelectedText.toString());
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with SelectionModel

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

the class ToggleColumnModeAction method setSelected.

@Override
public void setSelected(AnActionEvent e, boolean state) {
    final EditorEx editor = getEditor(e);
    final SelectionModel selectionModel = editor.getSelectionModel();
    final CaretModel caretModel = editor.getCaretModel();
    if (state) {
        caretModel.removeSecondaryCarets();
        boolean hasSelection = selectionModel.hasSelection();
        int selStart = selectionModel.getSelectionStart();
        int selEnd = selectionModel.getSelectionEnd();
        LogicalPosition blockStart, blockEnd;
        if (caretModel.supportsMultipleCarets()) {
            LogicalPosition logicalSelStart = editor.offsetToLogicalPosition(selStart);
            LogicalPosition logicalSelEnd = editor.offsetToLogicalPosition(selEnd);
            int caretOffset = caretModel.getOffset();
            blockStart = selStart == caretOffset ? logicalSelEnd : logicalSelStart;
            blockEnd = selStart == caretOffset ? logicalSelStart : logicalSelEnd;
        } else {
            blockStart = selStart == caretModel.getOffset() ? caretModel.getLogicalPosition() : editor.offsetToLogicalPosition(selStart);
            blockEnd = selEnd == caretModel.getOffset() ? caretModel.getLogicalPosition() : editor.offsetToLogicalPosition(selEnd);
        }
        editor.setColumnMode(true);
        if (hasSelection) {
            selectionModel.setBlockSelection(blockStart, blockEnd);
        } else {
            selectionModel.removeSelection();
        }
    } else {
        boolean hasSelection = false;
        int selStart = 0;
        int selEnd = 0;
        if (caretModel.supportsMultipleCarets()) {
            hasSelection = true;
            List<Caret> allCarets = caretModel.getAllCarets();
            Caret fromCaret = allCarets.get(0);
            Caret toCaret = allCarets.get(allCarets.size() - 1);
            if (fromCaret == caretModel.getPrimaryCaret()) {
                Caret tmp = fromCaret;
                fromCaret = toCaret;
                toCaret = tmp;
            }
            selStart = fromCaret.getLeadSelectionOffset();
            selEnd = toCaret.getSelectionStart() == toCaret.getLeadSelectionOffset() ? toCaret.getSelectionEnd() : toCaret.getSelectionStart();
        }
        editor.setColumnMode(false);
        caretModel.removeSecondaryCarets();
        if (hasSelection) {
            selectionModel.setSelection(selStart, selEnd);
        } else {
            selectionModel.removeSelection();
        }
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) Caret(com.intellij.openapi.editor.Caret)

Example 20 with SelectionModel

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

the class DartServerExtractMethodDialog method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection())
        selectionModel.selectLineAtCaret();
    final int offset = selectionModel.getSelectionStart();
    final int length = selectionModel.getSelectionEnd() - offset;
    final ServerExtractMethodRefactoring refactoring = new ServerExtractMethodRefactoring(project, file.getVirtualFile(), offset, length);
    // Validate initial status.
    {
        final RefactoringStatus initialStatus = refactoring.checkInitialConditions();
        if (initialStatus == null) {
            return;
        }
        if (initialStatus.hasError()) {
            final String title = DartBundle.message("dart.refactoring.extract.method.error");
            CommonRefactoringUtil.showErrorHint(project, editor, initialStatus.getMessage(), title, null);
            return;
        }
    }
    new DartServerExtractMethodDialog(project, editor, refactoring).show();
}
Also used : ServerExtractMethodRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerExtractMethodRefactoring) SelectionModel(com.intellij.openapi.editor.SelectionModel) RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus)

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