mirror of
https://github.com/chylex/IntelliJ-Keyboard-Master.git
synced 2025-05-05 20:34:07 +02:00
Minor refactoring
This commit is contained in:
parent
c9fd077253
commit
a2b79d4f52
src/main
kotlin/com/chylex/intellij/keyboardmaster
configuration
feature/codeCompletion
resources/META-INF
@ -1,6 +1,6 @@
|
||||
package com.chylex.intellij.keyboardmaster.configuration
|
||||
|
||||
import com.chylex.intellij.keyboardmaster.lookup.ProjectLookupListener
|
||||
import com.chylex.intellij.keyboardmaster.feature.codeCompletion.CodeCompletionPopupConfiguration
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.components.PersistentStateComponent
|
||||
import com.intellij.openapi.components.State
|
||||
@ -31,8 +31,8 @@ class PluginConfiguration : PersistentStateComponent<PluginConfiguration> {
|
||||
instance.apply(callback).apply(this::update)
|
||||
}
|
||||
|
||||
private fun update(instance: PluginConfiguration) {
|
||||
ProjectLookupListener.updateShortcuts(instance)
|
||||
private fun update(instance: PluginConfiguration) = with(instance) {
|
||||
CodeCompletionPopupConfiguration.updateShortcuts(codeCompletionItemShortcuts, codeCompletionNextPageShortcut)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,42 @@
|
||||
package com.chylex.intellij.keyboardmaster.feature.codeCompletion
|
||||
|
||||
import com.chylex.intellij.keyboardmaster.configuration.PluginConfiguration
|
||||
import com.intellij.util.containers.IntIntHashMap
|
||||
|
||||
object CodeCompletionPopupConfiguration {
|
||||
const val SHORTCUT_NONE = -1
|
||||
const val SHORTCUT_NEXT_PAGE = 0
|
||||
|
||||
private val charToShortcutMap = IntIntHashMap(16, SHORTCUT_NONE)
|
||||
private var hintTexts = mutableListOf<String>()
|
||||
|
||||
val itemShortcutCount
|
||||
get() = hintTexts.size
|
||||
|
||||
init {
|
||||
PluginConfiguration.load()
|
||||
}
|
||||
|
||||
fun updateShortcuts(itemShortcutChars: String, nextPageShortcutCode: Int) {
|
||||
charToShortcutMap.clear()
|
||||
if (nextPageShortcutCode != 0) {
|
||||
charToShortcutMap[nextPageShortcutCode] = SHORTCUT_NEXT_PAGE
|
||||
}
|
||||
for ((index, char) in itemShortcutChars.withIndex()) {
|
||||
charToShortcutMap[char.code] = index + 1
|
||||
}
|
||||
|
||||
hintTexts.clear()
|
||||
for (char in itemShortcutChars) {
|
||||
hintTexts.add(" [$char]")
|
||||
}
|
||||
}
|
||||
|
||||
fun getShortcut(char: Char): Int {
|
||||
return charToShortcutMap[char.code]
|
||||
}
|
||||
|
||||
fun getHintText(index: Int): String {
|
||||
return hintTexts[index]
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.chylex.intellij.keyboardmaster.lookup
|
||||
package com.chylex.intellij.keyboardmaster.feature.codeCompletion
|
||||
|
||||
import com.intellij.codeInsight.lookup.LookupFocusDegree
|
||||
import com.intellij.codeInsight.lookup.LookupManager
|
||||
@ -10,10 +10,9 @@ import com.intellij.openapi.editor.actionSystem.TypedActionHandler
|
||||
import com.intellij.ui.ScrollingUtil
|
||||
|
||||
/**
|
||||
* When typing digits 1-9 inside a code completion popup menu, selects the n-th item in the list.
|
||||
* When typing the digit 0, moves down the list by 9 items, wrapping around if needed.
|
||||
* Handles configured key bindings inside a code completion popup menu.
|
||||
*/
|
||||
class LookupTypedActionHandler(originalHandler: TypedActionHandler?) : TypedActionHandlerBase(originalHandler) {
|
||||
class CodeCompletionPopupKeyHandler(originalHandler: TypedActionHandler?) : TypedActionHandlerBase(originalHandler) {
|
||||
override fun execute(editor: Editor, charTyped: Char, dataContext: DataContext) {
|
||||
if (!executeImpl(editor, charTyped)) {
|
||||
myOriginalHandler?.execute(editor, charTyped, dataContext)
|
||||
@ -21,8 +20,8 @@ class LookupTypedActionHandler(originalHandler: TypedActionHandler?) : TypedActi
|
||||
}
|
||||
|
||||
private fun executeImpl(editor: Editor, charTyped: Char): Boolean {
|
||||
val shortcutItem = ProjectLookupListener.getShortcut(charTyped)
|
||||
if (shortcutItem == -1) {
|
||||
val shortcutItem = CodeCompletionPopupConfiguration.getShortcut(charTyped)
|
||||
if (shortcutItem == CodeCompletionPopupConfiguration.SHORTCUT_NONE) {
|
||||
return false
|
||||
}
|
||||
|
||||
@ -31,16 +30,16 @@ class LookupTypedActionHandler(originalHandler: TypedActionHandler?) : TypedActi
|
||||
return false
|
||||
}
|
||||
|
||||
val offset = ProjectLookupListener.getLookupOffset(lookup)
|
||||
val offset = CodeCompletionPopupListener.getLookupOffset(lookup)
|
||||
|
||||
if (shortcutItem == 0) {
|
||||
if (shortcutItem == CodeCompletionPopupConfiguration.SHORTCUT_NEXT_PAGE) {
|
||||
val list = lookup.list
|
||||
val itemCount = list.model.size
|
||||
|
||||
val shortcutCount = ProjectLookupListener.itemShortcutCount
|
||||
val shortcutCount = CodeCompletionPopupConfiguration.itemShortcutCount
|
||||
val topIndex = (offset + shortcutCount).let { if (it >= itemCount) 0 else it }
|
||||
|
||||
ProjectLookupListener.setLookupOffset(lookup, topIndex)
|
||||
CodeCompletionPopupListener.setLookupOffset(lookup, topIndex)
|
||||
lookup.selectedIndex = topIndex
|
||||
ScrollingUtil.ensureRangeIsVisible(list, topIndex, topIndex + shortcutCount - 1)
|
||||
lookup.markSelectionTouched()
|
@ -1,48 +1,19 @@
|
||||
package com.chylex.intellij.keyboardmaster.lookup
|
||||
package com.chylex.intellij.keyboardmaster.feature.codeCompletion
|
||||
|
||||
import com.chylex.intellij.keyboardmaster.configuration.PluginConfiguration
|
||||
import com.intellij.codeInsight.lookup.Lookup
|
||||
import com.intellij.codeInsight.lookup.LookupElementPresentation
|
||||
import com.intellij.codeInsight.lookup.LookupManagerListener
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.util.containers.IntIntHashMap
|
||||
|
||||
/**
|
||||
* Adds hints to code completion items with the digit that selects it.
|
||||
* Adds hints to code completion popup items with the character that selects the item.
|
||||
*/
|
||||
class ProjectLookupListener : LookupManagerListener {
|
||||
class CodeCompletionPopupListener : LookupManagerListener {
|
||||
companion object {
|
||||
private val OFFSET_KEY = Key.create<Int>("chylexKeyboardMasterOffset")
|
||||
private val IS_MODIFIED_KEY = Key.create<Boolean>("chylexKeyboardMasterModified")
|
||||
|
||||
private var hintTexts = mutableListOf<String>()
|
||||
private val charToShortcutMap = IntIntHashMap(16, -1)
|
||||
|
||||
val itemShortcutCount
|
||||
get() = hintTexts.size
|
||||
|
||||
init {
|
||||
PluginConfiguration.load()
|
||||
}
|
||||
|
||||
fun updateShortcuts(configuration: PluginConfiguration) {
|
||||
hintTexts.clear()
|
||||
for (char in configuration.codeCompletionItemShortcuts) {
|
||||
hintTexts.add(" [$char]")
|
||||
}
|
||||
|
||||
charToShortcutMap.clear()
|
||||
configuration.codeCompletionNextPageShortcut.takeUnless { it == 0 }?.let { charToShortcutMap[it] = 0 }
|
||||
for ((index, char) in configuration.codeCompletionItemShortcuts.withIndex()) {
|
||||
charToShortcutMap[char.code] = index + 1
|
||||
}
|
||||
}
|
||||
|
||||
fun getShortcut(char: Char): Int {
|
||||
return charToShortcutMap[char.code]
|
||||
}
|
||||
|
||||
fun getLookupOffset(lookup: LookupImpl): Int {
|
||||
val offset = lookup.getUserData(OFFSET_KEY)
|
||||
if (offset == null || offset >= lookup.list.model.size) {
|
||||
@ -59,7 +30,7 @@ class ProjectLookupListener : LookupManagerListener {
|
||||
}
|
||||
|
||||
override fun activeLookupChanged(oldLookup: Lookup?, newLookup: Lookup?) {
|
||||
if (newLookup !is LookupImpl || newLookup.getUserData(IS_MODIFIED_KEY) == true || itemShortcutCount == 0) {
|
||||
if (newLookup !is LookupImpl || newLookup.getUserData(IS_MODIFIED_KEY) == true || CodeCompletionPopupConfiguration.itemShortcutCount == 0) {
|
||||
return
|
||||
}
|
||||
|
||||
@ -71,7 +42,7 @@ class ProjectLookupListener : LookupManagerListener {
|
||||
val itemCount = itemList.size
|
||||
val offset = getLookupOffset(newLookup)
|
||||
|
||||
for (index in hintTexts.indices) {
|
||||
for (index in 0 until CodeCompletionPopupConfiguration.itemShortcutCount) {
|
||||
val itemIndex = offset + index
|
||||
if (itemIndex >= itemCount) {
|
||||
break
|
||||
@ -80,7 +51,7 @@ class ProjectLookupListener : LookupManagerListener {
|
||||
if (item === itemList.getElementAt(itemIndex)) {
|
||||
val customized = LookupElementPresentation()
|
||||
customized.copyFrom(presentation)
|
||||
customized.appendTailTextItalic(hintTexts[index], true)
|
||||
customized.appendTailTextItalic(CodeCompletionPopupConfiguration.getHintText(index), true)
|
||||
return@addPresentationCustomizer customized
|
||||
}
|
||||
}
|
@ -13,13 +13,13 @@
|
||||
<depends>com.intellij.modules.platform</depends>
|
||||
|
||||
<projectListeners>
|
||||
<listener class="com.chylex.intellij.keyboardmaster.lookup.ProjectLookupListener" topic="com.intellij.codeInsight.lookup.LookupManagerListener" />
|
||||
<listener class="com.chylex.intellij.keyboardmaster.feature.codeCompletion.CodeCompletionPopupListener" topic="com.intellij.codeInsight.lookup.LookupManagerListener" />
|
||||
</projectListeners>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<applicationService serviceImplementation="com.chylex.intellij.keyboardmaster.configuration.PluginConfiguration" />
|
||||
<applicationConfigurable parentId="tools" instance="com.chylex.intellij.keyboardmaster.configuration.PluginConfigurable" id="com.chylex.keyboardmaster" />
|
||||
<!--suppress PluginXmlValidity, PluginXmlDynamicPlugin -->
|
||||
<editorTypedHandler implementationClass="com.chylex.intellij.keyboardmaster.lookup.LookupTypedActionHandler" />
|
||||
<editorTypedHandler implementationClass="com.chylex.intellij.keyboardmaster.feature.codeCompletion.CodeCompletionPopupKeyHandler" />
|
||||
</extensions>
|
||||
</idea-plugin>
|
||||
|
Loading…
Reference in New Issue
Block a user