1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-08-11 09:40:37 +02:00

Fix problem with lookup selection

This commit is contained in:
Alex Plate 2019-11-01 13:55:32 +03:00
parent cdcb31cf2f
commit 9d6f43cfeb
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
2 changed files with 36 additions and 0 deletions
src/com/maddyhome/idea/vim/option
test/org/jetbrains/plugins/ideavim/group/visual

View File

@ -18,6 +18,8 @@
package com.maddyhome.idea.vim.option
import com.intellij.codeInsight.lookup.LookupEvent
import com.intellij.codeInsight.lookup.LookupListener
import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.codeInsight.lookup.impl.LookupImpl
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
@ -477,7 +479,19 @@ object IdeaRefactorMode {
val lookup = LookupManager.getActiveLookup(editor) as? LookupImpl
if (lookup != null) {
val selStart = editor.selectionModel.selectionStart
val selEnd = editor.selectionModel.selectionEnd
lookup.performGuardedChange(action)
lookup.addLookupListener(object : LookupListener {
override fun beforeItemSelected(event: LookupEvent): Boolean {
// FIXME: 01.11.2019 Nasty workaround because of problems in IJ platform
// Lookup replaces selected text and not the template itself. So, if there is no selection
// in the template, lookup value will not replace the template, but just insert value on the caret position
lookup.performGuardedChange { editor.selectionModel.setSelection(selStart, selEnd) }
lookup.removeLookupListener(this)
return true
}
})
} else {
action()
}

View File

@ -20,6 +20,7 @@
package org.jetbrains.plugins.ideavim.group.visual
import com.intellij.codeInsight.lookup.Lookup
import com.intellij.codeInsight.template.TemplateManager
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.ide.DataManager
@ -438,6 +439,27 @@ class TemplateTest : VimOptionTestCase(IdeaRefactorMode.name) {
assertNull(TemplateManagerImpl.getTemplateState(myFixture.editor))
}
@VimOptionTestConfiguration(VimTestOption(IdeaRefactorMode.name, VimTestOptionType.VALUE, [IdeaRefactorMode.keep]))
fun `test template with lookup`() {
configureByJavaText("""
class Hello {
public static void main() {
int my${c}Var = 5;
}
}
""".trimIndent())
startRenaming(VariableInplaceRenameHandler())
val lookupValue = myFixture.lookupElementStrings?.get(0) ?: kotlin.test.fail()
myFixture.finishLookup(Lookup.NORMAL_SELECT_CHAR)
myFixture.checkResult("""
class Hello {
public static void main() {
int $lookupValue = 5;
}
}
""".trimIndent())
}
private fun startRenaming(handler: VariableInplaceRenameHandler): Editor {
val editor = if (myFixture.editor is EditorWindow) (myFixture.editor as EditorWindow).delegate else myFixture.editor
VimListenerManager.EditorListeners.add(editor)