Example usage for com.intellij.openapi.editor EditorModificationUtil scrollToCaret

List of usage examples for com.intellij.openapi.editor EditorModificationUtil scrollToCaret

Introduction

In this page you can find the example usage for com.intellij.openapi.editor EditorModificationUtil scrollToCaret.

Prototype

public static void scrollToCaret(@NotNull Editor editor) 

Source Link

Document

This method is safe to run both in and out of com.intellij.openapi.editor.CaretModel#runForEachCaret(CaretAction) context.

Usage

From source file:com.intellij.codeInsight.completion.CodeCompletionHandlerBase.java

License:Apache License

private static CompletionAssertions.WatchingInsertionContext insertItem(
        final CompletionProgressIndicator indicator, final LookupElement item, final char completionChar,
        List<LookupElement> items, final CompletionLookupArranger.StatisticsUpdate update, final Editor editor,
        final PsiFile psiFile, final int caretOffset, final int idEndOffset, final OffsetMap offsetMap) {
    editor.getCaretModel().moveToOffset(caretOffset);
    final int initialStartOffset = caretOffset - item.getLookupString().length();
    assert initialStartOffset >= 0 : "negative startOffset: " + caretOffset + "; " + item.getLookupString();

    offsetMap.addOffset(CompletionInitializationContext.START_OFFSET, initialStartOffset);
    offsetMap.addOffset(CompletionInitializationContext.SELECTION_END_OFFSET, caretOffset);
    offsetMap.addOffset(CompletionInitializationContext.IDENTIFIER_END_OFFSET, idEndOffset);

    final CompletionAssertions.WatchingInsertionContext context = new CompletionAssertions.WatchingInsertionContext(
            offsetMap, psiFile, completionChar, items, editor);
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
        @Override//from w  ww  .j  a v a2s .  c o m
        public void run() {
            if (caretOffset != idEndOffset && completionChar == Lookup.REPLACE_SELECT_CHAR) {
                editor.getDocument().deleteString(caretOffset, idEndOffset);
            }

            assert context.getStartOffset() >= 0 : "stale startOffset: was " + initialStartOffset + "; selEnd="
                    + caretOffset + "; idEnd=" + idEndOffset + "; file=" + context.getFile();
            assert context.getTailOffset() >= 0 : "stale tail: was " + initialStartOffset + "; selEnd="
                    + caretOffset + "; idEnd=" + idEndOffset + "; file=" + context.getFile();

            item.handleInsert(context);
            Project project = indicator.getProject();
            PostprocessReformattingAspect.getInstance(project).doPostponedFormatting();

            if (context.shouldAddCompletionChar()) {
                addCompletionChar(project, context, item, editor, indicator, completionChar);
            }
            if (!editor.getCaretModel().supportsMultipleCarets()) { // done later, outside of this method
                context.stopWatching();
            }
            EditorModificationUtil.scrollToCaret(editor);
        }
    });
    update.addSparedChars(indicator, item, context, completionChar);
    return context;
}

From source file:com.vladsch.MissingInActions.util.EditHelpers.java

License:Apache License

public static void moveCaretToNextWordStartOrEnd(@NotNull Editor editor, boolean isWithSelection, boolean camel,
        int flags) {
    if (!isSet(flags, BOUNDARY_FLAGS))
        return;/*  w ww .j  a va 2 s. c  om*/

    Document document = editor.getDocument();
    SelectionModel selectionModel = editor.getSelectionModel();
    int selectionStart = selectionModel.getLeadSelectionOffset();
    CaretModel caretModel = editor.getCaretModel();
    LogicalPosition blockSelectionStart = caretModel.getLogicalPosition();
    boolean haveMultiCarets = caretModel.getCaretCount() > 1;

    boolean stopAtTrailingBlanks = isSet(flags, START_OF_TRAILING_BLANKS);
    boolean stopAtLeadingBlanks = isSet(flags, END_OF_LEADING_BLANKS);
    boolean stopAtStartOfLine = isSet(flags, START_OF_LINE);
    boolean stopAtStartOfWord = isSet(flags, START_OF_WORD);
    boolean stopAtEndOfWord = isSet(flags, END_OF_WORD);
    boolean stopAtStartOfFolding = isSet(flags, START_OF_FOLDING_REGION);
    boolean stopAtEndOfFolding = isSet(flags, END_OF_FOLDING_REGION);
    boolean stopAtEndOfLine = isSet(flags, END_OF_LINE);
    boolean strictIdentifier = isSet(flags, MIA_IDENTIFIER);
    boolean singleLine = isSet(flags, SINGLE_LINE) || isSet(flags, MULTI_CARET_SINGLE_LINE) && haveMultiCarets;

    int offset = caretModel.getOffset();
    if (offset == document.getTextLength()) {
        return;
    }

    int lineNumber = caretModel.getLogicalPosition().line;
    if (lineNumber >= document.getLineCount())
        return;

    int stopAtLastNonBlank = 0;

    // have to stop at start of character if caret is not at or before first non-blank
    // only applies to start boundary condition
    int lineStartOffset = document.getLineStartOffset(lineNumber);
    if (stopAtTrailingBlanks || stopAtEndOfLine) {
        int lineEndOffset = document.getLineEndOffset(lineNumber);
        int trailingBlanks = countWhiteSpaceReversed(document.getCharsSequence(), lineStartOffset,
                lineEndOffset);
        if (stopAtTrailingBlanks && caretModel.getOffset() < lineEndOffset - trailingBlanks) {
            stopAtLastNonBlank = lineEndOffset - trailingBlanks;
        } else if (stopAtEndOfLine && (caretModel.getOffset() < lineEndOffset || singleLine)) {
            stopAtLastNonBlank = lineEndOffset;
        }
    }

    int maxLineNumber = stopAtLastNonBlank > 0 || lineNumber + 1 > document.getLineCount() ? lineNumber
            : lineNumber + 1;
    int maxOffset = stopAtLastNonBlank > 0 ? stopAtLastNonBlank
            : (stopAtStartOfLine && lineNumber < maxLineNumber ? document.getLineStartOffset(maxLineNumber)
                    : document.getLineEndOffset(maxLineNumber));

    int newOffset = offset + 1;
    if (newOffset > maxOffset)
        return;

    boolean done = false;
    FoldRegion currentFoldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(offset);
    if (currentFoldRegion != null) {
        newOffset = currentFoldRegion.getEndOffset();
        if (stopAtEndOfFolding)
            done = true;
    }

    int wordType = getWordType(flags);
    while (!done) {
        for (; newOffset < maxOffset; newOffset++) {
            if (stopAtStartOfWord && isWordTypeStart(wordType, editor, newOffset, camel)) {
                done = true;
                break;
            }
            if (stopAtEndOfWord && isWordTypeEnd(wordType, editor, newOffset, camel)) {
                done = true;
                break;
            }
        }
        if (newOffset >= maxOffset)
            break;

        FoldRegion foldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(newOffset);
        if (foldRegion != null) {
            if (stopAtStartOfFolding) {
                newOffset = foldRegion.getStartOffset();
                break;
            }
            newOffset = foldRegion.getEndOffset();
            if (stopAtEndOfFolding)
                break;
        }
    }

    if (editor instanceof EditorImpl) {
        int boundaryOffset = ((EditorImpl) editor).findNearestDirectionBoundary(offset, true);
        if (boundaryOffset >= 0) {
            newOffset = Math.min(boundaryOffset, newOffset);
        }
    }

    caretModel.moveToOffset(newOffset);
    EditorModificationUtil.scrollToCaret(editor);
    setupSelection(editor, isWithSelection, selectionStart, blockSelectionStart);
}

From source file:com.vladsch.MissingInActions.util.EditHelpers.java

License:Apache License

public static void moveCaretToPreviousWordStartOrEnd(@NotNull Editor editor, boolean isWithSelection,
        boolean camel, int flags) {
    if (!isSet(flags, BOUNDARY_FLAGS))
        return;/*from   w  w  w.  j a  va2  s . c o m*/

    Document document = editor.getDocument();
    SelectionModel selectionModel = editor.getSelectionModel();
    int selectionStart = selectionModel.getLeadSelectionOffset();
    CaretModel caretModel = editor.getCaretModel();
    boolean haveMultiCarets = caretModel.getCaretCount() > 1;

    int offset = caretModel.getOffset();
    if (offset == 0)
        return;

    boolean stopAtTrailingBlanks = isSet(flags, START_OF_TRAILING_BLANKS);
    boolean stopAtLeadingBlanks = isSet(flags, END_OF_LEADING_BLANKS);
    boolean stopAtStartOfLine = isSet(flags, START_OF_LINE);
    boolean stopAtStartOfWord = isSet(flags, START_OF_WORD);
    boolean stopAtEndOfWord = isSet(flags, END_OF_WORD);
    boolean stopAtEndOfLine = isSet(flags, END_OF_LINE);
    boolean stopAtStartOfFolding = isSet(flags, START_OF_FOLDING_REGION);
    boolean stopAtEndOfFolding = isSet(flags, END_OF_FOLDING_REGION);
    boolean strictIdentifier = isSet(flags, MIA_IDENTIFIER);
    boolean singleLine = isSet(flags, SINGLE_LINE) || isSet(flags, MULTI_CARET_SINGLE_LINE) && haveMultiCarets;

    LogicalPosition position = caretModel.getLogicalPosition();
    int lineNumber = position.line;
    int stopAtIndent = 0;

    // have to stop at start of character if caret is not at or before first non-blank
    int lineStartOffset = document.getLineStartOffset(lineNumber);
    int lineEndOffset = document.getLineEndOffset(lineNumber);

    if (stopAtEndOfLine && position.column > editor.offsetToLogicalPosition(lineEndOffset).column) {
        stopAtIndent = lineEndOffset;
    }

    if (stopAtIndent == 0 && stopAtTrailingBlanks) {
        int trailingBlanks = countWhiteSpaceReversed(document.getCharsSequence(), lineStartOffset,
                lineEndOffset);
        if (offset > lineEndOffset - trailingBlanks) {
            stopAtIndent = lineEndOffset - trailingBlanks;
        }
    }

    if (stopAtIndent == 0 && (stopAtLeadingBlanks || stopAtStartOfLine)) {
        int firstNonBlank = countWhiteSpace(document.getCharsSequence(), lineStartOffset,
                document.getLineEndOffset(lineNumber));
        LogicalPosition firstNonBlankPosition = editor.offsetToLogicalPosition(lineStartOffset + firstNonBlank);
        if (stopAtLeadingBlanks && position.column > firstNonBlankPosition.column) {
            stopAtIndent = lineStartOffset + firstNonBlank;
        } else if (stopAtStartOfLine && (position.column != 0 || singleLine)) {
            stopAtIndent = lineStartOffset;
        }
    }

    int minLineNumber = lineNumber == 0 || stopAtIndent > 0 ? lineNumber : lineNumber - 1;
    int minOffset = stopAtIndent > 0 ? stopAtIndent
            : (stopAtEndOfLine && lineNumber > minLineNumber ? document.getLineEndOffset(minLineNumber)
                    : document.getLineStartOffset(minLineNumber));

    // if virtual spaces are enabled the caret can be after the end so we should pretend it is on the next char after the end
    int newOffset = stopAtIndent > offset - 1 ? stopAtIndent : offset - 1;
    if (newOffset < minOffset)
        return;

    boolean done = false;
    FoldRegion currentFoldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(offset - 1);
    if (currentFoldRegion != null) {
        newOffset = currentFoldRegion.getStartOffset();
        if (stopAtStartOfFolding)
            done = true;
    }

    int wordType = getWordType(flags);
    while (!done) {
        for (; newOffset > minOffset; newOffset--) {
            if (stopAtEndOfWord && isWordTypeEnd(wordType, editor, newOffset, camel)) {
                done = true;
                break;
            }
            if (stopAtStartOfWord && isWordTypeStart(wordType, editor, newOffset, camel)) {
                done = true;
                break;
            }
        }
        if (newOffset <= minOffset)
            break;

        FoldRegion foldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(newOffset);
        if (foldRegion != null) {
            if (stopAtEndOfFolding) {
                newOffset = foldRegion.getEndOffset();
                break;
            }
            newOffset = foldRegion.getStartOffset();
            if (stopAtStartOfFolding)
                break;
        }
    }

    if (editor instanceof EditorImpl) {
        int boundaryOffset = ((EditorImpl) editor).findNearestDirectionBoundary(offset, false);
        if (boundaryOffset >= 0) {
            newOffset = Math.max(boundaryOffset, newOffset);
        }
        caretModel.moveToLogicalPosition(editor.offsetToLogicalPosition(newOffset).leanForward(true));
    } else {
        editor.getCaretModel().moveToOffset(newOffset);
    }

    EditorModificationUtil.scrollToCaret(editor);
    setupSelection(editor, isWithSelection, selectionStart, position);
}

From source file:com.vladsch.MissingInActions.util.EditHelpers.java

License:Apache License

public static void deleteSelectedText(@NotNull Editor editor) {
    SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection())
        return;//  w ww  . ja  v a  2  s  .  co m

    int selectionStart = selectionModel.getSelectionStart();
    int selectionEnd = selectionModel.getSelectionEnd();

    VisualPosition selectionStartPosition = selectionModel.getSelectionStartPosition();
    if (editor.isColumnMode() && editor.getCaretModel().supportsMultipleCarets()
            && selectionStartPosition != null) {
        editor.getCaretModel().moveToVisualPosition(selectionStartPosition);
        selectionModel.removeSelection();
        editor.getDocument().deleteString(selectionStart, selectionEnd);
        EditorModificationUtil.scrollToCaret(editor);
    } else {
        // we handle line type selection deletes
        delete(editor, editor.getCaretModel().getPrimaryCaret(), selectionStart, selectionEnd, false);
    }
}

From source file:com.vladsch.MissingInActions.util.EditHelpers.java

License:Apache License

public static void delete(@NotNull Editor editor, @NotNull Caret caret, int start, int end, boolean clearOnly) {
    CopyPasteManager.getInstance().stopKillRings();
    if (clearOnly) {
        editor.getDocument().replaceString(start, end, RepeatedCharSequence.of(" ", end - start));
    } else {//from   w  w  w . j  a  v a2  s.co m
        LineSelectionManager manager = LineSelectionManager.getInstance(editor);
        manager.guard(() -> {
            EditorCaret editorCaret = manager.getEditorCaret(caret);
            if (editorCaret.isLine()) {
                EditorPositionFactory f = manager.getPositionFactory();
                EditorPosition pos = f.fromPosition(caret.getLogicalPosition());
                EditorPosition selStart = f.fromOffset(start);

                editor.getDocument().deleteString(start, end);

                // in case the caret was in the virtual space, we force it to go back to the real offset
                caret.moveToLogicalPosition(selStart.atColumn(pos.column));
            } else {
                editor.getDocument().deleteString(start, end);
                // in case the caret was in the virtual space, we force it to go back to the real offset
                caret.moveToOffset(start);
            }
            EditorModificationUtil.scrollToCaret(editor);
        });
    }
}

From source file:com.vladsch.MissingInActions.util.EditHelpers.java

License:Apache License

public static void scrollToCaret(Editor editor) {
    EditorModificationUtil.scrollToCaret(editor);
}