com.intellij.openapi.editor.CaretState Java Examples

The following examples show how to use com.intellij.openapi.editor.CaretState. 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: SortTypeDialog.java    From StringManipulation with Apache License 2.0 6 votes vote down vote up
protected List<String> sort(Editor editor, SortSettings settings) {
	List<CaretState> caretsAndSelections = editor.getCaretModel().getCaretsAndSelections();
	IdeUtils.sort(caretsAndSelections);
	int length = 0;
	List<String> lines = new ArrayList<String>();
	for (CaretState caretsAndSelection : caretsAndSelections) {
		LogicalPosition selectionStart = caretsAndSelection.getSelectionStart();
		LogicalPosition selectionEnd = caretsAndSelection.getSelectionEnd();
		String text = editor.getDocument().getText(
				new TextRange(editor.logicalPositionToOffset(selectionStart),
						editor.logicalPositionToOffset(selectionEnd)));

		length += text.length();
		String[] split = text.split("\n");
		lines.addAll(Arrays.asList(split));
		if (length > MAX_PREVIEW_LENGTH) {
			break;
		}
	}

	List<String> result = new SortLines(lines, settings).sortLines();
	return result;
}
 
Example #2
Source File: SortTokensGui.java    From StringManipulation with Apache License 2.0 6 votes vote down vote up
protected void preview() {
	List<CaretState> caretsAndSelections = editor.getCaretModel().getCaretsAndSelections();
	IdeUtils.sort(caretsAndSelections);
	List<String> lines = new ArrayList<String>();
	for (CaretState caretsAndSelection : caretsAndSelections) {
		LogicalPosition selectionStart = caretsAndSelection.getSelectionStart();
		LogicalPosition selectionEnd = caretsAndSelection.getSelectionEnd();
		String text = editor.getDocument().getText(
				new TextRange(editor.logicalPositionToOffset(selectionStart),
						editor.logicalPositionToOffset(selectionEnd)));

		String[] split = text.split("\n");
		lines.addAll(Arrays.asList(split));
	}

	SortTokens columnAligner = new SortTokens(lines, getModel());
	List<String> result = columnAligner.sortLines();

	ApplicationManager.getApplication().runWriteAction(() -> {
		myEditor.getDocument().setText(Joiner.on("\n").join(result));
		myPreviewPanel.validate();
		myPreviewPanel.repaint();
	});
}
 
Example #3
Source File: SortLinesBySubSelectionAction.java    From StringManipulation with Apache License 2.0 6 votes vote down vote up
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;
	}
}
 
Example #4
Source File: PluginPersistentStateComponent.java    From StringManipulation with Apache License 2.0 6 votes vote down vote up
@NotNull
protected String getTextForPreview(Editor editor) {
	List<CaretState> caretsAndSelections = editor.getCaretModel().getCaretsAndSelections();
	IdeUtils.sort(caretsAndSelections);
	StringBuilder sb = new StringBuilder();
	for (CaretState caretsAndSelection : caretsAndSelections) {
		LogicalPosition selectionStart = caretsAndSelection.getSelectionStart();
		LogicalPosition selectionEnd = caretsAndSelection.getSelectionEnd();
		String text = editor.getDocument().getText(
				new TextRange(editor.logicalPositionToOffset(selectionStart),
						editor.logicalPositionToOffset(selectionEnd)));

		sb.append(text);
		if (sb.length() > 10000) {
			break;
		}
	}
	String s = sb.toString();
	s = s.substring(0, Math.min(10000, s.length()));
	return s;
}
 
Example #5
Source File: HaxeRenameTest.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
public void doTestOnNthSelection(int n, String newName, String... additionalFiles) {
  // One-based, for humans. :)
  assertTrue(n > 0);

  myFixture.configureByFiles(ArrayUtil.reverseArray(ArrayUtil.append(additionalFiles, getTruncatedSourceFileName())));

  // Extract the caret info, and then reset it to just the selection/position requested.
  List<CaretState> carets = myFixture.getEditor().getCaretModel().getCaretsAndSelections();
  assertNotEmpty(carets);  // No carets/selections in the source file.
  assertTrue(carets.size() >= n);

  List<CaretState> useCaret = new ArrayList<CaretState>(1);
  useCaret.add(carets.get(n-1));
  myFixture.getEditor().getCaretModel().setCaretsAndSelections(useCaret);

  myFixture.testRename(getTruncatedResultFileName(), newName);
}
 
Example #6
Source File: RecentLocationsRenderer.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void selectSearchResultsInEditor(@Nonnull Editor editor, @Nonnull Iterator<? extends TextRange> resultIterator) {
  if (!editor.getCaretModel().supportsMultipleCarets()) {
    return;
  }
  ArrayList<CaretState> caretStates = new ArrayList<>();
  while (resultIterator.hasNext()) {
    TextRange findResult = resultIterator.next();

    int caretOffset = findResult.getEndOffset();

    int selectionStartOffset = findResult.getStartOffset();
    int selectionEndOffset = findResult.getEndOffset();
    EditorActionUtil.makePositionVisible(editor, caretOffset);
    EditorActionUtil.makePositionVisible(editor, selectionStartOffset);
    EditorActionUtil.makePositionVisible(editor, selectionEndOffset);
    caretStates.add(new CaretState(editor.offsetToLogicalPosition(caretOffset), editor.offsetToLogicalPosition(selectionStartOffset), editor.offsetToLogicalPosition(selectionEndOffset)));
  }
  if (caretStates.isEmpty()) {
    return;
  }
  editor.getCaretModel().setCaretsAndSelections(caretStates);
}
 
Example #7
Source File: MultiCaretHandlerHandler.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example #8
Source File: MultiCaretHandlerHandler.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #9
Source File: IdeUtils.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
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());
		}
	});
}
 
Example #10
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 #11
Source File: AlignToColumnsForm.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
protected void preview() {
	List<CaretState> caretsAndSelections = editor.getCaretModel().getCaretsAndSelections();
	IdeUtils.sort(caretsAndSelections);
	List<String> lines = new ArrayList<String>();
	for (CaretState caretsAndSelection : caretsAndSelections) {
		LogicalPosition selectionStart = caretsAndSelection.getSelectionStart();
		LogicalPosition selectionEnd = caretsAndSelection.getSelectionEnd();
		String text = editor.getDocument().getText(
			new TextRange(editor.logicalPositionToOffset(selectionStart),
				editor.logicalPositionToOffset(selectionEnd)));

		String[] split = text.split("\n");
		lines.addAll(Arrays.asList(split));
	}

	ColumnAligner columnAligner = new ColumnAligner(getModel());
	List<String> result = columnAligner.align(lines);

	debugValues.removeAll();
	List<String> debug = columnAligner.getDebugValues();
	for (String s : debug) {
		debugValues.add(new JLabel(s));
	}
	debugValues.revalidate();
	debugValues.repaint();


	ApplicationManager.getApplication().runWriteAction(() -> {
		myPreviewEditor.getDocument().setText(Joiner.on("\n").join(result));
		myPreviewPanel.validate();
		myPreviewPanel.repaint();
	});
}
 
Example #12
Source File: SlackPost.java    From SlackStorm with GNU General Public License v2.0 4 votes vote down vote 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;
}
 
Example #13
Source File: SortLinesBySubSelectionAction.java    From StringManipulation with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("Duplicates")
@Nullable
protected SortSettings getSortSettings(final Editor editor) {
	final SortTypeDialog dialog = new SortTypeDialog(PluginPersistentStateComponent.getInstance().getSortSettings(), false, editor) {
		@Override
		protected List<String> sort(Editor editor1, SortSettings settings) {
			List<CaretState> caretsAndSelections = editor1.getCaretModel().getCaretsAndSelections();
			IdeUtils.sort(caretsAndSelections);
			List<SubSelectionSortLine> lines = getLines(editor, getSettings(), caretsAndSelections);

			List<SubSelectionSortLine> sortedLines = settings.getSortType().sortLines(lines, settings.getBaseComparator(), settings.getCollatorLanguageTag());

			List<String> result = new ArrayList<>();
			for (SubSelectionSortLine sortedLine : sortedLines) {
				result.add(sortedLine.line);
			}
			return result;
		}
	};
	DialogWrapper dialogWrapper = new DialogWrapper(editor.getProject()) {
		{
			init();
			setTitle("Sort Lines by Subselection");
		}

		@Nullable
		@Override
		public JComponent getPreferredFocusedComponent() {
			return dialog.insensitive;
		}

		@Nullable
		@Override
		protected String getDimensionServiceKey() {
			return "StringManipulation.SortLinesBySubSelection";
		}

		@Nullable
		@Override
		protected JComponent createCenterPanel() {
			return dialog.contentPane;
		}

		@Override
		protected void doOKAction() {
			super.doOKAction();
		}

	};

	boolean b = dialogWrapper.showAndGet();
	if (!b) {
		return null;
	}
	SortSettings sortSettings = dialog.getSettings().preserveLeadingSpaces(false).preserveTrailingSpecialCharacters(false);
	PluginPersistentStateComponent.getInstance().setSortSettings(sortSettings);
	return sortSettings;
}
 
Example #14
Source File: SortLinesBySubSelectionAction.java    From StringManipulation with Apache License 2.0 3 votes vote down vote up
private void processMultiCaret(Editor editor, @NotNull SortSettings sortSettings, List<CaretState> caretsAndSelections) {
	List<SubSelectionSortLine> lines = getLines(editor, sortSettings, caretsAndSelections);

	List<SubSelectionSortLine> sortedLines = sortSettings.getSortType().sortLines(lines, sortSettings.getBaseComparator(), sortSettings.getCollatorLanguageTag());

	write(editor, lines, sortedLines);
}