diff --git a/build.gradle.kts b/build.gradle.kts index fef6437..3a55072 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "com.chylex.intellij.keyboardmaster" -version = "0.1" +version = "0.1.1" repositories { mavenCentral() diff --git a/src/main/kotlin/com/chylex/intellij/keyboardmaster/lookup/LookupTypedActionHandler.kt b/src/main/kotlin/com/chylex/intellij/keyboardmaster/lookup/LookupTypedActionHandler.kt index 688ec0d..202b159 100644 --- a/src/main/kotlin/com/chylex/intellij/keyboardmaster/lookup/LookupTypedActionHandler.kt +++ b/src/main/kotlin/com/chylex/intellij/keyboardmaster/lookup/LookupTypedActionHandler.kt @@ -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 } } diff --git a/src/main/kotlin/com/chylex/intellij/keyboardmaster/lookup/ProjectLookupListener.kt b/src/main/kotlin/com/chylex/intellij/keyboardmaster/lookup/ProjectLookupListener.kt index 50a2dd6..ddeb65a 100644 --- a/src/main/kotlin/com/chylex/intellij/keyboardmaster/lookup/ProjectLookupListener.kt +++ b/src/main/kotlin/com/chylex/intellij/keyboardmaster/lookup/ProjectLookupListener.kt @@ -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 } }