public Transition<KeyMapInfo> press(KeyStroke key) {
   char c = key.getCharacter();
   if ('0' <= c && c <= '9') {
     return new SimpleTransition<KeyMapInfo>(value, this);
   }
   return state.press(key);
 }
示例#2
0
  @Override
  @SuppressWarnings("unchecked")
  protected State<Command> buildInitialState() {
    final Command resumeInsertMode =
        new ChangeToInsertModeCommand(
            new MotionCommand(new GoToMarkMotion(false, CursorService.LAST_INSERT_MARK)));

    final Command visualMode = new ChangeModeCommand(VisualMode.NAME);
    final Command linewiseVisualMode = new ChangeModeCommand(LinewiseVisualMode.NAME);
    final Command blockwiseVisualMode = new ChangeModeCommand(BlockwiseVisualMode.NAME);

    final Motion moveLeft = MoveLeft.INSTANCE;
    final Motion moveRight = MoveRight.INSTANCE;
    final Motion wordRight = MoveWordRight.INSTANCE;
    final Motion wordEndRightForChange = MoveWordEndRightForChange.INSTANCE;
    final Motion bigWordRight = MoveBigWORDRight.INSTANCE;
    final Motion bigWordEndRightForChange = MoveBigWORDEndRightForChange.INSTANCE;
    final Motion bol = LineStartMotion.NON_WHITESPACE;
    final Motion eol = new LineEndMotion(BorderPolicy.EXCLUSIVE);
    final Motion wholeLineEol = new LineEndMotion(BorderPolicy.LINE_WISE);

    final State<Motion> motions = motions();
    final TextObject wordForCw =
        new OptionDependentTextObject(Options.SANE_CW, wordRight, wordEndRightForChange);
    final TextObject wordForCW =
        new OptionDependentTextObject(Options.SANE_CW, bigWordRight, bigWordEndRightForChange);
    final TextObject toEol = new MotionTextObject(eol);
    final TextObject toEolForY = new OptionDependentTextObject(Options.SANE_Y, eol, wholeLineEol);

    final State<TextObject> textObjects = textObjects();
    State<TextObject> textObjectsForChange =
        union(state(leafBind('w', wordForCw), leafBind('W', wordForCW)), textObjects);
    textObjectsForChange = CountingState.wrap(textObjectsForChange);

    final TextOperation delete = DeleteOperation.INSTANCE;
    final TextOperation change = ChangeOperation.INSTANCE;
    final TextOperation yank = YankOperation.INSTANCE;
    final TextOperation format = FormatOperation.INSTANCE;
    final Command undo = UndoCommand.INSTANCE;
    final Command redo = RedoCommand.INSTANCE;
    final Command pasteAfter = PasteAfterCommand.CURSOR_ON_TEXT;
    final Command pasteBefore = PasteBeforeCommand.CURSOR_ON_TEXT;
    final Command pasteAfterWithG = PasteAfterCommand.CURSOR_AFTER_TEXT;
    final Command pasteBeforeWithG = PasteBeforeCommand.CURSOR_AFTER_TEXT;
    final Command deleteNext =
        new TextOperationTextObjectCommand(delete, new MotionTextObject(moveRight));
    final Command deletePrevious =
        new TextOperationTextObjectCommand(delete, new MotionTextObject(moveLeft));
    final Command repeatLastOne = DotCommand.INSTANCE;
    final Command tildeCmd = SwapCaseCommand.INSTANCE;
    final LineEndMotion lineEndMotion = new LineEndMotion(BorderPolicy.LINE_WISE);
    final Command substituteLine =
        new TextOperationTextObjectCommand(change, new MotionTextObject(lineEndMotion));
    final Command substituteChar =
        new TextOperationTextObjectCommand(change, new MotionTextObject(moveRight));
    final Command incrementNum = IncrementDecrementCommand.INCREMENT;
    final Command decrementNum = IncrementDecrementCommand.DECREMENT;
    final Command joinLines = JoinLinesCommand.INSTANCE;
    final Command joinLinesDumbWay = JoinLinesCommand.DUMB_INSTANCE;
    final Command centerLine = CenterLineCommand.CENTER;
    final Command centerBottomLine = CenterLineCommand.BOTTOM;
    final Command centerTopLine = CenterLineCommand.TOP;
    final Command findFile = FindFileCommand.INSTANCE;
    final Command repeatSubLine = RepeatLastSubstitutionCommand.CURRENT_LINE_ONLY;
    final Command repeatSubGlobal = RepeatLastSubstitutionCommand.GLOBALLY;
    final Command saveAndClose = new VimCommandSequence(SaveCommand.INSTANCE, CloseCommand.CLOSE);

    final Command afterEnteringVisualInc =
        new OptionDependentCommand<String>(
            Options.SELECTION, "inclusive", new VisualMotionCommand(moveRight));
    final Command afterEnteringVisualExc =
        new OptionDependentCommand<String>(
            Options.SELECTION,
            "exclusive",
            new CountIgnoringNonRepeatableCommand() {
              @Override
              public void execute(final EditorAdaptor editorAdaptor)
                  throws CommandExecutionException {
                final Position position = editorAdaptor.getPosition();
                editorAdaptor.setSelection(
                    new SimpleSelection(new StartEndTextRange(position, position)));
              }
            });
    final Command afterEnteringBlockVisual =
        new CountIgnoringNonRepeatableCommand() {

          @Override
          public void execute(final EditorAdaptor editorAdaptor) throws CommandExecutionException {
            final Position position = editorAdaptor.getPosition();
            //                final Position next =
            // editorAdaptor.getCursorService().newPositionForModelOffset(position.getModelOffset()+1);
            editorAdaptor.setSelection(new BlockWiseSelection(editorAdaptor, position, position));
          }
        };
    final Command selectLine =
        new CountIgnoringNonRepeatableCommand() {
          @Override
          public void execute(final EditorAdaptor editorAdaptor) throws CommandExecutionException {
            final Position position = editorAdaptor.getPosition();
            editorAdaptor.setSelection(new LineWiseSelection(editorAdaptor, position, position));
          }
        };
    final Command afterEnteringVisual = seq(afterEnteringVisualInc, afterEnteringVisualExc);

    final State<Command> motionCommands = new GoThereState(motions);
    final Command nextResult = motionCommands.press(key('n')).getValue();

    final State<Command> platformSpecificState = getPlatformSpecificState(NAME);
    return RegisterState.wrap(
        CountingState.wrap(
            union(
                platformSpecificState,
                operatorCmdsWithUpperCase('d', delete, toEol, textObjects),
                operatorCmdsWithUpperCase('y', yank, toEolForY, textObjects),
                operatorCmdsWithUpperCase('c', change, toEol, textObjectsForChange),
                operatorCmds('!', NormalLineRangeOperation.INSTANCE, textObjects),
                prefixedOperatorCmds('g', 'q', format, textObjects),
                prefixedOperatorCmds('g', '~', SwapCaseCommand.TEXT_OBJECT_INSTANCE, textObjects),
                motionCommands,
                state(
                    leafBind('i', (Command) new ChangeToInsertModeCommand()),
                    leafBind(
                        'a', (Command) new ChangeToInsertModeCommand(new MotionCommand(moveRight))),
                    leafBind('I', (Command) new ChangeToInsertModeCommand(new MotionCommand(bol))),
                    leafBind('A', (Command) new ChangeToInsertModeCommand(new MotionCommand(eol))),
                    leafBind(':', (Command) new ChangeModeCommand(CommandLineMode.NAME)),
                    leafBind('?', (Command) new ChangeToSearchModeCommand(true, nextResult)),
                    leafBind('/', (Command) new ChangeToSearchModeCommand(false, nextResult)),
                    leafBind('R', (Command) new ReplaceMode.ChangeToReplaceModeCommand()),
                    leafBind(
                        'o',
                        (Command) new ChangeToInsertModeCommand(InsertLineCommand.POST_CURSOR)),
                    leafBind(
                        'O', (Command) new ChangeToInsertModeCommand(InsertLineCommand.PRE_CURSOR)),
                    leafBind('v', dontRepeat(seq(visualMode, afterEnteringVisual))),
                    leafBind('V', dontRepeat(seq(linewiseVisualMode, selectLine))),
                    leafCtrlBind(
                        'v', dontRepeat(seq(blockwiseVisualMode, afterEnteringBlockVisual))),
                    leafCtrlBind(
                        'q', dontRepeat(seq(blockwiseVisualMode, afterEnteringBlockVisual))),
                    leafBind('p', pasteAfter),
                    leafBind('.', repeatLastOne),
                    leafBind('P', pasteBefore),
                    leafBind('x', deleteNext),
                    leafBind(SpecialKey.DELETE, deleteNext),
                    leafBind('X', deletePrevious),
                    leafBind('~', tildeCmd),
                    leafBind('S', substituteLine),
                    leafBind('s', substituteChar),
                    leafBind('J', joinLines),
                    leafBind('&', repeatSubLine),
                    transitionBind(
                        'g',
                        leafBind('a', (Command) AsciiCommand.INSTANCE),
                        leafBind('f', findFile),
                        leafBind('&', repeatSubGlobal),
                        leafBind('i', resumeInsertMode),
                        leafBind('J', joinLinesDumbWay),
                        leafBind('p', pasteAfterWithG),
                        leafBind('P', pasteBeforeWithG),
                        leafBind('v', RestoreSelectionCommand.INSTANCE)),
                    transitionBind(
                        'q',
                        convertKeyStroke(
                            RecordMacroCommand.KEYSTROKE_CONVERTER,
                            VimConstants.PRINTABLE_KEYSTROKES)),
                    transitionBind(
                        '@',
                        convertKeyStroke(
                            PlaybackMacroCommand.KEYSTROKE_CONVERTER,
                            VimConstants.PRINTABLE_KEYSTROKES)),
                    transitionBind(
                        'r',
                        changeCaret(CaretType.UNDERLINE),
                        convertKeyStroke(
                            ReplaceCommand.KEYSTROKE_CONVERTER,
                            VimConstants.PRINTABLE_KEYSTROKES_WITH_NL)),
                    transitionBind(
                        'm',
                        convertKeyStroke(
                            SetMarkCommand.KEYSTROKE_CONVERTER, VimConstants.PRINTABLE_KEYSTROKES)),
                    leafBind('u', undo),
                    leafCtrlBind('r', redo),
                    leafCtrlBind('a', incrementNum),
                    leafCtrlBind('x', decrementNum),
                    transitionBind(
                        'Z',
                        leafBind('Z', saveAndClose),
                        leafBind('Q', (Command) CloseCommand.FORCED_CLOSE)),
                    transitionBind(
                        'z',
                        leafBind('z', centerLine),
                        leafBind('.', centerLine),
                        leafBind('-', centerBottomLine),
                        leafBind('b', centerBottomLine),
                        leafBind('t', centerTopLine),
                        leafBind(SpecialKey.RETURN, centerTopLine))))));
  }