Skip to content

Commit

Permalink
migrate some class to kotlin and add input source setting
Browse files Browse the repository at this point in the history
  • Loading branch information
rieonke committed Jun 9, 2017
1 parent 223325e commit aa4e566
Show file tree
Hide file tree
Showing 11 changed files with 583 additions and 281 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ sourceSets {
main {
java {
srcDirs 'src'
srcDirs 'gen'
}
resources {
srcDirs 'resources'
Expand All @@ -37,7 +36,7 @@ apply plugin: "kotlin"

tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }

version="1.1"
version="1.2"
intellij {
version 'IU-LATEST-TRUNK-SNAPSHOT'
pluginName 'AutoSwitchIm'
Expand Down
12 changes: 9 additions & 3 deletions resources/META-INF/plugin.xml
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[
Expand All @@ -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>
Expand All @@ -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>
]]>
Expand All @@ -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>
Expand All @@ -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 modified resources/native/libAutoSwitchInputSource.jnilib
Binary file not shown.
192 changes: 192 additions & 0 deletions src/cn/rieon/idea/plugin/AutoSwitchIm/component/AutoSwitchComponent.kt
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")
}

}
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)
}


}

Loading

0 comments on commit aa4e566

Please sign in to comment.