Search in sources :

Example 26 with TextRange

use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.

the class ChangeGroup method deleteEndOfLine.

/**
   * Delete from the cursor to the end of count - 1 lines down
   *
   * @param editor  The editor to delete from
   * @param count   The number of lines affected
   * @return true if able to delete the text, false if not
   */
public boolean deleteEndOfLine(@NotNull Editor editor, int count) {
    int offset = VimPlugin.getMotion().moveCaretToLineEndOffset(editor, count - 1, true);
    if (offset != -1) {
        boolean res = deleteText(editor, new TextRange(editor.getCaretModel().getOffset(), offset), SelectionType.CHARACTER_WISE);
        int pos = VimPlugin.getMotion().moveCaretHorizontal(editor, -1, false);
        if (pos != -1) {
            MotionGroup.moveCaret(editor, pos);
        }
        return res;
    }
    return false;
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 27 with TextRange

use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.

the class ChangeGroup method getDeleteMotionRange.

@Nullable
public static TextRange getDeleteMotionRange(@NotNull Editor editor, DataContext context, int count, int rawCount, @NotNull Argument argument) {
    TextRange range = MotionGroup.getMotionRange(editor, context, count, rawCount, argument, true);
    // This is a kludge for dw, dW, and d[w. Without this kludge, an extra newline is deleted when it shouldn't be.
    if (range != null) {
        String text = editor.getDocument().getCharsSequence().subSequence(range.getStartOffset(), range.getEndOffset()).toString();
        final int lastNewLine = text.lastIndexOf('\n');
        if (lastNewLine > 0) {
            final Command motion = argument.getMotion();
            if (motion != null) {
                final String id = ActionManager.getInstance().getId(motion.getAction());
                if (id.equals("VimMotionWordRight") || id.equals("VimMotionBigWordRight") || id.equals("VimMotionCamelRight")) {
                    if (!SearchHelper.anyNonWhitespace(editor, range.getEndOffset(), -1)) {
                        final int start = range.getStartOffset();
                        range = new TextRange(start, start + lastNewLine);
                    }
                }
            }
        }
    }
    return range;
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with TextRange

use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.

the class CommandParser method processCommand.

/**
   * Parse and execute an Ex command entered by the user
   *
   * @param editor  The editor to run the command in
   * @param context The data context
   * @param cmd     The text entered by the user
   * @param count   The count entered before the colon
   * @return A bitwise collection of flags, if any, from the result of running the command.
   * @throws ExException if any part of the command is invalid or unknown
   */
public int processCommand(@NotNull Editor editor, @NotNull DataContext context, @NotNull String cmd, int count) throws ExException {
    // Nothing entered
    int result = 0;
    if (cmd.length() == 0) {
        return result | RES_EMPTY;
    }
    // Save the command history
    VimPlugin.getHistory().addEntry(HistoryGroup.COMMAND, cmd);
    // Parse the command
    final ExCommand command = parse(cmd);
    final CommandHandler handler = getCommandHandler(command);
    if (handler == null) {
        final String message = MessageHelper.message(Msg.NOT_EX_CMD, command.getCommand());
        throw new InvalidCommandException(message, cmd);
    }
    if ((handler.getArgFlags() & CommandHandler.WRITABLE) > 0 && !editor.getDocument().isWritable()) {
        VimPlugin.indicateError();
        return result | RES_READONLY;
    }
    // Run the command
    boolean ok = handler.process(editor, context, command, count);
    if (ok && (handler.getArgFlags() & CommandHandler.DONT_SAVE_LAST) == 0) {
        VimPlugin.getRegister().storeTextInternal(editor, new TextRange(-1, -1), cmd, SelectionType.CHARACTER_WISE, ':', false);
    }
    if ((handler.getArgFlags() & CommandHandler.DONT_REOPEN) != 0) {
        result |= RES_DONT_REOPEN;
    }
    return result;
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 29 with TextRange

use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.

the class CopyTextHandler method execute.

public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) throws ExException {
    TextRange range = cmd.getTextRange(editor, context, false);
    final ExCommand argumentCmd = CommandParser.getInstance().parse(cmd.getArgument());
    int line = argumentCmd.getRanges().getFirstLine(editor, context);
    int offset = VimPlugin.getMotion().moveCaretToLineStart(editor, line + 1);
    String text = EditorHelper.getText(editor, range.getStartOffset(), range.getEndOffset());
    VimPlugin.getCopy().putText(editor, context, offset, text, SelectionType.LINE_WISE, 1, true, false, CommandState.SubMode.NONE);
    return true;
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 30 with TextRange

use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.

the class DeleteLinesHandler method execute.

public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) throws ExException {
    StringBuilder arg = new StringBuilder(cmd.getArgument());
    char register = VimPlugin.getRegister().getDefaultRegister();
    if (arg.length() > 0 && (arg.charAt(0) < '0' || arg.charAt(0) > '9')) {
        register = arg.charAt(0);
        arg.deleteCharAt(0);
        cmd.setArgument(arg.toString());
    }
    VimPlugin.getRegister().selectRegister(register);
    TextRange range = cmd.getTextRange(editor, context, true);
    return VimPlugin.getChange().deleteRange(editor, range, SelectionType.LINE_WISE, false);
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange)

Aggregations

TextRange (com.maddyhome.idea.vim.common.TextRange)46 Nullable (org.jetbrains.annotations.Nullable)8 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)2 CharacterPosition (com.maddyhome.idea.vim.common.CharacterPosition)2 Register (com.maddyhome.idea.vim.common.Register)2 RegExp (com.maddyhome.idea.vim.regexp.RegExp)2 Document (com.intellij.openapi.editor.Document)1 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 MotionEditorAction (com.maddyhome.idea.vim.action.motion.MotionEditorAction)1 TextObjectAction (com.maddyhome.idea.vim.action.motion.TextObjectAction)1 Command (com.maddyhome.idea.vim.command.Command)1 CommandState (com.maddyhome.idea.vim.command.CommandState)1 SelectionType (com.maddyhome.idea.vim.command.SelectionType)1 Mark (com.maddyhome.idea.vim.common.Mark)1 SearchHelper (com.maddyhome.idea.vim.helper.SearchHelper)1