Java Code Examples for com.intellij.openapi.editor.SelectionModel#getSelectedText()
The following examples show how to use
com.intellij.openapi.editor.SelectionModel#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: GoogleTranslation.java From GoogleTranslation with Apache License 2.0 | 6 votes |
private void getTranslation(AnActionEvent event) { Editor editor = event.getData(PlatformDataKeys.EDITOR); if (editor == null) { return; } SelectionModel model = editor.getSelectionModel(); String selectedText = model.getSelectedText(); if (TextUtils.isEmpty(selectedText)) { selectedText = getCurrentWords(editor); if (TextUtils.isEmpty(selectedText)) { return; } } String queryText = strip(addBlanks(selectedText)); new Thread(new RequestRunnable(mTranslator, editor, queryText)).start(); }
Example 2
Source File: SearchActionBase.java From sourcegraph-jetbrains with Apache License 2.0 | 6 votes |
@Nullable private String getSelectedText(Project project) { Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor == null) { return null; } Document currentDoc = editor.getDocument(); if (currentDoc == null) { return null; } VirtualFile currentFile = FileDocumentManager.getInstance().getFile(currentDoc); if (currentFile == null) { return null; } SelectionModel sel = editor.getSelectionModel(); return sel.getSelectedText(); }
Example 3
Source File: ReciteWords.java From ReciteWords with MIT License | 6 votes |
private void getTranslation(AnActionEvent event) { Editor mEditor = event.getData(PlatformDataKeys.EDITOR); Project project = event.getData(PlatformDataKeys.PROJECT); String basePath = project.getBasePath(); if (null == mEditor) { return; } SelectionModel model = mEditor.getSelectionModel(); String selectedText = model.getSelectedText(); if (TextUtils.isEmpty(selectedText)) { selectedText = getCurrentWords(mEditor); if (TextUtils.isEmpty(selectedText)) { return; } } String queryText = strip(addBlanks(selectedText)); new Thread(new RequestRunnable(mEditor, queryText,basePath)).start(); }
Example 4
Source File: AbstractStringManipAction.java From StringManipulation with Apache License 2.0 | 6 votes |
protected void executeMyWriteActionPerCaret(Editor editor, Caret caret, Map<String, Object> actionContext, DataContext dataContext, T additionalParam) { final SelectionModel selectionModel = editor.getSelectionModel(); String selectedText = selectionModel.getSelectedText(); if (selectedText == null) { selectSomethingUnderCaret(editor, dataContext, selectionModel); selectedText = selectionModel.getSelectedText(); if (selectedText == null) { return; } } String s = transformSelection(editor, actionContext, dataContext, selectedText, additionalParam); s = s.replace("\r\n", "\n"); s = s.replace("\r", "\n"); editor.getDocument().replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), s); }
Example 5
Source File: HighlightUsagesHandler.java From consulo with Apache License 2.0 | 6 votes |
private static void handleNoUsageTargets(PsiFile file, @Nonnull Editor editor, SelectionModel selectionModel, @Nonnull Project project) { if (file.findElementAt(editor.getCaretModel().getOffset()) instanceof PsiWhiteSpace) return; selectionModel.selectWordAtCaret(false); String selection = selectionModel.getSelectedText(); LOG.assertTrue(selection != null); for (int i = 0; i < selection.length(); i++) { if (!Character.isJavaIdentifierPart(selection.charAt(i))) { selectionModel.removeSelection(); } } doRangeHighlighting(editor, project); selectionModel.removeSelection(); }
Example 6
Source File: LogView.java From logviewer with Apache License 2.0 | 5 votes |
/** * Returns the selected text if there is any selection. If not return all based on parameter * * @param defaultToAll If no selection, then this decides whether to return all text */ String getSelectedText(boolean defaultToAll) { ConsoleView console = this.getConsole(); Editor myEditor = console != null ? (Editor) CommonDataKeys.EDITOR.getData((DataProvider) console) : null; if (myEditor != null) { Document document = myEditor.getDocument(); final SelectionModel selectionModel = myEditor.getSelectionModel(); if (selectionModel.hasSelection()) { return selectionModel.getSelectedText(); } else if (defaultToAll) { return document.getText(); } } return null; }
Example 7
Source File: AbstractCaseConvertingAction.java From StringManipulation with Apache License 2.0 | 5 votes |
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 8
Source File: AbstractStringManipAction.java From StringManipulation with Apache License 2.0 | 5 votes |
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 9
Source File: Selector.java From translator with MIT License | 4 votes |
private String getAutoSelectedText(SelectionModel selectionModel){ selectionModel.selectWordAtCaret(false); return selectionModel.getSelectedText(); }
Example 10
Source File: ExtractRuleAction.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 4 votes |
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. }