Java Code Examples for com.intellij.openapi.editor.Caret#getSelectedText()
The following examples show how to use
com.intellij.openapi.editor.Caret#getSelectedText() .
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: DelimitedListAction.java From StringManipulation with Apache License 2.0 | 5 votes |
protected String getSelectedText(Caret caret) { final String originalText; if (caret.hasSelection()) { originalText = caret.getSelectedText() + "";//avoid null value } else { originalText = ""; } return originalText; }
Example 2
Source File: SelectAllOccurrencesAction.java From consulo with Apache License 2.0 | 5 votes |
@Override public void doExecute(Editor editor, @Nullable Caret c, DataContext dataContext) { Caret caret = c == null ? editor.getCaretModel().getPrimaryCaret() : c; if (!caret.hasSelection()) { TextRange wordSelectionRange = getSelectionRange(editor, caret); if (wordSelectionRange != null) { setSelection(editor, caret, wordSelectionRange); } } String selectedText = caret.getSelectedText(); Project project = editor.getProject(); if (project == null || selectedText == null) { return; } int caretShiftFromSelectionStart = caret.getOffset() - caret.getSelectionStart(); FindManager findManager = FindManager.getInstance(project); FindModel model = new FindModel(); model.setStringToFind(selectedText); model.setCaseSensitive(true); model.setWholeWordsOnly(true); int searchStartOffset = 0; FindResult findResult = findManager.findString(editor.getDocument().getCharsSequence(), searchStartOffset, model); while (findResult.isStringFound()) { int newCaretOffset = caretShiftFromSelectionStart + findResult.getStartOffset(); EditorActionUtil.makePositionVisible(editor, newCaretOffset); Caret newCaret = editor.getCaretModel().addCaret(editor.offsetToVisualPosition(newCaretOffset)); if (newCaret != null) { setSelection(editor, newCaret, findResult); } findResult = findManager.findString(editor.getDocument().getCharsSequence(), findResult.getEndOffset(), model); } editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); }
Example 3
Source File: QueryAction.java From jetbrains-plugin-graph-database-support with Apache License 2.0 | 4 votes |
@Override public void actionPerformed(AnActionEvent e) { Analytics.event("query", getQueryExecutionAction(e)); Project project = getEventProject(e); Editor editor = e.getData(CommonDataKeys.EDITOR_EVEN_IF_INACTIVE); PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE); if (isNull(project)) { Notifier.error(QUERY_EXECUTION_ERROR_TITLE, NO_PROJECT_PRESENT_MESSAGE); return; } if (isNull(editor)) { Notifier.error(QUERY_EXECUTION_ERROR_TITLE, NO_EDITOR_PRESENT_MESSAGE); return; } String query = null; String analyticsEvent; Map<String, Object> parameters = Collections.emptyMap(); if (preSetQuery == null) { Caret caret = editor.getCaretModel().getPrimaryCaret(); analyticsEvent = caret.hasSelection() ? CONTENT_FROM_SELECT_ACTION : CONTENT_FROM_CARET_ACTION; if (caret.hasSelection()) { query = caret.getSelectedText(); } else if (nonNull(psiFile)) { String languageId = psiFile.getLanguage().getID(); if (isSupported(languageId)) { PsiElement cypherStatement = getCypherStatementAtOffset(psiFile, caret.getOffset()); if (nonNull(cypherStatement)) { query = cypherStatement.getText(); parameters = getParametersFromQuery(cypherStatement, project, editor); } } } } else { analyticsEvent = CONTENT_FROM_LINE_MARKER_ACTION; query = preSetQuery.getText(); parameters = getParametersFromQuery(preSetQuery, project, editor); } Analytics.event("query-content", analyticsEvent); if (isNull(query)) { Notifier.error(QUERY_EXECUTION_ERROR_TITLE, NO_QUERY_SELECTED_MESSAGE); return; } actionPerformed(e, project, editor, query, parameters); }
Example 4
Source File: ActionPerformer.java From dummytext-plugin with Apache License 2.0 | 4 votes |
/** * @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 5
Source File: SelectNextOccurrenceAction.java From consulo with Apache License 2.0 | 4 votes |
@Override public void doExecute(Editor editor, @Nullable Caret c, DataContext dataContext) { Caret caret = c == null ? editor.getCaretModel().getPrimaryCaret() : c; TextRange wordSelectionRange = getSelectionRange(editor, caret); boolean notFoundPreviously = getAndResetNotFoundStatus(editor); boolean wholeWordSearch = isWholeWordSearch(editor); if (caret.hasSelection()) { Project project = editor.getProject(); String selectedText = caret.getSelectedText(); if (project == null || selectedText == null) { return; } FindManager findManager = FindManager.getInstance(project); FindModel model = new FindModel(); model.setStringToFind(selectedText); model.setCaseSensitive(true); model.setWholeWordsOnly(wholeWordSearch); int searchStartOffset = notFoundPreviously ? 0 : caret.getSelectionEnd(); FindResult findResult = findManager.findString(editor.getDocument().getCharsSequence(), searchStartOffset, model); if (findResult.isStringFound()) { int newCaretOffset = caret.getOffset() - caret.getSelectionStart() + findResult.getStartOffset(); EditorActionUtil.makePositionVisible(editor, newCaretOffset); Caret newCaret = editor.getCaretModel().addCaret(editor.offsetToVisualPosition(newCaretOffset)); if (newCaret == null) { // this means that the found occurence is already selected if (notFoundPreviously) { setNotFoundStatus(editor); // to make sure we won't show hint anymore if there are no more occurrences } } else { setSelection(editor, newCaret, findResult); } } else { setNotFoundStatus(editor); showHint(editor); } } else { if (wordSelectionRange == null) { return; } setSelection(editor, caret, wordSelectionRange); setWholeWordSearch(editor, true); } editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); }