static EditorWriteActionHandler createExpandTemplateHandler(final char shortcutChar) { return new EditorWriteActionHandler(true) { @Override public void executeWriteAction(Editor editor, @Nullable Caret caret, DataContext dataContext) { Project project = editor.getProject(); assert project != null; TemplateManager.getInstance(project).startTemplate(editor, shortcutChar); } @Override protected boolean isEnabledForCaret(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) { Project project = editor.getProject(); return project != null && ((TemplateManagerImpl)TemplateManager.getInstance(project)).prepareTemplate(editor, shortcutChar, null) != null; } }; }
@Override protected final void doExecute(final Editor editor, @Nullable final Caret caret, final DataContext dataContext) { final Pair<Boolean, T> additionalParameter = beforeWriteAction(editor, dataContext); if (!additionalParameter.first) { return; } final Runnable runnable = new Runnable() { @Override public void run() { executeWriteAction(editor, caret, dataContext, additionalParameter.second); } }; new EditorWriteActionHandler(false) { @Override public void executeWriteAction(Editor editor1, @Nullable Caret caret1, DataContext dataContext1) { runnable.run(); } }.doExecute(editor, caret, dataContext); }
public static EditorWriteActionHandler createExpandTemplateHandler(final char shortcutChar) { return new EditorWriteActionHandler(true) { @Override public void executeWriteAction(Editor editor, @Nullable Caret caret, DataContext dataContext) { Project project = editor.getProject(); assert project != null; TemplateManager.getInstance(project).startTemplate(editor, shortcutChar); } @Override protected boolean isEnabledForCaret(@Nonnull Editor editor, @Nonnull Caret caret, DataContext dataContext) { Project project = editor.getProject(); return project != null && ((TemplateManagerImpl)TemplateManager.getInstance(project)).prepareTemplate(editor, shortcutChar, null) != null; } }; }
public RemoveEmptyLinesAction() { super(new EditorWriteActionHandler(true) { @Override public void executeWriteAction(Editor editor, DataContext dataContext) { // Column mode not supported if (editor.isColumnMode()) { return; } final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { final String selectedText = selectionModel.getSelectedText(); String[] textParts = selectedText.split("\n"); Collection<String> result = new ArrayList<String>(); for (String textPart : textParts) { if (StringUtils.isNotBlank(textPart)) { result.add(textPart); } } String[] res = result.toArray(new String[result.size()]); final String s = StringUtils.join(res, '\n'); editor.getDocument().replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), s); } } }); }
public DecrementAction() { super(null); this.setupHandler(new EditorWriteActionHandler(true) { @Override public void executeWriteAction(Editor editor, DataContext dataContext) { // Column mode not supported if (editor.isColumnMode()) { return; } final CaretModel caretModel = editor.getCaretModel(); final int line = caretModel.getLogicalPosition().line; final int column = caretModel.getLogicalPosition().column; int caretOffset = caretModel.getOffset(); final SelectionModel selectionModel = editor.getSelectionModel(); boolean hasSelection = selectionModel.hasSelection(); if (hasSelection == false) { selectionModel.selectLineAtCaret(); } final String selectedText = selectionModel.getSelectedText(); if (selectedText != null) { String[] textParts = StringUtil.splitPreserveAllTokens(selectedText, DuplicatUtils.SIMPLE_NUMBER_REGEX); for (int i = 0; i < textParts.length; i++) { textParts[i] = DuplicatUtils.simpleDec(textParts[i]); } final String newText = StringUtils.join(textParts); applyChanges(editor, caretModel, line, column, selectionModel, hasSelection, newText, caretOffset); } } }); }
public IncrementAction() { super(null); this.setupHandler(new EditorWriteActionHandler(true) { @Override public void executeWriteAction(Editor editor, DataContext dataContext) { // Column mode not supported if (editor.isColumnMode()) { return; } final CaretModel caretModel = editor.getCaretModel(); final int line = caretModel.getLogicalPosition().line; final int column = caretModel.getLogicalPosition().column; int caretOffset = caretModel.getOffset(); final SelectionModel selectionModel = editor.getSelectionModel(); boolean hasSelection = selectionModel.hasSelection(); if (hasSelection == false) { selectionModel.selectLineAtCaret(); } final String selectedText = selectionModel.getSelectedText(); if (selectedText != null) { String[] textParts = StringUtil.splitPreserveAllTokens(selectedText, DuplicatUtils.SIMPLE_NUMBER_REGEX); for (int i = 0; i < textParts.length; i++) { textParts[i] = DuplicatUtils.simpleInc(textParts[i]); } final String newText = StringUtils.join(textParts); applyChanges(editor, caretModel, line, column, selectionModel, hasSelection, selectedText, newText, caretOffset); } } }); }
@Override @RequiredWriteAction public void handleInsert(final InsertionContext context, final T item) { final Editor editor = context.getEditor(); final Document document = editor.getDocument(); context.commitDocument(); PsiElement elementAt = context.getFile().findElementAt(context.getStartOffset()); handleInsertImpl(context, item, editor, document); if(myOpenChar == '{') { document.insertString(editor.getCaretModel().getOffset(), "\n"); } context.commitDocument(); if(elementAt != null) { PsiElement parent = elementAt.getParent(); CodeStyleManager.getInstance(elementAt.getProject()).reformat(parent); if(myOpenChar == '{') { EditorWriteActionHandler actionHandler = (EditorWriteActionHandler) EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER); actionHandler.executeWriteAction(editor, DataManager.getInstance().getDataContext(editor.getContentComponent())); } } }
static void extend(CompletionContributor contributor) { contributor.extend(CompletionType.BASIC, psiElement().andNot(psiElement().inside(DotNetXXXAccessor.class)), new CompletionProvider() { @RequiredReadAction @Override public void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet resultSet) { PsiElement position = completionParameters.getPosition(); final CSharpXXXAccessorOwner accessorOwner = PsiTreeUtil.getParentOfType(position, CSharpXXXAccessorOwner.class); if(accessorOwner == null) { return; } PsiElement leftBrace = accessorOwner.getLeftBrace(); if(leftBrace == null) { return; } int textOffset = position.getTextOffset(); PsiElement rightBrace = accessorOwner.getRightBrace(); int rightTextRange = rightBrace == null ? -1 : rightBrace.getTextOffset(); if((rightTextRange == -1 || textOffset < rightTextRange) && textOffset > leftBrace.getTextOffset()) { if(accessorOwner.hasModifier(DotNetModifier.ABSTRACT)) { buildAccessorKeywordsCompletion(resultSet, accessorOwner, null); } else { buildAccessorKeywordsCompletion(resultSet, accessorOwner, new InsertHandler<LookupElement>() { @Override @RequiredWriteAction public void handleInsert(InsertionContext context, LookupElement item) { if(context.getCompletionChar() == '{') { context.setAddCompletionChar(false); Editor editor = context.getEditor(); CaretModel caretModel = editor.getCaretModel(); int offset = caretModel.getOffset(); context.getDocument().insertString(offset, "{\n}"); caretModel.moveToOffset(offset + 1); PsiElement elementAt = context.getFile().findElementAt(offset - 1); context.commitDocument(); DotNetXXXAccessor accessor = PsiTreeUtil.getParentOfType(elementAt, DotNetXXXAccessor.class); if(accessor != null) { CodeStyleManager.getInstance(context.getProject()).reformat(accessor); } EditorWriteActionHandler actionHandler = (EditorWriteActionHandler) EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER); actionHandler.executeWriteAction(editor, DataManager.getInstance().getDataContext(editor.getContentComponent())); } } }); } } } }); }