-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate some class to kotlin and add input source setting
- Loading branch information
Showing
11 changed files
with
583 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<idea-plugin> | ||
<id>AutoSwitchIm</id> | ||
<name>Auto Switch Input Source In IdeaVim Mode</name> | ||
<version>1.1</version> | ||
<version>1.2</version> | ||
<vendor email="[email protected]" url="http://rieon.cn">Rieon Ke</vendor> | ||
|
||
<description><![CDATA[ | ||
|
@@ -13,6 +13,8 @@ | |
<li>Auto switch to the last IME while idea re-focused</li> | ||
<h1>Author and the project page </h1> | ||
<a href="https://github.com/rieonke/idea-auto-switch-im">Follow this link to Github</a> | ||
<br> | ||
<br> | ||
<h2>只有MAC可用</h2> | ||
在IdeaVim中自动切换输入法<br> | ||
|
@@ -25,12 +27,14 @@ | |
]]></description> | ||
|
||
<change-notes><![CDATA[ | ||
<li>1.2 Fixed known bugs in JNI <br> add switch input source options setting</li> | ||
<li>1.1 use JNI to replace native module</li> | ||
<li>1.0 basic switch works</li> | ||
<br/> | ||
<br/> | ||
<li>1.2 修复JNI部分的BUG<br/>提供了切换输入法选项设置</li> | ||
<li>1.1 使用JNI替换掉原来的本地执行文件</li> | ||
<li>1.0 basic switch works</li> | ||
]]> | ||
|
@@ -46,7 +50,9 @@ | |
--> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
<!-- Add your extensions here --> | ||
<applicationConfigurable instance="cn.rieon.idea.plugin.AutoSwitchIm.ui.InputSourceConfigure"/> | ||
<applicationService serviceInterface="cn.rieon.idea.plugin.AutoSwitchIm.provider.ConfigurationProvider" | ||
serviceImplementation="cn.rieon.idea.plugin.AutoSwitchIm.provider.ConfigurationProvider"/> | ||
</extensions> | ||
|
||
<actions> | ||
|
@@ -55,7 +61,7 @@ | |
|
||
<application-components> | ||
<component> | ||
<implementation-class>cn.rieon.idea.vim.AutoSwitchComponent</implementation-class> | ||
<implementation-class>cn.rieon.idea.plugin.AutoSwitchIm.component.AutoSwitchComponent</implementation-class> | ||
</component> | ||
</application-components> | ||
</idea-plugin> |
Binary file not shown.
192 changes: 192 additions & 0 deletions
192
src/cn/rieon/idea/plugin/AutoSwitchIm/component/AutoSwitchComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package cn.rieon.idea.plugin.AutoSwitchIm.component | ||
|
||
import cn.rieon.idea.plugin.AutoSwitchIm.provider.ConfigurationProvider | ||
import cn.rieon.idea.plugin.AutoSwitchIm.util.InputSourceUtil | ||
import com.intellij.ide.FrameStateListener | ||
import com.intellij.ide.FrameStateManager | ||
import com.intellij.notification.Notification | ||
import com.intellij.notification.NotificationType | ||
import com.intellij.notification.Notifications | ||
import com.intellij.openapi.command.CommandAdapter | ||
import com.intellij.openapi.command.CommandEvent | ||
import com.intellij.openapi.command.CommandListener | ||
import com.intellij.openapi.command.CommandProcessor | ||
import com.intellij.openapi.components.ApplicationComponent | ||
import com.intellij.openapi.diagnostic.Logger | ||
|
||
import java.util.* | ||
|
||
/** | ||
* @author Rieon Ke <rieon></rieon>@rieon.cn> | ||
* * | ||
* @version 1.0.0 | ||
* * | ||
* @since 2017/5/20 | ||
*/ | ||
class AutoSwitchComponent : ApplicationComponent { | ||
|
||
/** | ||
* last input source | ||
*/ | ||
private var lastInputSource: String? = null | ||
|
||
private var inNormal: Boolean? = true | ||
|
||
private var configurationProvider: ConfigurationProvider? = null | ||
|
||
/** | ||
* init component and add listener | ||
*/ | ||
override fun initComponent() { | ||
|
||
LOG.info("INIT COMPONENT") | ||
|
||
|
||
lastInputSource = currentInputSourceId | ||
CommandProcessor.getInstance().addCommandListener(commandListener) | ||
FrameStateManager.getInstance().addListener(frameStateListener) | ||
|
||
configurationProvider = ConfigurationProvider.instance | ||
|
||
if (configurationProvider!!.selectedInputSource != null) { | ||
|
||
switchToInputSource = configurationProvider!!.selectedInputSource!! | ||
|
||
LOG.info("LOAD PREVIOUS CONFIG") | ||
LOG.info("USE CONFIG INPUT SOURCE " + switchToInputSource) | ||
} | ||
|
||
LOG.info("INIT SUCCESSFUL") | ||
} | ||
|
||
/** | ||
* auto switch input source while plugin state changed | ||
* @return CommandListener | ||
*/ | ||
private val commandListener: CommandListener | ||
get() = object : CommandAdapter() { | ||
override fun beforeCommandFinished(event: CommandEvent?) { | ||
|
||
val commandName = event!!.commandName | ||
|
||
if (VimStateSwitchToABCOnly.contains(commandName)) { | ||
lastInputSource = currentInputSourceId | ||
inNormal = true | ||
if (lastInputSource == null || lastInputSource == switchToInputSource) | ||
return | ||
switchTo(switchToInputSource) | ||
} else if (VimStateSwitchToLastIm.contains(commandName)) { | ||
val current = currentInputSourceId | ||
inNormal = false | ||
if (current == null || current == lastInputSource) | ||
return | ||
switchTo(lastInputSource) | ||
} else if ("Typing" == commandName) { | ||
inNormal = false | ||
} else if ("" == commandName) { | ||
if (inNormal!!) | ||
switchTo(switchToInputSource) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* restore last input source while idea re-focused | ||
* @return FrameStateListener | ||
*/ | ||
private val frameStateListener: FrameStateListener | ||
get() = object : FrameStateListener.Adapter() { | ||
override fun onFrameDeactivated() { | ||
lastInputSource = currentInputSourceId | ||
} | ||
|
||
override fun onFrameActivated() { | ||
val current = currentInputSourceId | ||
if (current == null || current == lastInputSource) | ||
return | ||
switchTo(lastInputSource) | ||
} | ||
} | ||
|
||
/** | ||
* get current input source | ||
* @return String currentInputSource | ||
*/ | ||
private val currentInputSourceId: String? | ||
get() { | ||
|
||
val current = InputSourceUtil.currentInputSource | ||
|
||
LOG.info("CURRENT INPUT SOURCE " + current) | ||
if (current.isEmpty()) { | ||
val notification = Notification("Switch IME Error", "Switch IME Error", "Get current input source faild", NotificationType.ERROR) | ||
Notifications.Bus.notify(notification) | ||
LOG.info("GET CURRENT INPUT SOURCE FAILED") | ||
} | ||
|
||
return current | ||
|
||
} | ||
|
||
internal fun switchTo(source: String?) { | ||
|
||
LOG.info("SWITCH TO INPUT SOURCE " + source!!) | ||
if (!InputSourceUtil.switchTo(source, InputSourceUtil.BY_ID)) { | ||
val notification = Notification("Switch IME Error", "Switch IME Error", "Switch IME Failed", NotificationType.ERROR) | ||
Notifications.Bus.notify(notification) | ||
LOG.info("SWITCH TO INPUT SOURCE FAILED") | ||
} | ||
} | ||
|
||
override fun disposeComponent() { | ||
// TODO: insert component disposal logic here | ||
} | ||
|
||
override fun getComponentName(): String { | ||
return "AutoSwitchComponent" | ||
} | ||
|
||
companion object { | ||
|
||
private val LOG = Logger.getInstance(AutoSwitchComponent::class.java) | ||
|
||
var switchToInputSource = "com.apple.keylayout.ABC" | ||
|
||
|
||
/** | ||
* switch input source in these plugin states | ||
*/ | ||
private val VimStateSwitchToABCOnly = Arrays.asList( | ||
"Vim Exit Visual Mode", | ||
"Vim Toggle Line Selection", | ||
"Vim Toggle Block Selection", | ||
"Vim Toggle Character Selection", | ||
"Vim Exit Insert Mode") | ||
|
||
/** | ||
* switch to last input source in these plugin states | ||
*/ | ||
private val VimStateSwitchToLastIm = Arrays.asList( | ||
"Vim Enter", | ||
"Vim Insert at Line Start", | ||
"Vim Insert New Line Above", | ||
"Vim Insert New Line Below", | ||
"Vim Insert Previous Text", | ||
"Vim Insert Previous Text", | ||
"Vim Insert Register", | ||
"Vim Toggle Insert/Replace", | ||
"Vim Change Line", | ||
"Vim Change Character", | ||
"Vim Change Characters", | ||
"Vim Replace", | ||
"Vim Insert After Cursor", | ||
"Vim Insert After Line End", | ||
"Vim Insert Before Cursor", | ||
"Vim Insert Before First non-Blank", | ||
"Vim Insert Character Above Cursor", | ||
"Vim Insert Character Below Cursor", | ||
"Vim Delete Inserted Text", | ||
"Vim Delete Previous Word") | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
src/cn/rieon/idea/plugin/AutoSwitchIm/provider/ConfigurationProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cn.rieon.idea.plugin.AutoSwitchIm.provider | ||
|
||
import cn.rieon.idea.plugin.AutoSwitchIm.component.AutoSwitchComponent | ||
import com.intellij.openapi.components.PersistentStateComponent | ||
import com.intellij.openapi.components.ServiceManager | ||
import com.intellij.openapi.components.State | ||
import com.intellij.openapi.components.Storage | ||
|
||
/** | ||
* @author oleg | ||
*/ | ||
@State(name = "ConfigurationProvider", storages = arrayOf(Storage("selected_input_source_setting.xml"))) | ||
class ConfigurationProvider : PersistentStateComponent<ConfigurationProvider.State> { | ||
|
||
class State { | ||
|
||
internal var selectedInputSource: String? = null | ||
|
||
} | ||
|
||
private var myState: State? = State() | ||
|
||
override fun getState(): State? { | ||
return myState | ||
} | ||
|
||
override fun loadState(state: State) { | ||
myState = state | ||
} | ||
|
||
var selectedInputSource: String? | ||
get() { | ||
|
||
if (myState != null) { | ||
|
||
return myState!!.selectedInputSource | ||
} | ||
|
||
return null | ||
} | ||
set(selected) { | ||
|
||
myState!!.selectedInputSource = selected | ||
AutoSwitchComponent.switchToInputSource = selected as String | ||
|
||
} | ||
|
||
companion object { | ||
|
||
val instance: ConfigurationProvider | ||
get() = ServiceManager.getService(ConfigurationProvider::class.java) | ||
} | ||
|
||
|
||
} | ||
|
Oops, something went wrong.