@Override protected void doExecute(final Editor editor, @Nullable final Caret caret, final DataContext dataContext) { LineSelectionManager manager = LineSelectionManager.getInstance(editor); List<CaretState> caretStates = manager.getStartCaretStates(); if (caretStates != null) { Set<CaretEx> startMatchedCarets = manager.getStartMatchedCarets(); if (startMatchedCarets != null) { Set<Long> excludeList = CaretEx.getExcludedCoordinates(null, startMatchedCarets); Set<CaretEx> foundCarets = manager.getFoundCarets(); excludeList = CaretEx.getExcludedCoordinates(excludeList, foundCarets); List<CaretState> keepCarets = new ArrayList<>(caretStates.size() - startMatchedCarets.size()); for (CaretState caretState : caretStates) { if (excludeList != null && excludeList.contains(CaretEx.getCoordinates(caretState.getCaretPosition()))) continue; keepCarets.add(caretState); } manager.clearSearchFoundCarets(); editor.getCaretModel().setCaretsAndSelections(keepCarets); } else { manager.clearSearchFoundCarets(); editor.getCaretModel().setCaretsAndSelections(caretStates); } } }
public void filterCarets(Editor editor, List<CaretState> caretsAndSelections) { int previousLineNumber = -1; Iterator<CaretState> iterator = caretsAndSelections.iterator(); while (iterator.hasNext()) { CaretState caretsAndSelection = iterator.next(); LogicalPosition caretPosition = caretsAndSelection.getCaretPosition(); int lineNumber = editor.getDocument().getLineNumber( editor.logicalPositionToOffset(caretPosition)); if (lineNumber == previousLineNumber) { Caret caret = getCaretAt(editor, caretsAndSelection.getCaretPosition()); editor.getCaretModel().removeCaret(caret); iterator.remove(); } previousLineNumber = lineNumber; } }
@Override protected void doExecute(final Editor editor, @Nullable final Caret caret, final DataContext dataContext) { LineSelectionManager manager = LineSelectionManager.getInstance(editor); List<CaretState> caretStates = manager.getStartCaretStates(); if (caretStates != null) { manager.clearSearchFoundCarets(); editor.getCaretModel().setCaretsAndSelections(caretStates); } }
protected boolean perform(@NotNull LineSelectionManager manager, @NotNull Caret caret, @NotNull Range range, @NotNull ArrayList<CaretState> createCarets) { Editor editor = caret.getEditor(); final BasedSequence chars = BasedSequenceImpl.of(editor.getDocument().getCharsSequence()); boolean keepCaret = !isMoveFirstMatch(); T matcher = prepareMatcher(manager, caret, range, chars); if (matcher != null) { // forward search withing range in document CaretMatch lastMatch = null; while (true) { CaretMatch match = nextMatch(matcher, chars, range, lastMatch); if (match == null) break; // found it, create or move caret here if (!keepCaret) { keepCaret = true; if (isMoveFirstMatch()) { caret.moveToOffset(match.caretOffset); caret.setSelection(match.selectionStart, match.selectionEnd); } } else { // create a new position if caret moved LogicalPosition offset = editor.offsetToLogicalPosition(match.caretOffset); LogicalPosition startOffset = editor.offsetToLogicalPosition(match.selectionStart); LogicalPosition endOffset = editor.offsetToLogicalPosition(match.selectionEnd); CaretState caretState = new CaretState(offset, startOffset, endOffset); createCarets.add(caretState); } if (isSingleMatch() || match.caretOffset + match.matchLength >= chars.length()) break; lastMatch = match; } } return keepCaret || isSingleMatch(); }
@Override protected void executeWriteAction(Editor editor, @Nullable Caret caret, DataContext dataContext, @Nullable T additionalParameter) { List<CaretState> caretsAndSelections = editor.getCaretModel().getCaretsAndSelections(); if (caretsAndSelections.size() > 1) { multiSelection(editor, caretsAndSelections, additionalParameter); } else if (caretsAndSelections.size() == 1) { singleSelection(editor, caretsAndSelections, additionalParameter); } }
protected final void singleSelection(Editor editor, List<CaretState> caretsAndSelections, T additionalParameter) { CaretState caretsAndSelection = caretsAndSelections.get(0); LogicalPosition selectionStart = caretsAndSelection.getSelectionStart(); LogicalPosition selectionEnd = caretsAndSelection.getSelectionEnd(); String text = editor.getDocument().getText( new TextRange(editor.logicalPositionToOffset(selectionStart), editor.logicalPositionToOffset(selectionEnd))); String charSequence = processSingleSelection(text, additionalParameter); editor.getDocument().replaceString(editor.logicalPositionToOffset(selectionStart), editor.logicalPositionToOffset(selectionEnd), charSequence); }
public static void sort(List<CaretState> caretsAndSelections) { Collections.sort(caretsAndSelections, new Comparator<CaretState>() { @Override public int compare(CaretState o1, CaretState o2) { return o1.getCaretPosition().compareTo(o2.getCaretPosition()); } }); }
private void processMultiCaret(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 )); } List<SubSelectionSortLine> sortedLines = new ArrayList<SubSelectionSortLine>(lines); sortSettings.getSortType().sortLines(sortedLines); write(editor, lines, sortedLines); }
public void scollEditorToLine(int sourceLine) { ApplicationManager.getApplication().invokeLater( () -> { getEditor().getCaretModel().setCaretsAndSelections( Arrays.asList(new CaretState(new LogicalPosition(sourceLine - 1, 0), null, null)) ); getEditor().getScrollingModel().scrollToCaret(ScrollType.CENTER_UP); } ); }
protected String buildFileDetails() { CaretState selectionInfo = editor.getCaretModel().getCaretsAndSelections().get(0); int selectionStart = selectionInfo.getSelectionStart().line + 1; int selectionEnd = selectionInfo.getSelectionEnd().line + 1; return "File: " + currentFileName + ", Line(s): " + selectionStart + "-" + selectionEnd; }