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

Typing digit '0' in code completion list moves down by 9 items

This commit is contained in:
chylex 2021-08-23 14:25:35 +02:00
parent c915ba2fcd
commit 619aa49b42
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
3 changed files with 49 additions and 8 deletions
build.gradle.kts
src/main/kotlin/com/chylex/intellij/keyboardmaster/lookup

View File

@ -5,7 +5,7 @@ plugins {
}
group = "com.chylex.intellij.keyboardmaster"
version = "0.1"
version = "0.1.1"
repositories {
mavenCentral()

View File

@ -6,9 +6,11 @@ import com.intellij.codeInsight.template.impl.editorActions.TypedActionHandlerBa
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.TypedActionHandler
import com.intellij.ui.ScrollingUtil
/**
* 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.
* 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.
*/
class LookupTypedActionHandler(originalHandler: TypedActionHandler?) : TypedActionHandlerBase(originalHandler) {
override fun execute(editor: Editor, charTyped: Char, dataContext: DataContext) {
@ -27,7 +29,23 @@ class LookupTypedActionHandler(originalHandler: TypedActionHandler?) : TypedActi
return false
}
lookup.selectedIndex = if (charTyped == '0') 9 else charTyped - '1'
val offset = ProjectLookupListener.getLookupOffset(lookup)
if (charTyped == '0') {
val list = lookup.list
val itemCount = list.model.size
val topIndex = (offset + 9).let { if (it >= itemCount) 0 else it }
ProjectLookupListener.setLookupOffset(lookup, topIndex)
lookup.selectedIndex = topIndex
ScrollingUtil.ensureRangeIsVisible(list, topIndex, topIndex + 8)
lookup.markSelectionTouched()
lookup.refreshUi(false, true)
}
else {
lookup.selectedIndex = offset + (charTyped - '1')
}
return true
}
}

View File

@ -11,8 +11,24 @@ import com.intellij.openapi.util.Key
*/
class ProjectLookupListener : LookupManagerListener {
companion object {
private val OFFSET_KEY = Key.create<Int>("chylexKeyboardMasterOffset")
private val IS_MODIFIED_KEY = Key.create<Boolean>("chylexKeyboardMasterModified")
private val HINT_TEXT = Array(10) { " [${(it + 1) % 10}]" }
private val HINT_TEXT = Array(9) { " [${it + 1}]" }
fun getLookupOffset(lookup: LookupImpl): Int {
val offset = lookup.getUserData(OFFSET_KEY)
if (offset == null || offset >= lookup.list.model.size) {
return 0
}
else {
return offset
}
}
fun setLookupOffset(lookup: LookupImpl, newOffset: Int) {
lookup.putUserData(OFFSET_KEY, newOffset)
}
}
override fun activeLookupChanged(oldLookup: Lookup?, newLookup: Lookup?) {
@ -24,13 +40,20 @@ class ProjectLookupListener : LookupManagerListener {
@Suppress("UnstableApiUsage")
newLookup.addPresentationCustomizer { item, presentation ->
val items = newLookup.list.model
val itemList = newLookup.list.model
val itemCount = itemList.size
val offset = getLookupOffset(newLookup)
for (index in 0 until items.size.coerceAtMost(10)) {
if (item === items.getElementAt(index)) {
for (digitIndex in 0 until 9) {
val itemIndex = offset + digitIndex
if (itemIndex >= itemCount) {
break
}
if (item === itemList.getElementAt(itemIndex)) {
val customized = LookupElementPresentation()
customized.copyFrom(presentation)
customized.appendTailTextItalic(HINT_TEXT[index], true)
customized.appendTailTextItalic(HINT_TEXT[digitIndex], true)
return@addPresentationCustomizer customized
}
}