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(); }
public void actionPerformed( AnActionEvent e ) { Editor editor = ( Editor ) e.getDataContext().getData( "editor" ); SelectionModel selectionModel = editor.getSelectionModel(); if ( selectionModel != null ) { String selectedText = selectionModel.getSelectedText(); if ( selectedText != null && selectedText.trim().length() > 0 ) { Project project = ( Project ) e.getDataContext().getData( DataConstants.PROJECT ); String panelid = ( String ) project.getUserData( QuickNotes.KEY_PANELID ); QuickNotesPanel quickNotesPanel = QuickNotesManager.getInstance().getQuickNotesPanel( panelid ); if ( quickNotesPanel != null ) { FileDocumentManager manager = FileDocumentManager.getInstance(); VirtualFile virtualFile = manager.getFile( editor.getDocument() ); quickNotesPanel.addNewNote( "[File: " + virtualFile.getPath() + "]\n" + selectedText ); } } } }
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(); }
@Nullable String getSelectedText(Editor editor) { SelectionModel selectionModel = editor.getSelectionModel(); if(selectionModel.hasSelection()){ return selectionModel.getSelectedText(); }else { final ArrayList<TextRange> ranges = new ArrayList<>(); final int offset = editor.getCaretModel().getOffset(); SelectWordUtilCompat.addWordOrLexemeSelection(false, editor, offset, ranges, SelectWordUtilCompat.JAVASCRIPT_IDENTIFIER_PART_CONDITION); if(ranges.size()>0){ return editor.getDocument().getText(ranges.get(0)); }else { return null; } } }
/** * {@link com.intellij.codeInsight.highlighting.HighlightUsagesHandler#handleNoUsageTargets(PsiFile, * Editor, SelectionModel, Project)} */ private static void handleNoUsageTargets(PsiFile file, @NotNull Editor editor, @NotNull Project project) { if (file.findElementAt(editor.getCaretModel().getOffset()) instanceof PsiWhiteSpace) { return; } final SelectionModel selectionModel = editor.getSelectionModel(); selectionModel.selectWordAtCaret(false); String selection = selectionModel.getSelectedText(); // LOG.assertTrue(selection != null); if (selection != null) { for (int i = 0; i < selection.length(); i++) { if (!Character.isJavaIdentifierPart(selection.charAt(i))) { selectionModel.removeSelection(); } } searchSelection(editor, project); selectionModel.removeSelection(); } }
@Override public void actionPerformed(AnActionEvent e) { //获取编辑器 Editor editor = e.getData(PlatformDataKeys.EDITOR); if (editor != null){ SelectionModel model = editor.getSelectionModel(); //获取选中文本 String selectedText = model.getSelectedText().toString(); if (selectedText!=null){ selectedText = addBlanks(selectedText); try { getTranslation(selectedText); } catch (JSONException e1) { e1.printStackTrace(); } } } }
protected Runnable getReplacementRunnable(final Document document, SelectionModel selectionModel) { final String selectedText = selectionModel.getSelectedText(); final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); final Address address = extractLonLatAndGetAddress(selectedText); Runnable replacementProcess = new Runnable() { @Override public void run() { if (address == null) { return; } document.replaceString( start, end, address.toString()); } }; return replacementProcess; }
@Override @NotNull protected Runnable getReplacementRunnable(final Document document, SelectionModel selectionModel) { final String selectedText = selectionModel.getSelectedText(); final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); final LonLat lonLat = nominatimGeocoder.addressToLonLat(selectedText); return new Runnable() { @Override public void run() { if (lonLat == null) { return; } document.replaceString( start, end, lonLatRenderer.renderLonLat(lonLat)); } }; }
@NotNull public DiffContent[] getContents() { if (myContents != null) return myContents; DiffContent clipboardContent = ClipboardVsValueContents.createClipboardContent(); if (clipboardContent == null) clipboardContent = new SimpleContent(""); myContents = new DiffContent[2]; myContents[0] = clipboardContent; SelectionModel selectionModel = myEditor.getSelectionModel(); if (selectionModel.hasSelection()) { TextRange range = new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); boolean forceReadOnly = myEditor.isViewer(); myContents[1] = new FragmentContent(DiffContent.fromDocument(getProject(), getDocument()), range, getProject(), getDocumentFile(getDocument()), forceReadOnly); } else { myContents [1] = DiffContent.fromDocument(getProject(), getDocument()); } return myContents; }
@Override public void executeWriteAction(@NotNull Editor editor, Caret caret, DataContext dataContext) { final Document document = editor.getDocument(); final int caretOffset = editor.getCaretModel().getOffset(); if (caretOffset < 1) { return; } final SelectionModel selectionModel = editor.getSelectionModel(); final CharSequence text = document.getCharsSequence(); final char c = text.charAt(caretOffset - 1); if (!selectionModel.hasSelection() && StringUtil.isWhiteSpace(c)) { int startOffset = CharArrayUtil.shiftBackward(text, caretOffset - 2, "\t \n") + 1; document.deleteString(startOffset, caretOffset); } else { final EditorActionHandler handler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_BACKSPACE); handler.execute(editor, caret, dataContext); } }
@Override public void executeWriteAction(final Editor editor, DataContext dataContext) { final SelectionModel selectionModel = editor.getSelectionModel(); int changedLines = 0; if (selectionModel.hasSelection()) { changedLines = performAction(editor, new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd())); } else { changedLines += performAction(editor, new TextRange(0, editor.getDocument().getTextLength())); } if (changedLines == 0) { HintManager.getInstance().showInformationHint(editor, "All lines already have requested indentation"); } else { HintManager.getInstance().showInformationHint(editor, "Changed indentation in " + changedLines + (changedLines == 1 ? " line" : " lines")); } }
@Override public void execute(final Editor editor, final DataContext dataContext) { SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) { return; } final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); if (start >= end) { return; } KillRingUtil.copyToKillRing(editor, start, end, false); if (myRemove) { ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(editor.getDocument(),editor.getProject()) { @Override public void run() { editor.getDocument().deleteString(start, end); } }); } }
private static void handleNoUsageTargets(PsiFile file, @NotNull Editor editor, SelectionModel selectionModel, @NotNull 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(); }
protected static List<TextRange> getSelectedRanges(@NotNull SelectionModel selectionModel) { final List<TextRange> ranges = new SmartList<TextRange>(); if (selectionModel.hasSelection()) { TextRange range = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); ranges.add(range); } else if (selectionModel.hasBlockSelection()) { int[] starts = selectionModel.getBlockSelectionStarts(); int[] ends = selectionModel.getBlockSelectionEnds(); for (int i = 0; i < starts.length; i++) { ranges.add(TextRange.create(starts[i], ends[i])); } } return ranges; }
@Nullable private static String getSingleLineSelection(@NotNull Editor editor) { final SelectionModel model = editor.getSelectionModel(); final Document document = editor.getDocument(); if (!model.hasSelection()) { final int offset = editor.getCaretModel().getOffset(); if (offset <= document.getTextLength()) { final int lineNumber = document.getLineNumber(offset); final String line = document.getText().substring(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lineNumber)).trim(); if (StringUtil.isNotEmpty(line)) { return line; } } return null; } final int start = model.getSelectionStart(); final int end = model.getSelectionEnd(); if (document.getLineNumber(start) == document.getLineNumber(end)) { final String selection = document.getText().substring(start, end).trim(); if (StringUtil.isNotEmpty(selection)) { return selection; } } return null; }
public void performReplaceAll(Editor e) { if (!ReadonlyStatusHandler.ensureDocumentWritable(e.getProject(), e.getDocument())) return; if (mySearchResults.getFindModel() != null) { final FindModel copy = new FindModel(); copy.copyFrom(mySearchResults.getFindModel()); final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel(); final int offset; if (!selectionModel.hasSelection() || copy.isGlobal()) { copy.setGlobal(true); offset = 0; } else { offset = selectionModel.getBlockSelectionStarts()[0]; } FindUtil.replace(e.getProject(), e, offset, copy, this); } }
@Override public void execute(Editor editor, DataContext dataContext) { final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { final TemplateState state = TemplateManagerImpl.getTemplateState(editor); if (state != null && editor.getUserData(InplaceRefactoring.INPLACE_RENAMER) != null) { final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor); if (lookup != null) { selectionModel.removeSelection(); lookup.hideLookup(true); return; } } } myOriginalHandler.execute(editor, dataContext); }
private void calculateSelectedTextRectangle() { SelectionModel selectionModel = myEditor.getSelectionModel(); int selectionStartOffset = selectionModel.getSelectionStart(); int selectionEndOffset = selectionModel.getSelectionEnd(); VisualPosition maxColumnVp = getMaxColumnInsideRange(selectionStartOffset, selectionEndOffset); Point maxColumnsPoint = myEditor.visualPositionToXY(maxColumnVp); Point selectionStart = myEditor.visualPositionToXY(myEditor.offsetToVisualPosition(selectionStartOffset)); Point selectionEnd = myEditor.visualPositionToXY(myEditor.offsetToVisualPosition(selectionEndOffset)); selectionStart = SwingUtilities.convertPoint(myContentComponent, selectionStart, myEditorComponent); selectionEnd = SwingUtilities.convertPoint(myContentComponent, selectionEnd, myEditorComponent); maxColumnsPoint = SwingUtilities.convertPoint(myContentComponent, maxColumnsPoint, myEditorComponent); mySelectionStartY = selectionStart.y; mySelectionEndY = selectionEnd.y; myTextRangeMaxColumnX = maxColumnsPoint.x; }
private static SelectionContainer getSelectionLenContainer(@NotNull final Editor editor, @NotNull final MyLineRange toMove) { final SelectionModel selectionModel = editor.getSelectionModel(); final PsiElement startToMove = toMove.myStartElement; final PsiElement endToMove = toMove.myEndElement; final int selectionStart = selectionModel.getSelectionStart(); final int selectionEnd = selectionModel.getSelectionEnd(); final TextRange range = startToMove.getTextRange(); final int column = editor.offsetToLogicalPosition(selectionStart).column; final int additionalSelection = range.getStartOffset() > selectionStart ? range.getStartOffset() - selectionStart : 0; if (startToMove == endToMove) return new SelectionContainer(selectionEnd - range.getStartOffset(), additionalSelection, column == 0); int len = range.getStartOffset() <= selectionStart ? range.getEndOffset() - selectionStart : startToMove.getTextLength(); PsiElement tmp = startToMove.getNextSibling(); while (tmp != endToMove && tmp != null) { if (!(tmp instanceof PsiWhiteSpace)) len += tmp.getTextLength(); tmp = tmp.getNextSibling(); } len = len + selectionEnd - endToMove.getTextOffset(); return new SelectionContainer(len, additionalSelection, column == 0); }
private static boolean isTestClass(PsiFile file, Editor editor) { PsiElement element1 = null; final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { element1 = file.findElementAt(selectionModel.getSelectionStart()); } else { final CaretModel caretModel = editor.getCaretModel(); final Document document = editor.getDocument(); int lineNumber = document.getLineNumber(caretModel.getOffset()); if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) { element1 = file.findElementAt(document.getLineStartOffset(lineNumber)); } } if (element1 != null) { final PyClass clazz = PyUtil.getContainingClassOrSelf(element1); if (clazz != null && PythonUnitTestUtil.isTestCaseClass(clazz)) return true; } return false; }
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) { PsiElement element1 = null; PsiElement element2 = null; final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { element1 = file.findElementAt(selectionModel.getSelectionStart()); element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1); } else { final CaretModel caretModel = editor.getCaretModel(); final Document document = editor.getDocument(); int lineNumber = document.getLineNumber(caretModel.getOffset()); if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) { element1 = file.findElementAt(document.getLineStartOffset(lineNumber)); element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1); } } if (element1 == null || element2 == null) { CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.introduce.selection.error"), getTitle(), "members.pull.up"); return; } doRefactor(project, element1, element2, editor, file, dataContext); }
@Nullable private static TextRange getNonEmptySelectionRange(Editor editor) { if (editor != null) { final SelectionModel model = editor.getSelectionModel(); if (model.hasSelection()) { final int start = model.getSelectionStart(); final int end = model.getSelectionEnd(); if (start < end) { return TextRange.create(start, end); } } } return null; }
@Override public void actionPerformed(AnActionEvent e) { final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext()); PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext()); if (editor == null || file == null) return; final Project project = file.getProject(); CommandProcessorEx commandProcessor = (CommandProcessorEx)CommandProcessorEx.getInstance(); Object commandToken = commandProcessor.startCommand(project, e.getPresentation().getText(), e.getPresentation().getText(), UndoConfirmationPolicy.DEFAULT); AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass()); try { final SelectionModel selectionModel = editor.getSelectionModel(); int[] starts = selectionModel.getBlockSelectionStarts(); int[] ends = selectionModel.getBlockSelectionEnds(); int startOffset = starts.length == 0? 0 : starts[0]; int endOffset = ends.length == 0? editor.getDocument().getTextLength() : ends[ends.length - 1]; perform(project, editor.getDocument(), startOffset, endOffset); } finally { token.finish(); commandProcessor.finishCommand(project, commandToken, null); } }
@Override public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable DataContext dataContext) { editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); final SelectionModel model = editor.getSelectionModel(); if (model.hasSelection()) { invokeImpl(project, editor, file, model.getSelectionStart(), model.getSelectionEnd()); } else { final List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(file, editor, editor.getCaretModel().getOffset(), true); final Pass<GrExpression> callback = new Callback(project, editor, file); if (expressions.size() == 1) { callback.pass(expressions.get(0)); } else if (expressions.isEmpty()) { model.selectLineAtCaret(); invokeImpl(project, editor, file, model.getSelectionStart(), model.getSelectionEnd()); } else { IntroduceTargetChooser.showChooser(editor, expressions, callback, GrIntroduceHandlerBase.GR_EXPRESSION_RENDERER); } } }
@Override public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable final DataContext dataContext) { final SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) { final int offset = editor.getCaretModel().getOffset(); final List<GrExpression> expressions = collectExpressions(file, editor, offset, false); if (expressions.isEmpty()) { updateSelectionForVariable(editor, file, selectionModel, offset); } else if (expressions.size() == 1) { final TextRange textRange = expressions.get(0).getTextRange(); selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset()); } else { IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() { @Override public void pass(final GrExpression selectedValue) { invoke(project, editor, file, selectedValue.getTextRange().getStartOffset(), selectedValue.getTextRange().getEndOffset()); } }, GR_EXPRESSION_RENDERER); return; } } invoke(project, editor, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); }
@Override public void checkApplicability(final PsiFile psiFile, final Editor editor) throws IncorrectOperationException { PsiLiteralExpression literalExpression = I18nizeAction.getEnclosingStringLiteral(psiFile, editor); if (literalExpression != null) { SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) return; int start = selectionModel.getSelectionStart(); int end = selectionModel.getSelectionEnd(); TextRange textRange = literalExpression.getTextRange(); if (textRange.contains(start) && textRange.contains(end)) { mySelectionRange = new TextRange(start, end); return; } } String message = CodeInsightBundle.message("i18nize.error.message"); throw new IncorrectOperationException(message); }
@Override public boolean satisfiedBy(PsiElement element, @Nullable Editor editor) { if (editor == null) { return false; } final SelectionModel selectionModel = editor.getSelectionModel(); final Document document = editor.getDocument(); if (selectionModel.hasSelection()) { final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); if (start < 0 || end < 0 || start > end) { // shouldn't happen but http://ea.jetbrains.com/browser/ea_problems/50192 return false; } final String text = document.getCharsSequence().subSequence(start, end).toString(); return indexOfUnicodeEscape(text, 1) >= 0; } else { final CaretModel caretModel = editor.getCaretModel(); final int lineNumber = document.getLineNumber(caretModel.getOffset()); final String line = document.getText(new TextRange(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lineNumber))); final int column = caretModel.getLogicalPosition().column; final int index = indexOfUnicodeEscape(line, column); return index >= 0 && column >= index; } }
public void replaceSelection(@Nonnull final String clipboardText) { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { @Override public void run() { final SelectionModel model = Assertions.assertNotNull(getEditor()).getSelectionModel(); final int start = model.getSelectionStart(); final int end = model.getSelectionEnd(); getDocument().replaceString(start, end, ""); getDocument().insertString(start, clipboardText); } },null, null, UndoConfirmationPolicy.DEFAULT, getDocument()); } }); }
@Override public void actionPerformed(@NotNull AnActionEvent e) { final MCSettingsProvider settings = MCSettingsProvider.getInstance(e.getProject()); final Editor editor = e.getData(LangDataKeys.EDITOR); if (editor == null) return; final SelectionModel selectionModel = editor.getSelectionModel(); String selectedText; if (selectionModel.hasSelection()) { selectedText = selectionModel.getSelectedText(); } else { selectionModel.selectLineAtCaret(); if (selectionModel.hasSelection()) { selectedText = selectionModel.getSelectedText(); selectionModel.removeSelection(); } else { return; } } MayaCommInterface maya = new MayaCommInterface(settings.getHost(), settings.getPort()); maya.connectMayaLog(); maya.sendCodeToMaya(selectedText); }
private String getSelectedText(SelectionModel selectionModel, Document document) { VisualPosition start = selectionModel.getSelectionStartPosition(); VisualPosition end = selectionModel.getSelectionEndPosition(); String lines[] = document.getText().split("\n"); List<String> contentLines = new ArrayList<String>(); for (int i = start.getLine(); i <= end.getLine(); i++) { contentLines.add(lines[i]); } int diff = contentLines.get(0).length() - contentLines.get(0).trim().length(); if (diff > 0) { for (int i = 0; i < contentLines.size(); i++) { contentLines.set(i, this.leftTrim(contentLines.get(i), diff)); } } return this.arrayToStringConversion(contentLines); }
public void alignText(Editor editor) { String regex = Messages.showInputDialog(editor.getContentComponent(), "Enter regex to align with:", "Front End Alignment", null); SelectionModel selectionModel = editor.getSelectionModel(); if(selectionModel.getSelectedText() != null) { Document document = editor.getDocument(); final int startOffset = document.getLineStartOffset(document.getLineNumber(selectionModel.getSelectionStart())); final int endOffset = document.getLineEndOffset(document.getLineNumber(selectionModel.getSelectionEnd())); final String text = document.getText(new TextRange(startOffset, endOffset)); final Editor editorArg = editor; final String newText = (new RegexTextAligner(text, regex)).alignText(); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { editorArg.getDocument().replaceString(startOffset, endOffset, newText); } }); } }
protected List<PsiElement> getPsiElements(Class stepClass) { SelectionModel selectionModel = editor.getSelectionModel(); List<PsiElement> specSteps = new ArrayList<>(); int currentOffset = selectionModel.getSelectionStart(); while (selectionModel.getSelectionEnd() >= currentOffset) { try { if (psiFile.getText().charAt(currentOffset++) == '\n') continue; PsiElement step = getStep(psiFile.findElementAt(currentOffset), stepClass); if (step == null) return new ArrayList<>(); specSteps.add(step); currentOffset += step.getText().length(); } catch (Exception ignored) { } } return specSteps; }
/** * 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); }
/** * 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); }
@NotNull public DiffContent[] getContents() { if (myContents != null) return myContents; DiffContent clipboardContent = ClipboardVsValueContents.createClipboardContent(); if (clipboardContent == null) clipboardContent = new SimpleContent(""); myContents = new DiffContent[2]; myContents[0] = clipboardContent; SelectionModel selectionModel = myEditor.getSelectionModel(); if (selectionModel.hasSelection()) { TextRange range = new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); myContents[1] = new FragmentContent(DiffContent.fromDocument(getProject(), getDocument()), range, getProject(), getDocumentFile(getDocument())); } else { myContents [1] = DiffContent.fromDocument(getProject(), getDocument()); } return myContents; }
@Override public void executeWriteAction(final Editor editor, DataContext dataContext) { final SelectionModel selectionModel = editor.getSelectionModel(); int changedLines = 0; if (selectionModel.hasSelection()) { changedLines = performAction(editor, new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd())); } else if (selectionModel.hasBlockSelection()) { final int[] starts = selectionModel.getBlockSelectionStarts(); final int[] ends = selectionModel.getBlockSelectionEnds(); for (int i = 0; i < starts.length; i++) { changedLines += performAction(editor, new TextRange(starts [i], ends [i])); } } else { changedLines += performAction(editor, new TextRange(0, editor.getDocument().getTextLength())); } if (changedLines == 0) { HintManager.getInstance().showInformationHint(editor, "All lines already have requested indentation"); } else { HintManager.getInstance().showInformationHint(editor, "Changed indentation in " + changedLines + (changedLines == 1 ? " line" : " lines")); } }
@Override public void execute(Editor editor, DataContext dataContext) { if (!(editor instanceof EditorEx)) { return; } final SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) { return; } EditorEx editorEx = (EditorEx)editor; final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); final CaretModel caretModel = editor.getCaretModel(); boolean moveToEnd = caretModel.getOffset() == start; editorEx.setStickySelection(false); editorEx.setStickySelection(true); if (moveToEnd) { caretModel.moveToOffset(end); } else { caretModel.moveToOffset(start); } }
@Nullable public static Language getLanguageInEditor(@NotNull final Editor editor, @NotNull final Project project) { PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null) return null; final SelectionModel selectionModel = editor.getSelectionModel(); int caretOffset = editor.getCaretModel().getOffset(); int mostProbablyCorrectLanguageOffset = caretOffset == selectionModel.getSelectionStart() || caretOffset == selectionModel.getSelectionEnd() ? selectionModel.getSelectionStart() : caretOffset; PsiElement elt = getElementAtOffset(file, mostProbablyCorrectLanguageOffset); Language lang = findLanguageFromElement(elt); if (selectionModel.hasSelection()) { final Language rangeLanguage = evaluateLanguageInRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), file); if (rangeLanguage == null) return file.getLanguage(); lang = rangeLanguage; } return narrowLanguage(lang, file.getLanguage()); }
@Nullable public static PsiFile getPsiFileInEditor(@NotNull final Editor editor, @NotNull final Project project) { final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null) return null; final Language language = getLanguageInEditor(editor, project); if (language == null) return file; if (language == file.getLanguage()) return file; final SelectionModel selectionModel = editor.getSelectionModel(); int caretOffset = editor.getCaretModel().getOffset(); int mostProbablyCorrectLanguageOffset = caretOffset == selectionModel.getSelectionStart() || caretOffset == selectionModel.getSelectionEnd() ? selectionModel.getSelectionStart() : caretOffset; return getPsiFileAtOffset(file, mostProbablyCorrectLanguageOffset); }