Skip to content

Commit

Permalink
fix(VIM-2462): reset caret when disabling plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
citizenmatt authored and AlexPl292 committed Nov 9, 2021
1 parent d88f38c commit 296b714
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/maddyhome/idea/vim/group/EditorGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.intellij.openapi.editor.event.CaretEvent;
import com.intellij.openapi.editor.event.CaretListener;
import com.intellij.openapi.editor.ex.EditorGutterComponentEx;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
import com.intellij.openapi.project.Project;
import com.maddyhome.idea.vim.KeyHandler;
import com.maddyhome.idea.vim.VimPlugin;
Expand Down Expand Up @@ -230,9 +229,9 @@ public void editorDeinit(@NotNull Editor editor, boolean isReleased) {
deinitLineNumbers(editor, isReleased);
UserDataManager.unInitializeEditor(editor);
VimPlugin.getKey().unregisterShortcutKeys(editor);
editor.getSettings().setBlockCursor(EditorSettingsExternalizable.getInstance().isBlockCursor());
editor.getSettings().setRefrainFromScrolling(isRefrainFromScrolling);
DocumentManager.INSTANCE.removeListeners(editor.getDocument());
CaretVisualAttributesHelperKt.removeCaretsVisualAttributes(editor);
}

public void notifyIdeaJoin(@Nullable Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ fun Editor.updateCaretsVisualAttributes() {
updateSecondaryCaretsVisualAttributes()
}

/**
* Remove custom visual attributes and reset to defaults
*
* Used when Vim emulation is disabled
*/
fun removeCaretsVisualAttributes(editor: Editor) {
editor.caretModel.allCarets.forEach { it.visualAttributes = CaretVisualAttributes.DEFAULT }
editor.settings.isBlockCursor = EditorSettingsExternalizable.getInstance().isBlockCursor
}

fun Editor.guicursorMode(): GuiCursorMode {
if (subMode == CommandState.SubMode.REPLACE_CHARACTER) {
// Can be true for NORMAL and VISUAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.jetbrains.plugins.ideavim.helper

import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
import com.maddyhome.idea.vim.helper.VimBehaviorDiffers
import com.maddyhome.idea.vim.helper.buildGreater212
Expand Down Expand Up @@ -239,6 +240,30 @@ class CaretVisualAttributesHelperTest : VimTestCase() {
}
}

@TestWithoutNeovim(SkipNeovimReason.NOT_VIM_TESTING)
fun `test reset caret shape when disable plugin`() {
configureByText("I found it in a legendary land")
typeText(parseKeys("i"))
VimPlugin.setEnabled(false)
assertCaretVisualAttributes("DEFAULT", 1.0f)
}

@TestWithoutNeovim(SkipNeovimReason.NOT_VIM_TESTING)
fun `test reset caret shape when disable plugin 2`() {
configureByText("I found it in a legendary land")
typeText(parseKeys("v2e"))
VimPlugin.setEnabled(false)
assertCaretVisualAttributes("DEFAULT", 1.0f)
}

@TestWithoutNeovim(SkipNeovimReason.NOT_VIM_TESTING)
fun `test reset caret shape when disable plugin 3`() {
configureByText("I found it in a legendary land")
typeText(parseKeys("r"))
VimPlugin.setEnabled(false)
assertCaretVisualAttributes("DEFAULT", 1.0f)
}

private fun assertCaretVisualAttributes(expectedShape: String, expectedThickness: Float) {
assertCaretVisualAttributes(myFixture.editor.caretModel.primaryCaret, expectedShape, expectedThickness)
}
Expand Down

0 comments on commit 296b714

Please sign in to comment.