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

The following examples show how to use com.intellij.openapi.editor.actionSystem.TypedAction. 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: PowerMode.java    From power-mode-intellij-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void initComponent() {

    final EditorActionManager editorActionManager = EditorActionManager.getInstance();
    final EditorFactory editorFactory = EditorFactory.getInstance();
    particleContainerManager = new ParticleContainerManager();
    editorFactory.addEditorFactoryListener(particleContainerManager, new Disposable() {
        @Override
        public void dispose() {

        }
    });
    final TypedAction typedAction = editorActionManager.getTypedAction();
    final TypedActionHandler rawHandler = typedAction.getRawHandler();
    typedAction.setupRawHandler(new TypedActionHandler() {
        @Override
        public void execute(@NotNull final Editor editor, final char c, @NotNull final DataContext dataContext) {
            updateEditor(editor);
            rawHandler.execute(editor, c, dataContext);
        }
    });
}
 
Example #2
Source File: SelectionQuotingTypedHandlerTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void doTest(@Nonnull final String cs, @Nonnull String before, @Nonnull String expected) {
  final boolean smarterSelection = Registry.is("editor.smarterSelectionQuoting");
  Registry.get("editor.smarterSelectionQuoting").setValue(true);
  try {
    myFixture.configureByText(PlainTextFileType.INSTANCE, before);
    final TypedAction typedAction = EditorActionManager.getInstance().getTypedAction();

    performAction(myFixture.getProject(), new Runnable() {
      @Override
      public void run() {
        for (int i = 0, max = cs.length(); i < max; i++) {
          final char c = cs.charAt(i);
          typedAction.actionPerformed(myFixture.getEditor(), c, ((EditorEx)myFixture.getEditor()).getDataContext());
        }
      }
    });
    myFixture.checkResult(expected);
  } finally {
    Registry.get("editor.smarterSelectionQuoting").setValue(smarterSelection);
  }
}
 
Example #3
Source File: SelectionQuotingTypedHandlerTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testRuby7852ErrantEditor() {
  myFixture.configureByText(PlainTextFileType.INSTANCE, "\"aaa\"\nbbb\n\n");
  myFixture.getEditor().getCaretModel().moveToOffset(0);
  myFixture.getEditor().getSelectionModel().setSelection(0, 5);
  final TypedAction typedAction = EditorActionManager.getInstance().getTypedAction();
  performAction(myFixture.getProject(), new Runnable() {
    @Override
    public void run() {
      typedAction.actionPerformed(myFixture.getEditor(), '\'', ((EditorEx)myFixture.getEditor()).getDataContext());
    }
  });
  myFixture.getEditor().getSelectionModel().removeSelection();
  myFixture.checkResult("'aaa'\nbbb\n\n");

  myFixture.getEditor().getCaretModel().moveToOffset(myFixture.getEditor().getDocument().getLineStartOffset(3));
  performAction(myFixture.getProject(), new Runnable() {
    @Override
    public void run() {
      typedAction.actionPerformed(myFixture.getEditor(), 'A', ((EditorEx)myFixture.getEditor()).getDataContext());
      typedAction.actionPerformed(myFixture.getEditor(), 'B', ((EditorEx)myFixture.getEditor()).getDataContext());
    }
  });
  myFixture.checkResult("'aaa'\nbbb\n\nAB");
}
 
Example #4
Source File: StartupEvent.java    From tmc-intellij with MIT License 5 votes vote down vote up
private void setupHandlersForSnapshots(ProgressObserver observer) {
    observer.progress(0, 0.70, "Setting handlers");
    final EditorActionManager actionManager = EditorActionManager.getInstance();
    final TypedAction typedAction = actionManager.getTypedAction();
    TypedActionHandler originalHandler = actionManager.getTypedAction().getHandler();
    typedAction.setupHandler(new ActivateSnapshotsAction(originalHandler));
}
 
Example #5
Source File: SelectionQuotingTypedHandlerTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void doTest(final char c, @Nonnull String before, @Nonnull String expected) {
  myFixture.configureByText(PlainTextFileType.INSTANCE, before);
  final TypedAction typedAction = EditorActionManager.getInstance().getTypedAction();
  performAction(myFixture.getProject(), new Runnable() {
    @Override
    public void run() {
      typedAction.actionPerformed(myFixture.getEditor(), c, ((EditorEx)myFixture.getEditor()).getDataContext());
    }
  });
  myFixture.getEditor().getSelectionModel().removeSelection();
  myFixture.checkResult(expected);
}
 
Example #6
Source File: ActionMacro.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void playBack(DataContext context) {
  Editor editor = context.getData(PlatformDataKeys.EDITOR);
  final TypedAction typedAction = EditorActionManager.getInstance().getTypedAction();
  for (final char aChar : myText.toCharArray()) {
    typedAction.actionPerformed(editor, aChar, context);
  }
}
 
Example #7
Source File: JumpHandler.java    From KJump with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * start search mode
 *
 * @param mode mode enum, see {@link #MODE_CHAR1} {@link #MODE_CHAR2} etc
 */
public final void start(@NotNull Editor e, int mode) {
    if (!isStart) {
        isStart = true;
        EditorActionManager manager = EditorActionManager.getInstance();
        TypedAction typedAction = manager.getTypedAction();

        mOldTypedHandler = typedAction.getRawHandler();
        typedAction.setupRawHandler(this);

        mOldEscActionHandler = manager.getActionHandler(IdeActions.ACTION_EDITOR_ESCAPE);
        manager.setActionHandler(IdeActions.ACTION_EDITOR_ESCAPE, escActionHandler);

        switch (mode) {
            case MODE_CHAR1:
                finder = new Char1Finder();
                break;
            case MODE_CHAR2:
                finder = new Char2Finder();
                break;
            case MODE_WORD0:
                finder = new Word0Finder();
                break;
            case MODE_WORD1:
                finder = new Word1Finder();
                break;
            case MODE_LINE:
                finder = new LineFinder();
                break;
            default:
                throw new RuntimeException("Invalid start mode: " + mode);
        }

        TextRange visibleBorderOffset = EditorUtils.getVisibleRangeOffset(e);
        String visibleString = e.getDocument().getText(visibleBorderOffset);
        List<MarksCanvas.Mark> marks = finder.start(e, visibleString, visibleBorderOffset);
        if (marks != null) {
            lastMarks = marks;
            this.jumpOrShowCanvas(e, lastMarks);
        }
    }
}
 
Example #8
Source File: TypedHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void indentBrace(@Nonnull final Project project, @Nonnull final Editor editor, final char braceChar) {
  final int offset = editor.getCaretModel().getOffset() - 1;
  final Document document = editor.getDocument();
  CharSequence chars = document.getCharsSequence();
  if (offset < 0 || chars.charAt(offset) != braceChar) return;

  int spaceStart = CharArrayUtil.shiftBackward(chars, offset - 1, " \t");
  if (spaceStart < 0 || chars.charAt(spaceStart) == '\n' || chars.charAt(spaceStart) == '\r') {
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    documentManager.commitDocument(document);

    final PsiFile file = documentManager.getPsiFile(document);
    if (file == null || !file.isWritable()) return;
    PsiElement element = file.findElementAt(offset);
    if (element == null) return;

    EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset);

    final FileType fileType = file.getFileType();
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
    boolean rBraceToken = braceMatcher.isRBraceToken(iterator, chars, fileType);
    final boolean isBrace = braceMatcher.isLBraceToken(iterator, chars, fileType) || rBraceToken;
    int lBraceOffset = -1;

    if (CodeInsightSettings.getInstance().REFORMAT_BLOCK_ON_RBRACE && rBraceToken && braceMatcher.isStructuralBrace(iterator, chars, fileType) && offset > 0) {
      lBraceOffset = BraceMatchingUtil
              .findLeftLParen(highlighter.createIterator(offset - 1), braceMatcher.getOppositeBraceTokenType(iterator.getTokenType()), editor.getDocument().getCharsSequence(), fileType);
    }
    if (element.getNode() != null && isBrace) {
      DefaultRawTypedHandler handler = ((TypedActionImpl)TypedAction.getInstance()).getDefaultRawTypedHandler();
      handler.beginUndoablePostProcessing();

      final int finalLBraceOffset = lBraceOffset;
      ApplicationManager.getApplication().runWriteAction(() -> {
        try {
          int newOffset;
          if (finalLBraceOffset != -1) {
            RangeMarker marker = document.createRangeMarker(offset, offset + 1);
            CodeStyleManager.getInstance(project).reformatRange(file, finalLBraceOffset, offset, true);
            newOffset = marker.getStartOffset();
            marker.dispose();
          }
          else {
            newOffset = CodeStyleManager.getInstance(project).adjustLineIndent(file, offset);
          }

          editor.getCaretModel().moveToOffset(newOffset + 1);
          editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
          editor.getSelectionModel().removeSelection();
        }
        catch (IncorrectOperationException e) {
          LOG.error(e);
        }
      });
    }
  }
}
 
Example #9
Source File: ConsoleViewImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static synchronized void initTypedHandler() {
  if (ourTypedHandlerInitialized) return;
  TypedAction typedAction = EditorActionManager.getInstance().getTypedAction();
  typedAction.setupHandler(new MyTypedHandler(typedAction.getHandler()));
  ourTypedHandlerInitialized = true;
}