1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-02-24 08:46:00 +01:00

Update selection when searching in Visual mode

This commit is contained in:
Matt Ellis 2024-05-01 18:04:02 +01:00 committed by Alex Pláte
parent ddabf8df5e
commit 4e2db68acf
6 changed files with 68 additions and 2 deletions
src
main/java/com/maddyhome/idea/vim/group/visual
test/java/org/jetbrains/plugins/ideavim/group
vim-engine/src/main/kotlin/com/maddyhome/idea/vim

View File

@ -26,9 +26,11 @@ import com.maddyhome.idea.vim.listener.VimListenerManager
import com.maddyhome.idea.vim.newapi.vim
import com.maddyhome.idea.vim.options.OptionConstants
import com.maddyhome.idea.vim.state.mode.Mode
import com.maddyhome.idea.vim.state.mode.inCommandLineMode
import com.maddyhome.idea.vim.state.mode.inNormalMode
import com.maddyhome.idea.vim.state.mode.inSelectMode
import com.maddyhome.idea.vim.state.mode.inVisualMode
import com.maddyhome.idea.vim.state.mode.returnTo
import com.maddyhome.idea.vim.vimscript.model.options.helpers.IdeaRefactorModeHelper
import com.maddyhome.idea.vim.vimscript.model.options.helpers.isIdeaRefactorModeKeep
import com.maddyhome.idea.vim.vimscript.model.options.helpers.isIdeaRefactorModeSelect
@ -67,6 +69,11 @@ internal object IdeaSelectionControl {
}
if (editor.selectionModel.hasSelection(true)) {
if (editor.vim.inCommandLineMode && editor.vim.mode.returnTo().hasVisualSelection) {
logger.trace { "Modifying selection while in Command-line mode, most likely incsearch" }
return@singleTask
}
if (dontChangeMode(editor)) {
IdeaRefactorModeHelper.correctSelection(editor)
logger.trace { "Selection corrected for refactoring" }

View File

@ -19,6 +19,8 @@ import com.maddyhome.idea.vim.action.motion.search.SearchWholeWordForwardAction
import com.maddyhome.idea.vim.common.Direction
import com.maddyhome.idea.vim.helper.RunnableHelper
import com.maddyhome.idea.vim.newapi.vim
import com.maddyhome.idea.vim.state.mode.Mode
import com.maddyhome.idea.vim.state.mode.SelectionType
import org.jetbrains.plugins.ideavim.SkipNeovimReason
import org.jetbrains.plugins.ideavim.TestWithoutNeovim
import org.jetbrains.plugins.ideavim.VimTestCase
@ -29,6 +31,7 @@ import kotlin.test.assertEquals
/**
* @author Alex Plate
*/
@Suppress("SpellCheckingInspection")
class SearchGroupTest : VimTestCase() {
@Test
fun `test one letter`() {
@ -1695,6 +1698,50 @@ class SearchGroupTest : VimTestCase() {
// TODO: Check caret position
}
@Test
fun `test incsearch updates selection when started in Visual mode`() {
doTest(
listOf("ve", "/dolor"),
"""
|Lorem ipsum dolor sit amet,
|consectetur adipiscing elit
|Sed in orci mauris.
|Cras id tellus in ex imperdiet egestas.
""".trimMargin(),
"""
|${s}Lorem ipsum ${c}${se}dolor sit amet,
|consectetur adipiscing elit
|Sed in orci mauris.
|Cras id tellus in ex imperdiet egestas.
""".trimMargin(),
Mode.CMD_LINE(Mode.VISUAL(SelectionType.CHARACTER_WISE))
) {
enterCommand("set hlsearch incsearch")
}
}
@Test
fun `test incsearch updates block selection when started in Visual mode`() {
doTest(
listOf("ll", "<C-V>2j", "/mauris"),
"""
|Lorem ipsum dolor sit amet,
|consectetur adipiscing elit
|Sed in orci mauris.
|Cras id tellus in ex imperdiet egestas.
""".trimMargin(),
"""
|Lo${s}rem ipsum ${c}d${se}olor sit amet,
|co${s}nsectetur ${c}a${se}dipiscing elit
|Se${s}d in orci ${c}m${se}auris.
|Cras id tellus in ex imperdiet egestas.
""".trimMargin(),
Mode.CMD_LINE(Mode.VISUAL(SelectionType.BLOCK_WISE))
) {
enterCommand("set hlsearch incsearch")
}
}
@Test
fun `test highlight search results`() {
configureByText(

View File

@ -19,6 +19,7 @@ import com.maddyhome.idea.vim.helper.exitVisualMode
import com.maddyhome.idea.vim.register.Register
import com.maddyhome.idea.vim.state.mode.SelectionType
import com.maddyhome.idea.vim.state.mode.inBlockSelection
import com.maddyhome.idea.vim.state.mode.inCommandLineMode
import com.maddyhome.idea.vim.state.mode.inSelectMode
import com.maddyhome.idea.vim.state.mode.inVisualMode
import javax.swing.KeyStroke
@ -104,6 +105,10 @@ per-caret marks.
// Another inconsistency with immutable caret. This method should be called on the new caret instance.
caretAfterMove.vimMoveSelectionToCaret(this.vimSelectionStart)
editor.findLastVersionOfCaret(caretAfterMove) ?: caretAfterMove
} else if (editor.inCommandLineMode && caretAfterMove.hasSelection()) {
// If we're updating the caret in Command-line mode, it's most likely due to incsearch
caretAfterMove.setSelection(caretAfterMove.selectionStart, offset)
editor.findLastVersionOfCaret(caretAfterMove) ?: caretAfterMove
} else {
editor.exitVisualMode()
caretAfterMove

View File

@ -12,7 +12,6 @@ import com.maddyhome.idea.vim.api.*
import com.maddyhome.idea.vim.helper.*
import com.maddyhome.idea.vim.state.mode.Mode
import com.maddyhome.idea.vim.state.mode.SelectionType
import com.maddyhome.idea.vim.state.mode.SelectionType.CHARACTER_WISE
import com.maddyhome.idea.vim.state.mode.inBlockSelection
import com.maddyhome.idea.vim.state.mode.inSelectMode
import com.maddyhome.idea.vim.state.mode.inVisualMode
@ -21,7 +20,7 @@ import com.maddyhome.idea.vim.state.mode.selectionType
public fun setVisualSelection(selectionStart: Int, selectionEnd: Int, caret: VimCaret) {
val (start, end) = if (selectionStart > selectionEnd) selectionEnd to selectionStart else selectionStart to selectionEnd
val editor = caret.editor
val subMode = editor.mode.selectionType ?: CHARACTER_WISE
val subMode = editor.mode.selectionType ?: SelectionType.CHARACTER_WISE
val mode = editor.mode
when (subMode) {
SelectionType.CHARACTER_WISE -> {

View File

@ -26,6 +26,9 @@ public val VimEditor.inNormalMode: Boolean
public val VimEditor.inSelectMode: Boolean
get() = this.mode is Mode.SELECT
public val VimEditor.inCommandLineMode: Boolean
get() = this.mode is Mode.CMD_LINE
public val VimEditor.singleModeActive: Boolean
get() = this.mode.isSingleModeActive

View File

@ -10,11 +10,16 @@ package com.maddyhome.idea.vim.state.mode
/**
* Get the selection type if the mode is [Mode.VISUAL] or [Mode.SELECT]. Otherwise, returns null.
*
* Note that if the mode is [Mode.CMD_LINE], we return the selection type of the underlying editor. This only has an
* effect for (inc)search, as we switch to [Mode.NORMAL] before entering an ex command.
*/
@Suppress("RecursivePropertyAccessor")
public val Mode.selectionType: SelectionType?
get() = when (this) {
is Mode.VISUAL -> this.selectionType
is Mode.SELECT -> this.selectionType
is Mode.CMD_LINE -> this.returnTo().selectionType
else -> null
}