Java Code Examples for com.intellij.openapi.editor.Caret#setSelection()

The following examples show how to use com.intellij.openapi.editor.Caret#setSelection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SortLinesBySubSelectionAction.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
@NotNull
private List<SubSelectionSortLine> getLines(Editor editor, @NotNull SortSettings sortSettings, List<CaretState> caretsAndSelections) {
	List<SubSelectionSortLine> lines = new ArrayList<SubSelectionSortLine>();
	for (CaretState caretsAndSelection : caretsAndSelections) {
		LogicalPosition selectionStart = caretsAndSelection.getSelectionStart();
		int selectionStartOffset = editor.logicalPositionToOffset(selectionStart);
		LogicalPosition selectionEnd = caretsAndSelection.getSelectionEnd();
		int selectionEndOffset = editor.logicalPositionToOffset(selectionEnd);
		LogicalPosition caretPosition = caretsAndSelection.getCaretPosition();
		// no selection -> expand to end of line
		if (selectionStartOffset == selectionEndOffset) {
			String text = editor.getDocument().getText();
			selectionEndOffset = text.indexOf("\n", selectionStartOffset);
			if (selectionEndOffset == -1) {
				selectionEndOffset = text.length();
			}
			Caret caret = getCaretAt(editor, caretsAndSelection.getCaretPosition());
			caret.setSelection(selectionStartOffset, selectionEndOffset);
		}

		String selection = editor.getDocument().getText(
				new TextRange(selectionStartOffset, selectionEndOffset));

		int lineNumber = editor.getDocument().getLineNumber(selectionStartOffset);
		int lineStartOffset = editor.getDocument().getLineStartOffset(lineNumber);
		int lineEndOffset = editor.getDocument().getLineEndOffset(lineNumber);
		String line = editor.getDocument().getText(new TextRange(lineStartOffset, lineEndOffset));

		lines.add(new SubSelectionSortLine(sortSettings, line, selection, lineStartOffset, lineEndOffset,
				selectionStartOffset - lineStartOffset, selectionEndOffset - lineStartOffset
		));
	}
	return lines;
}
 
Example 2
Source File: SortLinesBySubSelectionAction.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
private void write(Editor editor, List<SubSelectionSortLine> lines, List<SubSelectionSortLine> sortedLines) {
	List<Caret> allCarets = editor.getCaretModel().getAllCarets();
	for (int i = lines.size() - 1; i >= 0; i--) {
		SubSelectionSortLine oldLine = lines.get(i);
		SubSelectionSortLine newLine = sortedLines.get(i);
		int lineStartOffset = oldLine.lineStartOffset;
		editor.getDocument().replaceString(lineStartOffset, oldLine.lineEndOffset, newLine.line);
		int startColumn = newLine.selectionStartLineOffset;
		int endColumn = newLine.selectionEndLineOffset;

		Caret caret = allCarets.get(i);
		caret.setSelection(lineStartOffset + startColumn, lineStartOffset + endColumn);
		caret.moveToOffset(lineStartOffset + startColumn);
	}
}
 
Example 3
Source File: ActionPerformer.java    From dummytext-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * @param   event    ActionSystem event
 */
void write(final AnActionEvent event) {
    Editor editor = event.getData(PlatformDataKeys.EDITOR);

    if (editor != null) {
        final Document document = editor.getDocument();

        CaretModel caretModel = editor.getCaretModel();

        for (Caret caret : caretModel.getAllCarets()) {
            boolean hasSelection = caret.hasSelection();
            String selectedText  = caret.getSelectedText();

            String trailingCharacter    = TextualHelper.getTrailingPunctuationMark(selectedText);
            String leadingCharacters    = TextualHelper.getLeadingPreservation(selectedText);

            int amountLines     = 1;
            // applies only when replacing a single-lined selection, can be rounded up
            Integer amountWords = null;

            // Generated dummy text will replace the current selected text
            if (hasSelection && selectedText != null ) {
                int selectionLength = selectedText.length();
                if (selectionLength > 0) {
                    amountLines = TextualHelper.countLines(selectedText);
                    if (amountLines == 1) {
                        amountWords = TextualHelper.getWordCount(selectedText);
                    }
                }
            }

            // Generate and insert / replace selection with dummy text
            String dummyText  = generateText(amountLines, amountWords, leadingCharacters, trailingCharacter, selectedText).toString();

            Integer dummyTextLength = dummyText.length();
            Integer offsetStart;

            if (hasSelection) {
                // Move caret to end of selection
                offsetStart = caret.getSelectionStart();
                int offsetEnd = caret.getSelectionEnd();

                document.replaceString(offsetStart, offsetEnd, dummyText);
                caret.setSelection(offsetStart, offsetStart + dummyTextLength);
            } else {
                // Move caret to end of inserted text
                offsetStart  = caretModel.getOffset();
                dummyText   = dummyText.trim();
                document.insertString(offsetStart, dummyText);
            }
        }

    }
}
 
Example 4
Source File: SelectOccurrencesActionHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected static void setSelection(Editor editor, Caret caret, TextRange selectionRange) {
  EditorActionUtil.makePositionVisible(editor, selectionRange.getStartOffset());
  EditorActionUtil.makePositionVisible(editor, selectionRange.getEndOffset());
  caret.setSelection(selectionRange.getStartOffset(), selectionRange.getEndOffset());
}