1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-01-02 19:42:50 +01:00

Move paragraph action to vim-engine

This commit is contained in:
Alex Plate 2022-03-29 15:01:11 +03:00
parent d70abf6e27
commit bb1c30d6c8
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
7 changed files with 46 additions and 121 deletions
src/main/java/com/maddyhome/idea/vim
vim-engine/src/main/kotlin/com/maddyhome/idea/vim

View File

@ -1,44 +0,0 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2022 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 <https://www.gnu.org/licenses/>.
*/
package com.maddyhome.idea.vim.action.motion.text
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimCaret
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.handler.Motion
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.handler.toMotionOrError
import com.maddyhome.idea.vim.newapi.ij
class MotionCamelEndRightAction : MotionActionHandler.ForEachCaret() {
override fun getOffset(
editor: VimEditor,
caret: VimCaret,
context: ExecutionContext,
argument: Argument?,
operatorArguments: OperatorArguments,
): Motion {
return VimPlugin.getMotion().moveCaretToNextCamelEnd(editor.ij, caret.ij, operatorArguments.count1).toMotionOrError()
}
override val motionType: MotionType = MotionType.INCLUSIVE
}

View File

@ -1,49 +0,0 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2022 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 <https://www.gnu.org/licenses/>.
*/
package com.maddyhome.idea.vim.action.motion.text
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimCaret
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.handler.Motion
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.handler.toMotionOrError
import com.maddyhome.idea.vim.helper.enumSetOf
import com.maddyhome.idea.vim.newapi.ij
import java.util.*
class MotionParagraphPreviousAction : MotionActionHandler.ForEachCaret() {
override val flags: EnumSet<CommandFlags> = enumSetOf(CommandFlags.FLAG_SAVE_JUMP)
override fun getOffset(
editor: VimEditor,
caret: VimCaret,
context: ExecutionContext,
argument: Argument?,
operatorArguments: OperatorArguments,
): Motion {
return VimPlugin.getMotion().moveCaretToNextParagraph(editor.ij, caret.ij, -operatorArguments.count1).toMotionOrError()
}
override val motionType: MotionType = MotionType.EXCLUSIVE
}

View File

@ -552,26 +552,6 @@ public class MotionGroup extends VimMotionGroupBase {
}
}
/**
* This moves the caret to the start of the next/previous paragraph.
*
* @param editor The editor to move in
* @param caret The caret to be moved
* @param count The number of paragraphs to skip
* @return position
*/
public int moveCaretToNextParagraph(@NotNull Editor editor, @NotNull Caret caret, int count) {
int res = SearchHelper.findNextParagraph(editor, caret, count, false);
if (res >= 0) {
res = normalizeOffset(editor, res, true);
}
else {
res = -1;
}
return res;
}
public int moveCaretToNextSentenceStart(@NotNull Editor editor, @NotNull Caret caret, int count) {
int res = SearchHelper.findNextSentenceStart(editor, caret, count, false, true);
if (res >= 0) {

View File

@ -40,4 +40,12 @@ class IjVimSearchHelper : VimSearchHelper {
count,
)
}
override fun findNextCamelEnd(editor: VimEditor, caret: VimCaret, count: Int): Int {
return SearchHelper.findNextCamelEnd(
(editor as IjVimEditor).editor,
(caret as IjVimCaret).caret,
count,
)
}
}

View File

@ -17,19 +17,22 @@
*/
package com.maddyhome.idea.vim.action.motion.text
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimCaret
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.common.Direction
import com.maddyhome.idea.vim.handler.Motion
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.handler.toMotionOrError
import com.maddyhome.idea.vim.newapi.ij
class MotionCamelEndLeftAction : MotionActionHandler.ForEachCaret() {
class MotionCamelEndLeftAction : MotionCamelEndAction(Direction.BACKWARDS)
class MotionCamelEndRightAction : MotionCamelEndAction(Direction.FORWARDS)
sealed class MotionCamelEndAction(val direction: Direction) : MotionActionHandler.ForEachCaret() {
override fun getOffset(
editor: VimEditor,
caret: VimCaret,
@ -37,8 +40,16 @@ class MotionCamelEndLeftAction : MotionActionHandler.ForEachCaret() {
argument: Argument?,
operatorArguments: OperatorArguments,
): Motion {
return VimPlugin.getMotion().moveCaretToNextCamelEnd(editor.ij, caret.ij, -operatorArguments.count1).toMotionOrError()
return moveCaretToNextCamelEnd(editor, caret, direction.toInt() * operatorArguments.count1).toMotionOrError()
}
override val motionType: MotionType = MotionType.INCLUSIVE
}
fun moveCaretToNextCamelEnd(editor: VimEditor, caret: VimCaret, count: Int): Int {
return if (caret.offset.point == 0 && count < 0 || caret.offset.point >= editor.fileSize() - 1 && count > 0) {
-1
} else {
injector.searchHelper.findNextCamelEnd(editor, caret, count)
}
}

View File

@ -17,22 +17,22 @@
*/
package com.maddyhome.idea.vim.action.motion.text
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimCaret
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.common.Direction
import com.maddyhome.idea.vim.handler.Motion
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.handler.toMotionOrError
import com.maddyhome.idea.vim.helper.enumSetOf
import com.maddyhome.idea.vim.newapi.ij
import java.util.*
class MotionParagraphNextAction : MotionActionHandler.ForEachCaret() {
sealed class MotionParagraphAction(val direction: Direction) : MotionActionHandler.ForEachCaret() {
override val flags: EnumSet<CommandFlags> = enumSetOf(CommandFlags.FLAG_SAVE_JUMP)
override fun getOffset(
@ -42,8 +42,21 @@ class MotionParagraphNextAction : MotionActionHandler.ForEachCaret() {
argument: Argument?,
operatorArguments: OperatorArguments,
): Motion {
return VimPlugin.getMotion().moveCaretToNextParagraph(editor.ij, caret.ij, operatorArguments.count1).toMotionOrError()
return moveCaretToNextParagraph(editor, caret, direction.toInt() * operatorArguments.count1).toMotionOrError()
}
override val motionType: MotionType = MotionType.EXCLUSIVE
}
class MotionParagraphNextAction : MotionParagraphAction(Direction.FORWARDS)
class MotionParagraphPreviousAction : MotionParagraphAction(Direction.BACKWARDS)
private fun moveCaretToNextParagraph(editor: VimEditor, caret: VimCaret, count: Int): Int {
var res = injector.searchHelper.findNextParagraph(editor, caret, count, false)
res = if (res >= 0) {
injector.engineEditorHelper.normalizeOffset(editor, res, true)
} else {
-1
}
return res
}

View File

@ -23,4 +23,10 @@ interface VimSearchHelper {
dir: Int,
count: Int,
): Int
fun findNextCamelEnd(
editor: VimEditor,
caret: VimCaret,
count: Int,
): Int
}