Java Code Examples for com.intellij.openapi.editor.CaretState#getSelectionStart()

The following examples show how to use com.intellij.openapi.editor.CaretState#getSelectionStart() . 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: 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 4
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 5
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 6
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 7
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;
}