com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler Java Examples

The following examples show how to use com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler. 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: MyEditorWriteActionHandler.java    From StringManipulation with Apache License 2.0 6 votes vote down vote up
@Override
protected final void doExecute(final Editor editor, @Nullable final Caret caret, final DataContext dataContext) {
	MyApplicationService.setAction(actionClass, customActionModel);

	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);
}
 
Example #2
Source File: MyEditorWriteActionHandler.java    From dummytext-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected final void doExecute(@NotNull final Editor editor, @Nullable final Caret caret, final DataContext dataContext) {
    MyApplicationComponent.setAction(actionClass);

    final Pair<Boolean, T> additionalParameter = beforeWriteAction(editor, dataContext);
    if (!additionalParameter.first) {
        return;
    }

    final Runnable runnable = () -> 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);
}
 
Example #3
Source File: ExpandLiveTemplateCustomAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static EditorWriteActionHandler createExpandTemplateHandler(final char shortcutChar) {
  return new EditorWriteActionHandler(true) {
    @RequiredWriteAction
    @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;
    }
  };
}
 
Example #4
Source File: RemoveEmptyLinesAction.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
public RemoveEmptyLinesAction() {
	super(null);
	setupHandler(new EditorWriteActionHandler(true) {

		@Override
		public void executeWriteAction(Editor editor, DataContext dataContext) {
			MyApplicationService.setAction(getActionClass());

			// 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);
			}

		}
	});
}
 
Example #5
Source File: DecrementAction.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
public DecrementAction() {
	super(null);
	this.setupHandler(new EditorWriteActionHandler(true) {
		@Override
		public void executeWriteAction(Editor editor, DataContext dataContext) {
			MyApplicationService.setAction(getActionClass());

			// 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);
			}
		}
	});
}
 
Example #6
Source File: IncrementAction.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
public IncrementAction(boolean setupHandler) {
	super(null);
	if (setupHandler) {
		this.setupHandler(new EditorWriteActionHandler(true) {

			@Override
			public void executeWriteAction(Editor editor, DataContext dataContext) {
				MyApplicationService.setAction(getActionClass());

				// 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) {
					final String newText = processSelection(selectedText);
					applyChanges(editor, caretModel, line, column, selectionModel, hasSelection, selectedText, newText,
						caretOffset);
				}
			}

		});

	}
}
 
Example #7
Source File: ExpressionOrStatementInsertHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
@RequiredWriteAction
public void handleInsert(final InsertionContext context, final T item)
{
	final Editor editor = context.getEditor();
	final Document document = editor.getDocument();
	context.commitDocument();

	handleInsertImpl(context, item, editor, document);

	if(myOpenChar == '{')
	{
		document.insertString(editor.getCaretModel().getOffset(), "\n");
	}

	context.commitDocument();

	PsiElement elementAt = context.getFile().findElementAt(context.getStartOffset());
	PsiElement parent = elementAt == null ? null : elementAt.getParent();
	if(parent != null)
	{
		CodeStyleManager.getInstance(parent.getProject()).reformat(parent);

		if(myOpenChar == '{')
		{
			EditorWriteActionHandler actionHandler = (EditorWriteActionHandler) EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER);
			actionHandler.executeWriteAction(editor, DataManager.getInstance().getDataContext(editor.getContentComponent()));
		}
	}
}
 
Example #8
Source File: CSharpAccessorCompletionContributor.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
static void extend(CompletionContributor contributor)
{
	contributor.extend(CompletionType.BASIC, psiElement().andNot(psiElement().inside(DotNetXAccessor.class)), new CompletionProvider()
	{
		@RequiredReadAction
		@Override
		public void addCompletions(@Nonnull CompletionParameters completionParameters, ProcessingContext processingContext, @Nonnull CompletionResultSet resultSet)
		{
			PsiElement position = completionParameters.getPosition();
			final CSharpXAccessorOwner accessorOwner = PsiTreeUtil.getParentOfType(position, CSharpXAccessorOwner.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();

								DotNetXAccessor accessor = PsiTreeUtil.getParentOfType(elementAt, DotNetXAccessor.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()));
							}
						}
					});
				}
			}
		}
	});
}