Example usage for com.intellij.openapi.editor.actionSystem EditorWriteActionHandler executeWriteAction

List of usage examples for com.intellij.openapi.editor.actionSystem EditorWriteActionHandler executeWriteAction

Introduction

In this page you can find the example usage for com.intellij.openapi.editor.actionSystem EditorWriteActionHandler executeWriteAction.

Prototype

@Deprecated
public void executeWriteAction(Editor editor, DataContext dataContext) 

Source Link

Usage

From source file:org.mustbe.consulo.csharp.ide.completion.CSharpAccessorCompletionContributor.java

License:Apache License

public CSharpAccessorCompletionContributor() {
    extend(CompletionType.BASIC, psiElement().andNot(psiElement().inside(DotNetXXXAccessor.class)),
            new CompletionProvider() {
                @RequiredReadAction/*from   w  w  w .j  ava  2  s. c o  m*/
                @Override
                protected 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()));
                                            }
                                        }
                                    });
                        }
                    }
                }
            });
}

From source file:org.mustbe.consulo.csharp.ide.completion.util.ExpressionOrStatementInsertHandler.java

License:Apache License

@Override
@RequiredWriteAction//from   www  .j ava2  s. co  m
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()));
        }
    }
}