1
0
mirror of https://github.com/chylex/IntelliJ-Keyboard-Master.git synced 2025-05-07 17:34:07 +02:00

Allow selecting code completion items by typing digits

This commit is contained in:
chylex 2021-08-21 16:02:38 +02:00
parent d6053046a5
commit c915ba2fcd
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
3 changed files with 86 additions and 0 deletions
src/main
kotlin/com/chylex/intellij/keyboardmaster/lookup
resources/META-INF

View File

@ -0,0 +1,33 @@
package com.chylex.intellij.keyboardmaster.lookup
import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.codeInsight.lookup.impl.LookupImpl
import com.intellij.codeInsight.template.impl.editorActions.TypedActionHandlerBase
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.TypedActionHandler
/**
* When typing digits inside a code completion popup menu, selects the n-th item (or 10th item if the digit is 0) in the list.
*/
class LookupTypedActionHandler(originalHandler: TypedActionHandler?) : TypedActionHandlerBase(originalHandler) {
override fun execute(editor: Editor, charTyped: Char, dataContext: DataContext) {
if (!executeImpl(editor, charTyped)) {
myOriginalHandler?.execute(editor, charTyped, dataContext)
}
}
private fun executeImpl(editor: Editor, charTyped: Char): Boolean {
if (charTyped !in '0'..'9') {
return false
}
val lookup = LookupManager.getActiveLookup(editor)
if (lookup !is LookupImpl) {
return false
}
lookup.selectedIndex = if (charTyped == '0') 9 else charTyped - '1'
return true
}
}

View File

@ -0,0 +1,41 @@
package com.chylex.intellij.keyboardmaster.lookup
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
/**
* Adds hints to code completion items with the digit that selects it.
*/
class ProjectLookupListener : LookupManagerListener {
companion object {
private val IS_MODIFIED_KEY = Key.create<Boolean>("chylexKeyboardMasterModified")
private val HINT_TEXT = Array(10) { " [${(it + 1) % 10}]" }
}
override fun activeLookupChanged(oldLookup: Lookup?, newLookup: Lookup?) {
if (newLookup !is LookupImpl || newLookup.getUserData(IS_MODIFIED_KEY) == true) {
return
}
newLookup.putUserData(IS_MODIFIED_KEY, true)
@Suppress("UnstableApiUsage")
newLookup.addPresentationCustomizer { item, presentation ->
val items = newLookup.list.model
for (index in 0 until items.size.coerceAtMost(10)) {
if (item === items.getElementAt(index)) {
val customized = LookupElementPresentation()
customized.copyFrom(presentation)
customized.appendTailTextItalic(HINT_TEXT[index], true)
return@addPresentationCustomizer customized
}
}
presentation
}
}
}

View File

@ -5,7 +5,19 @@
<description><![CDATA[
Collection of keyboard-centric additions.
<ul>
<li>Code completion items can be jumped to using numbers.</li>
</ul>
]]></description>
<depends>com.intellij.modules.platform</depends>
<projectListeners>
<listener class="com.chylex.intellij.keyboardmaster.lookup.ProjectLookupListener" topic="com.intellij.codeInsight.lookup.LookupManagerListener" />
</projectListeners>
<extensions defaultExtensionNs="com.intellij">
<!--suppress PluginXmlValidity, PluginXmlDynamicPlugin -->
<editorTypedHandler implementationClass="com.chylex.intellij.keyboardmaster.lookup.LookupTypedActionHandler" />
</extensions>
</idea-plugin>