@Override public void executeWriteAction(final Editor editor, DataContext dataContext) { if(!editor.getSelectionModel().hasSelection(true)) { if (Registry.is(CopyAction.SKIP_COPY_AND_CUT_FOR_EMPTY_SELECTION_KEY)) { return; } editor.getCaretModel().runForEachCaret(new CaretAction() { @Override public void perform(Caret caret) { editor.getSelectionModel().selectLineAtCaret(); } }); } editor.getSelectionModel().copySelectionToClipboard(); EditorModificationUtil.deleteSelectedTextForAllCarets(editor); }
@Override public void execute(final Editor editor, DataContext dataContext) { if (!editor.getSelectionModel().hasSelection(true)) { if (Registry.is(SKIP_COPY_AND_CUT_FOR_EMPTY_SELECTION_KEY)) { return; } editor.getCaretModel().runForEachCaret(new CaretAction() { @Override public void perform(Caret caret) { editor.getSelectionModel().selectLineAtCaret(); EditorActionUtil.moveCaretToLineStartIgnoringSoftWraps(editor); } }); } editor.getSelectionModel().copySelectionToClipboard(); }
private static void iterateOverCarets(@NotNull final Project project, @NotNull final Editor hostEditor, @NotNull final MultiCaretCodeInsightActionHandler handler) { PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); final PsiFile psiFile = documentManager.getCachedPsiFile(hostEditor.getDocument()); documentManager.commitAllDocuments(); hostEditor.getCaretModel().runForEachCaret(new CaretAction() { @Override public void perform(Caret caret) { Editor editor = hostEditor; if (psiFile != null) { Caret injectedCaret = InjectedLanguageUtil.getCaretForInjectedLanguageNoCommit(caret, psiFile); if (injectedCaret != null) { caret = injectedCaret; editor = caret.getEditor(); } } final PsiFile file = PsiUtilBase.getPsiFileInEditor(caret, project); if (file != null) { handler.invoke(project, editor, caret, file); } } }); }
@RequiredWriteAction @Override public void executeWriteAction(final Editor editor, DataContext dataContext) { if(!editor.getSelectionModel().hasSelection(true)) { if (Registry.is(CopyAction.SKIP_COPY_AND_CUT_FOR_EMPTY_SELECTION_KEY)) { return; } editor.getCaretModel().runForEachCaret(new CaretAction() { @Override public void perform(Caret caret) { editor.getSelectionModel().selectLineAtCaret(); } }); } editor.getSelectionModel().copySelectionToClipboard(); EditorModificationUtil.deleteSelectedTextForAllCarets(editor); }
private static void iterateOverCarets(@Nonnull final Project project, @Nonnull final Editor hostEditor, @Nonnull final MultiCaretCodeInsightActionHandler handler) { PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); final PsiFile psiFile = documentManager.getCachedPsiFile(hostEditor.getDocument()); documentManager.commitAllDocuments(); hostEditor.getCaretModel().runForEachCaret(new CaretAction() { @Override public void perform(Caret caret) { Editor editor = hostEditor; if (psiFile != null) { Caret injectedCaret = InjectedLanguageUtil.getCaretForInjectedLanguageNoCommit(caret, psiFile); if (injectedCaret != null) { caret = injectedCaret; editor = caret.getEditor(); } } final PsiFile file = PsiUtilBase.getPsiFileInEditor(caret, project); if (file != null) { handler.invoke(project, editor, caret, file); } } }); }
public void perform() { final Document document = myEditor.getDocument(); final VirtualFile file = FileDocumentManager.getInstance().getFile(document); if (file != null) { ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(file); } CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { public void run() { myEditor.getCaretModel().runForEachCaret(new CaretAction() { public void perform(Caret caret) { // adjust the selection so that it starts with a non-whitespace character (to make sure that the template is inserted // at a meaningful position rather than at indent 0) if (myEditor.getSelectionModel().hasSelection() && myTemplate.isToReformat()) { int offset = myEditor.getSelectionModel().getSelectionStart(); int selectionEnd = myEditor.getSelectionModel().getSelectionEnd(); int lineEnd = document.getLineEndOffset(document.getLineNumber(offset)); while (offset < lineEnd && offset < selectionEnd && (document.getCharsSequence().charAt(offset) == ' ' || document.getCharsSequence().charAt(offset) == '\t')) { offset++; } // avoid extra line break after $SELECTION$ in case when selection ends with a complete line if (selectionEnd == document.getLineStartOffset(document.getLineNumber(selectionEnd))) { selectionEnd--; } if (offset < lineEnd && offset < selectionEnd) { // found non-WS character in first line of selection myEditor.getSelectionModel().setSelection(offset, selectionEnd); } } String selectionString = myEditor.getSelectionModel().getSelectedText(); TemplateManager.getInstance(myProject).startTemplate(myEditor, selectionString, myTemplate); } }); } }, "Wrap with template", "Wrap with template " + myTemplate.getKey()); }
protected void executeMyWriteAction(Editor editor, final DataContext dataContext, final T additionalParam) { editor.getCaretModel().runForEachCaret(new CaretAction() { @Override public void perform(Caret caret) { executeMyWriteActionPerCaret(caret.getEditor(), caret, dataContext, additionalParam); } }); }
private static void runForCaret(Editor editor, Caret caret, CaretAction action) { if (caret == null) { editor.getCaretModel().runForEachCaret(action); } else { action.perform(caret); } }
public void perform() { final Document document = myEditor.getDocument(); final VirtualFile file = FileDocumentManager.getInstance().getFile(document); if (file != null) { ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(file); } CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { @Override public void run() { myEditor.getCaretModel().runForEachCaret(new CaretAction() { @Override public void perform(Caret caret) { // adjust the selection so that it starts with a non-whitespace character (to make sure that the template is inserted // at a meaningful position rather than at indent 0) if (myEditor.getSelectionModel().hasSelection() && myTemplate.isToReformat()) { int offset = myEditor.getSelectionModel().getSelectionStart(); int selectionEnd = myEditor.getSelectionModel().getSelectionEnd(); int lineEnd = document.getLineEndOffset(document.getLineNumber(offset)); while (offset < lineEnd && offset < selectionEnd && (document.getCharsSequence().charAt(offset) == ' ' || document.getCharsSequence().charAt(offset) == '\t')) { offset++; } // avoid extra line break after $SELECTION$ in case when selection ends with a complete line if (selectionEnd == document.getLineStartOffset(document.getLineNumber(selectionEnd))) { selectionEnd--; } if (offset < lineEnd && offset < selectionEnd) { // found non-WS character in first line of selection myEditor.getSelectionModel().setSelection(offset, selectionEnd); } } String selectionString = myEditor.getSelectionModel().getSelectedText(); TemplateManager.getInstance(myProject).startTemplate(myEditor, selectionString, myTemplate); } }); } }, "Wrap with template", "Wrap with template " + myTemplate.getKey()); }
@Override protected void executeInLookup(LookupImpl lookup, DataContext context, final Caret caret) { final Editor editor = lookup.getEditor(); final int offset = editor.getCaretModel().getOffset(); final CharSequence seq = editor.getDocument().getCharsSequence(); if (seq.length() <= offset || !lookup.isCompletion()) { myOriginalHandler.execute(editor, caret, context); return; } char c = seq.charAt(offset); CharFilter.Result lookupAction = LookupTypedHandler.getLookupAction(c, lookup); if (lookupAction != CharFilter.Result.ADD_TO_PREFIX || Character.isWhitespace(c)) { myOriginalHandler.execute(editor, caret, context); return; } if (!lookup.performGuardedChange(() -> { CaretAction action = new CaretAction() { @Override public void perform(Caret caret1) { caret1.removeSelection(); int caretOffset = caret1.getOffset(); if (caretOffset < seq.length()) { caret1.moveToOffset(caretOffset + 1); } } }; if (caret == null) { editor.getCaretModel().runForEachCaret(action); } else { action.perform(caret); } })) { return; } lookup.appendPrefix(c); final CompletionProgressIndicator completion = CompletionServiceImpl.getCompletionService().getCurrentCompletion(); if (completion != null) { completion.prefixUpdated(); } }