mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-08-15 02:17:05 +02:00
Enter insert mode after select mode while editing template
This commit is contained in:
src/com/maddyhome/idea/vim/group
test/org/jetbrains/plugins/ideavim
@@ -823,13 +823,14 @@ public class ChangeGroup {
|
||||
}
|
||||
|
||||
public boolean processKeyInSelectMode(@NotNull final Editor editor,
|
||||
@NotNull final DataContext context, @NotNull final KeyStroke key) {
|
||||
@NotNull final DataContext context,
|
||||
@NotNull final KeyStroke key) {
|
||||
SelectionVimListenerSuppressor.INSTANCE.lock();
|
||||
boolean res = processKey(editor, context, key);
|
||||
|
||||
VimPlugin.getVisualMotion().exitSelectModeAndResetKeyHandler(editor, false);
|
||||
|
||||
if (isPrintableChar(key.getKeyChar())) {
|
||||
if (isPrintableChar(key.getKeyChar()) || activeTemplateWithLeftRightMotion(editor, key)) {
|
||||
VimPlugin.getChange().insertBeforeCursor(editor, context);
|
||||
}
|
||||
|
||||
@@ -837,7 +838,7 @@ public class ChangeGroup {
|
||||
return res;
|
||||
}
|
||||
|
||||
public boolean isPrintableChar(char c) {
|
||||
private boolean isPrintableChar(char c) {
|
||||
Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
|
||||
return (!Character.isISOControl(c)) &&
|
||||
c != KeyEvent.CHAR_UNDEFINED &&
|
||||
@@ -845,6 +846,13 @@ public class ChangeGroup {
|
||||
block != Character.UnicodeBlock.SPECIALS;
|
||||
}
|
||||
|
||||
private boolean activeTemplateWithLeftRightMotion(Editor editor, KeyStroke keyStroke) {
|
||||
Template template =
|
||||
TemplateManager.getInstance(Objects.requireNonNull(editor.getProject())).getActiveTemplate(editor);
|
||||
return template != null &&
|
||||
(keyStroke.getKeyCode() == KeyEvent.VK_LEFT || keyStroke.getKeyCode() == KeyEvent.VK_RIGHT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes count lines including the current line
|
||||
*
|
||||
|
@@ -18,6 +18,7 @@
|
||||
|
||||
package com.maddyhome.idea.vim.group.visual
|
||||
|
||||
import com.intellij.codeInsight.template.TemplateManager
|
||||
import com.intellij.openapi.editor.Caret
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.editor.LogicalPosition
|
||||
@@ -229,6 +230,7 @@ class VisualMotionGroup {
|
||||
if (!CommandState.inSelectMode(editor)) return
|
||||
|
||||
CommandState.getInstance(editor).popState()
|
||||
SelectionVimListenerSuppressor.lock()
|
||||
editor.caretModel.allCarets.forEach {
|
||||
it.removeSelection()
|
||||
it.vimSelectionStartSetToNull()
|
||||
@@ -240,6 +242,7 @@ class VisualMotionGroup {
|
||||
}
|
||||
}
|
||||
}
|
||||
SelectionVimListenerSuppressor.unlock()
|
||||
updateCaretColours(editor)
|
||||
ChangeGroup.resetCursor(editor, false)
|
||||
}
|
||||
@@ -270,6 +273,9 @@ class VisualMotionGroup {
|
||||
ChangeGroup.resetCursor(editor, resetCaretToInsert)
|
||||
exitVisual(editor)
|
||||
exitSelectModeAndResetKeyHandler(editor, true)
|
||||
|
||||
TemplateManager.getInstance(editor.project).getActiveTemplate(editor)
|
||||
.run { VimPlugin.getChange().insertBeforeCursor(editor, EditorDataContext(editor)) }
|
||||
KeyHandler.getInstance().reset(editor)
|
||||
}
|
||||
}
|
||||
|
@@ -218,11 +218,14 @@ public abstract class VimTestCase extends UsefulTestCase {
|
||||
public void doTest(final List<KeyStroke> keys,
|
||||
String before,
|
||||
String after,
|
||||
CommandState.Mode modeAfter,
|
||||
CommandState.SubMode subModeAfter) {
|
||||
CommandState.Mode modeAfter, CommandState.SubMode subModeAfter) {
|
||||
configureByText(before);
|
||||
typeText(keys);
|
||||
myFixture.checkResult(after);
|
||||
assertState(modeAfter, subModeAfter);
|
||||
}
|
||||
|
||||
protected void assertState(CommandState.Mode modeAfter, CommandState.SubMode subModeAfter) {
|
||||
assertCaretsColour();
|
||||
assertMode(modeAfter);
|
||||
assertSubMode(subModeAfter);
|
||||
|
136
test/org/jetbrains/plugins/ideavim/group/visual/TemplateTest.kt
Normal file
136
test/org/jetbrains/plugins/ideavim/group/visual/TemplateTest.kt
Normal file
@@ -0,0 +1,136 @@
|
||||
@file:Suppress("RemoveCurlyBracesFromTemplate")
|
||||
|
||||
package org.jetbrains.plugins.ideavim.group.visual
|
||||
|
||||
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
|
||||
import com.intellij.ide.DataManager
|
||||
import com.intellij.injected.editor.EditorWindow
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.refactoring.rename.inplace.VariableInplaceRenameHandler
|
||||
import com.intellij.testFramework.fixtures.CodeInsightTestUtil.doInlineRename
|
||||
import com.maddyhome.idea.vim.command.CommandState
|
||||
import com.maddyhome.idea.vim.group.VimListenerManager
|
||||
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
|
||||
import org.jetbrains.plugins.ideavim.VimTestCase
|
||||
|
||||
/**
|
||||
* @author Alex Plate
|
||||
*/
|
||||
class TemplateTest : VimTestCase() {
|
||||
|
||||
lateinit var disposable: Disposable
|
||||
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
disposable = Disposer.newDisposable()
|
||||
}
|
||||
|
||||
override fun tearDown() {
|
||||
super.tearDown()
|
||||
Disposer.dispose(disposable)
|
||||
}
|
||||
|
||||
fun `test simple rename`() {
|
||||
configureByJavaText("""
|
||||
class Hello {
|
||||
public static void main() {
|
||||
int my${c}Var = 5;
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
doInlineRename(VariableInplaceRenameHandler(), "myNewVar", myFixture)
|
||||
myFixture.checkResult("""
|
||||
class Hello {
|
||||
public static void main() {
|
||||
int my${c}NewVar = 5;
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test type rename`() {
|
||||
configureByJavaText("""
|
||||
class Hello {
|
||||
public static void main() {
|
||||
int my${c}Var = 5;
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
startRenaming(VariableInplaceRenameHandler())
|
||||
assertState(CommandState.Mode.SELECT, CommandState.SubMode.VISUAL_CHARACTER)
|
||||
|
||||
typeText(parseKeys("myNewVar", "<CR>"))
|
||||
|
||||
assertState(CommandState.Mode.INSERT, CommandState.SubMode.NONE)
|
||||
myFixture.checkResult("""
|
||||
class Hello {
|
||||
public static void main() {
|
||||
int myNewVar${c} = 5;
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test prepend`() {
|
||||
configureByJavaText("""
|
||||
class Hello {
|
||||
public static void main() {
|
||||
int my${c}Var = 5;
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
startRenaming(VariableInplaceRenameHandler())
|
||||
assertState(CommandState.Mode.SELECT, CommandState.SubMode.VISUAL_CHARACTER)
|
||||
|
||||
typeText(parseKeys("<Left>"))
|
||||
assertState(CommandState.Mode.INSERT, CommandState.SubMode.NONE)
|
||||
typeText(parseKeys("pre", "<CR>"))
|
||||
|
||||
assertState(CommandState.Mode.INSERT, CommandState.SubMode.NONE)
|
||||
myFixture.checkResult("""
|
||||
class Hello {
|
||||
public static void main() {
|
||||
int mypre${c}Var = 5;
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test escape`() {
|
||||
configureByJavaText("""
|
||||
class Hello {
|
||||
public static void main() {
|
||||
int my${c}Var = 5;
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
startRenaming(VariableInplaceRenameHandler())
|
||||
assertState(CommandState.Mode.SELECT, CommandState.SubMode.VISUAL_CHARACTER)
|
||||
|
||||
typeText(parseKeys("<ESC>"))
|
||||
|
||||
assertState(CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
|
||||
myFixture.checkResult("""
|
||||
class Hello {
|
||||
public static void main() {
|
||||
int my${c}Var = 5;
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
|
||||
private fun startRenaming(handler: VariableInplaceRenameHandler): Editor {
|
||||
val editor = if (myFixture.editor is EditorWindow) (myFixture.editor as EditorWindow).delegate else myFixture.editor
|
||||
VimListenerManager.addEditorListeners(editor)
|
||||
|
||||
TemplateManagerImpl.setTemplateTesting(myFixture.project, disposable)
|
||||
handler.doRename(myFixture.elementAtCaret, editor, dataContext)
|
||||
return editor
|
||||
}
|
||||
|
||||
private val dataContext
|
||||
get() = DataManager.getInstance().getDataContext(myFixture.editor.component)
|
||||
}
|
Reference in New Issue
Block a user