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

The following examples show how to use com.intellij.openapi.editor.SelectionModel#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: WrapEditorAction.java    From idea-latex with MIT License 6 votes vote down vote up
/**
 * Wraps selection.
 *
 * @param editor Current editor.
 */
private void wrap(@NotNull TextEditor editor) {
    final Document document = editor.getEditor().getDocument();
    final SelectionModel selectionModel = editor.getEditor().getSelectionModel();
    final CaretModel caretModel = editor.getEditor().getCaretModel();

    final int start = selectionModel.getSelectionStart();
    final int end = selectionModel.getSelectionEnd();
    final String text = StringUtil.notNullize(selectionModel.getSelectedText());

    String newText = getLeftText() + text + getRightText();
    int newStart = start + getLeftText().length();
    int newEnd = StringUtil.isEmpty(text) ? newStart : end + getLeftText().length();

    document.replaceString(start, end, newText);
    selectionModel.setSelection(newStart, newEnd);
    caretModel.moveToOffset(newEnd);
}
 
Example 2
Source File: WrapEditorAction.java    From idea-latex with MIT License 6 votes vote down vote up
/**
 * Unwraps selection.
 *
 * @param editor  Current editor.
 * @param matched Matched PSI element.
 */
private void unwrap(@NotNull final TextEditor editor, @NotNull final PsiElement matched) {
    final Document document = editor.getEditor().getDocument();
    final SelectionModel selectionModel = editor.getEditor().getSelectionModel();
    final CaretModel caretModel = editor.getEditor().getCaretModel();

    final int start = matched.getTextRange().getStartOffset();
    final int end = matched.getTextRange().getEndOffset();
    final String text = StringUtil.notNullize(matched.getText());

    String newText = StringUtil.trimEnd(StringUtil.trimStart(text, getLeftText()), getRightText());
    int newStart = selectionModel.getSelectionStart() - getLeftText().length();
    int newEnd = selectionModel.getSelectionEnd() - getLeftText().length();

    document.replaceString(start, end, newText);

    selectionModel.setSelection(newStart, newEnd);
    caretModel.moveToOffset(newEnd);
}
 
Example 3
Source File: AbstractCaseConvertingAction.java    From StringManipulation with Apache License 2.0 6 votes vote down vote up
private boolean propertiesHandling(Editor editor, DataContext dataContext, SelectionModel selectionModel,
		PsiFile psiFile) {
	PsiElement elementAtCaret = PsiUtilBase.getElementAtCaret(editor);
	if (elementAtCaret instanceof PsiWhiteSpace) {
		return false;
	} else if (elementAtCaret instanceof LeafPsiElement) {
		IElementType elementType = ((LeafPsiElement) elementAtCaret).getElementType();
		if (elementType.toString().equals("Properties:VALUE_CHARACTERS")
				|| elementType.toString().equals("Properties:KEY_CHARACTERS")) {
			TextRange textRange = elementAtCaret.getTextRange();
			if (textRange.getLength() == 0) {
				return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
			}
			selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
			return true;
		}
	}
	return false;
}
 
Example 4
Source File: AbstractCaseConvertingAction.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
private boolean genericHandling(Editor editor, DataContext dataContext, SelectionModel selectionModel,
		PsiFile psiFile) {
	int caretOffset = editor.getCaretModel().getOffset();
	PsiElement elementAtCaret = PsiUtilBase.getElementAtCaret(editor);
	if (elementAtCaret instanceof PsiPlainText) {
		return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
	} else if (elementAtCaret instanceof PsiWhiteSpace) {
		elementAtCaret = PsiUtilBase.getElementAtOffset(psiFile, caretOffset - 1);
	}

	if (elementAtCaret == null || elementAtCaret instanceof PsiWhiteSpace) {
		return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
	} else {
		TextRange textRange = elementAtCaret.getTextRange();
		if (textRange.getLength() == 0) {
			return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
		}
		selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
		String selectedText = selectionModel.getSelectedText();

		if (selectedText != null && selectedText.contains("\n")) {
			selectionModel.removeSelection();
			return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
		}
		if (StringUtil.isQuoted(selectedText)) {
			selectionModel.setSelection(selectionModel.getSelectionStart() + 1,
					selectionModel.getSelectionEnd() - 1);
		}

		if (caretOffset < selectionModel.getSelectionStart()) {
			editor.getCaretModel().moveToOffset(selectionModel.getSelectionStart());
		}
		if (caretOffset > selectionModel.getSelectionEnd()) {
			editor.getCaretModel().moveToOffset(selectionModel.getSelectionEnd());
		}
		return true;
	}
}
 
Example 5
Source File: AbstractStringManipAction.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
protected boolean selectSomethingUnderCaret(Editor editor, DataContext dataContext, SelectionModel selectionModel) {
	selectionModel.selectLineAtCaret();
	String selectedText = selectionModel.getSelectedText();
	if (selectedText != null && selectedText.endsWith("\n")) {
		selectionModel.setSelection(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd() - 1);
	}
	return true;
}
 
Example 6
Source File: EditorTextFieldCellRenderer.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void setTextToEditor(String text) {
  myEditor.getMarkupModel().removeAllHighlighters();
  myEditor.getDocument().setText(text);
  ((DesktopEditorImpl)myEditor).resetSizes();
  myEditor.getHighlighter().setText(text);
  if (myTextAttributes != null) {
    myEditor.getMarkupModel().addRangeHighlighter(0, myEditor.getDocument().getTextLength(), HighlighterLayer.ADDITIONAL_SYNTAX, myTextAttributes, HighlighterTargetArea.EXACT_RANGE);
  }

  ((DesktopEditorImpl)myEditor).setPaintSelection(mySelected);
  SelectionModel selectionModel = myEditor.getSelectionModel();
  selectionModel.setSelection(0, mySelected ? myEditor.getDocument().getTextLength() : 0);
}
 
Example 7
Source File: ExtractRuleAction.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void extractSelection(@NotNull PsiFile psiFile, Editor editor, SelectionModel selectionModel) {
	Document doc = editor.getDocument();
	String grammarText = psiFile.getText();
	ParsingResult results = ParsingUtils.parseANTLRGrammar(grammarText);
	final Parser parser = results.parser;
	final ParserRuleContext tree = (ParserRuleContext) results.tree;
	TokenStream tokens = parser.getTokenStream();

	int selStart = selectionModel.getSelectionStart();
	int selStop = selectionModel.getSelectionEnd() - 1; // I'm inclusive and they are exclusive for end offset

	// find appropriate tokens for bounds, don't include WS
	Token start = RefactorUtils.getTokenForCharIndex(tokens, selStart);
	Token stop = RefactorUtils.getTokenForCharIndex(tokens, selStop);
	if ( start==null || stop==null ) {
		return;
	}
	if ( start.getType()==ANTLRv4Lexer.WS ) {
		start = tokens.get(start.getTokenIndex() + 1);
	}
	if ( stop.getType()==ANTLRv4Lexer.WS ) {
		stop = tokens.get(stop.getTokenIndex() - 1);
	}

	selectionModel.setSelection(start.getStartIndex(), stop.getStopIndex() + 1);
	final Project project = psiFile.getProject();
	final ChooseExtractedRuleName nameChooser = new ChooseExtractedRuleName(project);
	nameChooser.show();
	if ( nameChooser.ruleName==null ) return;

	// make new rule string
	final String ruleText = selectionModel.getSelectedText();

	final int insertionPoint = RefactorUtils.getCharIndexOfNextRuleStart(tree, start.getTokenIndex());
	final String newRule = "\n" + nameChooser.ruleName + " : " + ruleText + " ;" + "\n";

	runWriteCommandAction(project, () -> {
		// do all as one operation.
		if ( insertionPoint >= doc.getTextLength() ) {
			doc.insertString(doc.getTextLength(), newRule);
		} else {
			doc.insertString(insertionPoint, newRule);
		}
		doc.replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), nameChooser.ruleName);
	});

	// TODO: only allow selection of fully-formed syntactic entity.
	// E.g., "A (',' A" is invalid grammatically as a rule.
}