Skip to content

Commit e677a4e

Browse files
committed
Only Apply Blocking IM in the Editor component
Update Change IM when the Editor component is focused with VIM Normal Mode
1 parent 35b2118 commit e677a4e

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pluginGroup = com.devfive.vim_switch_ko
44
pluginName = vim-switch-ko
55
pluginRepositoryUrl = https://github.com/owjs3901/vim-switch-ko
66
# SemVer format -> https://semver.org
7-
pluginVersion = 0.0.1
7+
pluginVersion = 0.0.2
88

99
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
1010
pluginSinceBuild = 223

src/main/kotlin/com/devfive/vim_switch_ko/VimSwitchKoListener.kt

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.devfive.vim_switch_ko
22

3-
import com.intellij.ide.AppLifecycleListener
43
import com.intellij.ide.IdeEventQueue
54
import com.intellij.ide.plugins.PluginManager
65
import com.intellij.ide.plugins.cl.PluginAwareClassLoader
6+
import com.intellij.openapi.editor.impl.EditorComponentImpl
77
import com.intellij.openapi.extensions.PluginId
88
import com.intellij.openapi.fileEditor.FileEditorManager
99
import com.intellij.openapi.project.DumbAware
1010
import com.intellij.openapi.project.Project
1111
import com.intellij.openapi.project.ProjectManager
1212
import com.intellij.openapi.startup.ProjectActivity
13-
import com.intellij.openapi.startup.StartupActivity
1413
import java.awt.event.KeyEvent
1514
import java.awt.im.InputContext
15+
import javax.swing.FocusManager
1616

1717
internal class VimSwitchKoListener : ProjectActivity, DumbAware {
1818
override suspend fun execute(project: Project) {
@@ -26,31 +26,33 @@ internal class VimSwitchKoListener : ProjectActivity, DumbAware {
2626
return enabled
2727
}
2828

29-
fun toEnglishIME(event: KeyEvent) {
30-
val context = (event.source.javaClass.getMethod("getInputContext").invoke(event.source) as InputContext)
31-
context.setCharacterSubsets(null)
29+
fun toEnglishIME(context: InputContext?) {
30+
context?.setCharacterSubsets(null)
3231
InputContext.getInstance().setCharacterSubsets(null)
3332
}
3433

35-
var editors:List<Any>? = null
36-
fun loadEditors(){
34+
fun isFocusedEditor(): Boolean {
35+
return FocusManager.getCurrentManager().focusOwner is EditorComponentImpl
36+
}
37+
38+
var editors: List<Any>? = null
39+
fun loadEditors() {
3740
val pluginDescriptor = PluginManager.getLoadedPlugins().find { it.pluginId == PluginId.getId("IdeaVIM") }
38-
if(pluginDescriptor == null)
41+
if (pluginDescriptor == null)
3942
return
4043
val pluginClassLoader = pluginDescriptor.pluginClassLoader as PluginAwareClassLoader
4144
val injectClass = pluginClassLoader.tryLoadingClass("com.maddyhome.idea.vim.api.VimInjectorKt", true)
4245
val instance = injectClass?.getMethod("getInjector")!!.invoke(null)
4346
val editorGroup = instance.javaClass.getMethod("getEditorGroup").invoke(instance)
4447
editors = editorGroup.javaClass.getMethod("getEditors").invoke(editorGroup) as ArrayList<Any>
45-
4648
}
4749

4850

49-
fun isCurrentModeNormal(): Boolean {
51+
fun isCurrentModeNormal(forcedLoad: Boolean = false): Boolean {
5052
ProjectManager.getInstance().openProjects.forEach { project ->
51-
if(editors == null){
53+
if (editors == null || forcedLoad) {
5254
loadEditors()
53-
if(editors == null)
55+
if (editors == null)
5456
return false
5557
}
5658
val currentFile = FileEditorManager.getInstance(project).selectedEditor?.file?.path ?: return false
@@ -64,27 +66,33 @@ internal class VimSwitchKoListener : ProjectActivity, DumbAware {
6466
}
6567
return false
6668
}
69+
FocusManager.getCurrentManager().addPropertyChangeListener("focusOwner") {
70+
if (isFocusedEditor() && enableIdeaVIM() && isCurrentModeNormal(true))
71+
toEnglishIME((it.newValue as EditorComponentImpl).inputContext)
72+
}
73+
6774
IdeEventQueue.getInstance().addDispatcher(IdeEventQueue.EventDispatcher { e ->
6875
if (e !is KeyEvent) return@EventDispatcher false
69-
if (!enableIdeaVIM()) {
76+
if (!isFocusedEditor())
77+
return@EventDispatcher false
78+
79+
if (!enableIdeaVIM())
7080
return@EventDispatcher false
71-
}
7281
if (e.id != KeyEvent.KEY_PRESSED) {
7382
if (e.keyCode == 0 && e.keyChar.code == 65535 && isCurrentModeNormal()) {
7483
// Switching to English IME when switching korean mode in normal mode
75-
toEnglishIME(e)
84+
toEnglishIME(e.component.inputContext)
7685
}
7786
return@EventDispatcher false
7887
}
7988

8089
if (e.keyCode == KeyEvent.VK_ESCAPE) {
8190
// Always switch to English IME when pressing ESC
82-
toEnglishIME(e)
91+
toEnglishIME(e.component.inputContext)
8392
} else if (e.isControlDown && e.keyCode == KeyEvent.VK_C) {
8493
// Switch to English IME when pressing Ctrl+C in normal mode
85-
if (!isCurrentModeNormal()) {
86-
toEnglishIME(e)
87-
}
94+
if (!isCurrentModeNormal())
95+
toEnglishIME(e.component.inputContext)
8896
}
8997
return@EventDispatcher false
9098
}, null)

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Switch keyboard input source in Vim's normal mode. This plugin is useful for Kor
77
]]></description>
88

99
<depends>com.intellij.modules.platform</depends>
10+
<idea-version since-build="231"/>
1011

1112
<extensions defaultExtensionNs="com.intellij">
1213
<postStartupActivity implementation="com.devfive.vim_switch_ko.VimSwitchKoListener"/>

0 commit comments

Comments
 (0)