1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-03-06 18:32:51 +01:00

Convert PutLinesHandler to kotlin

This commit is contained in:
Alex Plate 2019-02-22 15:59:00 +03:00
parent cbb0d1a05e
commit 0f835fd3ba
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
2 changed files with 82 additions and 91 deletions
src/com/maddyhome/idea/vim/ex/handler

View File

@ -1,91 +0,0 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2019 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.maddyhome.idea.vim.ex.handler;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.CommandState;
import com.maddyhome.idea.vim.command.SelectionType;
import com.maddyhome.idea.vim.common.Register;
import com.maddyhome.idea.vim.common.TextRange;
import com.maddyhome.idea.vim.ex.CommandHandler;
import com.maddyhome.idea.vim.ex.CommandName;
import com.maddyhome.idea.vim.ex.ExCommand;
import com.maddyhome.idea.vim.ex.ExException;
import com.maddyhome.idea.vim.group.MarkGroup;
import com.maddyhome.idea.vim.group.RegisterGroup;
import com.maddyhome.idea.vim.handler.CaretOrder;
import com.maddyhome.idea.vim.helper.EditorHelper;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class PutLinesHandler extends CommandHandler {
public PutLinesHandler() {
super(new CommandName[]{
new CommandName("pu", "t")
}, RANGE_OPTIONAL | ARGUMENT_OPTIONAL | WRITABLE);
}
public boolean execute(@NotNull Editor editor, @NotNull DataContext context,
@NotNull ExCommand cmd) throws ExException {
if (editor.isOneLineMode()) return false;
final RegisterGroup registerGroup = VimPlugin.getRegister();
final String arg = cmd.getArgument();
if (arg.length() > 0 && !registerGroup.selectRegister(arg.charAt(0))) {
return false;
}
else {
registerGroup.selectRegister(registerGroup.getDefaultRegister());
}
final Register register = registerGroup.getLastRegister();
if (register == null) return false;
final String text = register.getText();
final List<Integer> lines = cmd.getOrderedLines(editor, context, CaretOrder.DECREASING_OFFSET);
final List<Caret> carets = EditorHelper.getOrderedCaretsList(editor, CaretOrder.DECREASING_OFFSET);
for (int i = 0; i < carets.size(); i++) {
final Caret caret = carets.get(i);
final int line = lines.get(i);
int startOffset = Math.min(editor.getDocument().getTextLength(),
VimPlugin.getMotion().moveCaretToLineEnd(editor, line, true) + 1);
if (startOffset > 0 && startOffset == editor.getDocument().getTextLength() &&
editor.getDocument().getCharsSequence().charAt(startOffset - 1) != '\n') {
editor.getDocument().insertString(startOffset, "\n");
startOffset++;
}
if (text == null) {
VimPlugin.getMark().setMark(editor, MarkGroup.MARK_CHANGE_POS, startOffset);
VimPlugin.getMark().setChangeMarks(editor, new TextRange(startOffset, startOffset));
continue;
}
VimPlugin.getCopy().putText(editor, caret, context, text, SelectionType.LINE_WISE, CommandState.SubMode.NONE,
startOffset, 1, false, false);
}
return true;
}
}

View File

@ -0,0 +1,82 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2019 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.maddyhome.idea.vim.ex.handler
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.SelectionType
import com.maddyhome.idea.vim.common.Register
import com.maddyhome.idea.vim.common.TextRange
import com.maddyhome.idea.vim.ex.*
import com.maddyhome.idea.vim.ex.CommandHandler.*
import com.maddyhome.idea.vim.group.MarkGroup
import com.maddyhome.idea.vim.group.RegisterGroup
import com.maddyhome.idea.vim.handler.CaretOrder
import com.maddyhome.idea.vim.helper.EditorHelper
class PutLinesHandler : CommandHandler(
commands("pu[t]"),
flags(RANGE_OPTIONAL, ARGUMENT_OPTIONAL, WRITABLE)
) {
@Throws(ExException::class)
override fun execute(editor: Editor, context: DataContext, cmd: ExCommand): Boolean {
if (editor.isOneLineMode) return false
val registerGroup = VimPlugin.getRegister()
val arg = cmd.argument
if (arg.isNotEmpty() && !registerGroup.selectRegister(arg[0])) {
return false
} else {
registerGroup.selectRegister(registerGroup.defaultRegister)
}
val register = registerGroup.lastRegister ?: return false
val text = register.text
val lines = cmd.getOrderedLines(editor, context, CaretOrder.DECREASING_OFFSET)
val carets = EditorHelper.getOrderedCaretsList(editor, CaretOrder.DECREASING_OFFSET)
for (i in carets.indices) {
val caret = carets[i]
val line = lines[i]
var startOffset = minOf(editor.document.textLength,
VimPlugin.getMotion().moveCaretToLineEnd(editor, line, true) + 1)
if (startOffset > 0 && startOffset == editor.document.textLength &&
editor.document.charsSequence[startOffset - 1] != '\n') {
editor.document.insertString(startOffset, "\n")
startOffset++
}
if (text == null) {
VimPlugin.getMark().setMark(editor, MarkGroup.MARK_CHANGE_POS, startOffset)
VimPlugin.getMark().setChangeMarks(editor, TextRange(startOffset, startOffset))
continue
}
VimPlugin.getCopy().putText(editor, caret, context, text, SelectionType.LINE_WISE, CommandState.SubMode.NONE,
startOffset, 1, false, false)
}
return true
}
}