Java Code Examples for com.intellij.openapi.editor.Caret#getSelectionEnd()
The following examples show how to use
com.intellij.openapi.editor.Caret#getSelectionEnd() .
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: EditorIllustrationAction.java From intellij-sdk-docs with Apache License 2.0 | 6 votes |
/** * Replaces the run of text selected by the primary caret with a fixed string. * @param e Event related to this action */ @Override public void actionPerformed(@NotNull final AnActionEvent e) { // Get all the required data from data keys // Editor and Project were verified in update(), so they are not null. final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR); final Project project = e.getRequiredData(CommonDataKeys.PROJECT); final Document document = editor.getDocument(); // Work off of the primary caret to get the selection info Caret primaryCaret = editor.getCaretModel().getPrimaryCaret(); int start = primaryCaret.getSelectionStart(); int end = primaryCaret.getSelectionEnd(); // Replace the selection with a fixed string. // Must do this document change in a write action context. WriteCommandAction.runWriteCommandAction(project, () -> document.replaceString(start, end, "editor_basics") ); // De-select the text range that was just replaced primaryCaret.removeSelection(); }
Example 2
Source File: DeleteLineAction.java From consulo with Apache License 2.0 | 6 votes |
private static TextRange getRangeToDelete(Editor editor, Caret caret) { int selectionStart = caret.getSelectionStart(); int selectionEnd = caret.getSelectionEnd(); int startOffset = EditorUtil.getNotFoldedLineStartOffset(editor, selectionStart); // There is a possible case that selection ends at the line start, i.e. something like below ([...] denotes selected text, // '|' is a line start): // |line 1 // |[line 2 // |]line 3 // We don't want to delete line 3 here. However, the situation below is different: // |line 1 // |[line 2 // |line] 3 // Line 3 must be removed here. int endOffset = EditorUtil.getNotFoldedLineEndOffset(editor, selectionEnd > 0 && selectionEnd != selectionStart ? selectionEnd - 1 : selectionEnd); if (endOffset < editor.getDocument().getTextLength()) { endOffset++; } else if (startOffset > 0) { startOffset--; } return new TextRange(startOffset, endOffset); }
Example 3
Source File: PsiUtilBase.java From consulo with Apache License 2.0 | 6 votes |
@Nullable public static Language getLanguageInEditor(@Nonnull Caret caret, @Nonnull final Project project) { Editor editor = caret.getEditor(); PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null) return null; int caretOffset = caret.getOffset(); int mostProbablyCorrectLanguageOffset = caretOffset == caret.getSelectionEnd() ? caret.getSelectionStart() : caretOffset; PsiElement elt = getElementAtOffset(file, mostProbablyCorrectLanguageOffset); Language lang = findLanguageFromElement(elt); if (caret.hasSelection()) { final Language rangeLanguage = evaluateLanguageInRange(caret.getSelectionStart(), caret.getSelectionEnd(), file); if (rangeLanguage == null) return file.getLanguage(); lang = rangeLanguage; } return narrowLanguage(lang, file.getLanguage()); }
Example 4
Source File: PsiUtilBase.java From consulo with Apache License 2.0 | 6 votes |
@Nullable public static PsiFile getPsiFileInEditor(@Nonnull Caret caret, @Nonnull final Project project) { Editor editor = caret.getEditor(); final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null) return null; PsiUtilCore.ensureValid(file); final Language language = getLanguageInEditor(caret, project); if (language == null) return file; if (language == file.getLanguage()) return file; int caretOffset = caret.getOffset(); int mostProbablyCorrectLanguageOffset = caretOffset == caret.getSelectionEnd() ? caret.getSelectionStart() : caretOffset; return getPsiFileAtOffset(file, mostProbablyCorrectLanguageOffset); }
Example 5
Source File: BaseFoldingHandler.java From consulo with Apache License 2.0 | 6 votes |
/** * Returns fold regions inside selection, or all regions in editor, if selection doesn't exist or doesn't contain fold regions. */ protected List<FoldRegion> getFoldRegionsForSelection(@Nonnull Editor editor, @Nullable Caret caret) { FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions(); if (caret == null) { caret = editor.getCaretModel().getPrimaryCaret(); } if (caret.hasSelection()) { List<FoldRegion> result = new ArrayList<>(); for (FoldRegion region : allRegions) { if (region.getStartOffset() >= caret.getSelectionStart() && region.getEndOffset() <= caret.getSelectionEnd()) { result.add(region); } } if (!result.isEmpty()) { return result; } } return Arrays.asList(allRegions); }
Example 6
Source File: TextEditorPsiDataProvider.java From consulo with Apache License 2.0 | 5 votes |
private static Language getLanguageAtCurrentPositionInEditor(Caret caret, final PsiFile psiFile) { int caretOffset = caret.getOffset(); int mostProbablyCorrectLanguageOffset = caretOffset == caret.getSelectionStart() || caretOffset == caret.getSelectionEnd() ? caret.getSelectionStart() : caretOffset; if (caret.hasSelection()) { return getLanguageAtOffset(psiFile, mostProbablyCorrectLanguageOffset, caret.getSelectionEnd()); } return PsiUtilCore.getLanguageAtOffset(psiFile, mostProbablyCorrectLanguageOffset); }
Example 7
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 8
Source File: CompletionInitializationContext.java From consulo with Apache License 2.0 | 4 votes |
private static int calcSelectionEnd(Caret caret) { return caret.hasSelection() ? caret.getSelectionEnd() : caret.getOffset(); }
Example 9
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); }