11package com.devfive.vim_switch_ko
22
3- import com.intellij.ide.AppLifecycleListener
43import com.intellij.ide.IdeEventQueue
54import com.intellij.ide.plugins.PluginManager
65import com.intellij.ide.plugins.cl.PluginAwareClassLoader
6+ import com.intellij.openapi.editor.impl.EditorComponentImpl
77import com.intellij.openapi.extensions.PluginId
88import com.intellij.openapi.fileEditor.FileEditorManager
99import com.intellij.openapi.project.DumbAware
1010import com.intellij.openapi.project.Project
1111import com.intellij.openapi.project.ProjectManager
1212import com.intellij.openapi.startup.ProjectActivity
13- import com.intellij.openapi.startup.StartupActivity
1413import java.awt.event.KeyEvent
1514import java.awt.im.InputContext
15+ import javax.swing.FocusManager
1616
1717internal 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 )
0 commit comments