From 3ecdb0bd5efc62e72c5f0612176eb5b78e560af9 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 4 Mar 2011 15:56:48 +0100 Subject: [PATCH] IDEA-66243 Support moving caret in lookups as in JB --- .../completion/JavaAutoPopupTest.groovy | 32 ++++++++++++ .../lookup/impl/BackspaceHandler.java | 9 +++- .../lookup/impl/LookupActionHandler.java | 49 +++++++++++++++++++ .../codeInsight/lookup/impl/TypedHandler.java | 8 ++- .../src/META-INF/LangExtensions.xml | 2 + 5 files changed, 93 insertions(+), 7 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy index 8fb52e985297..5701157ce85b 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy @@ -539,5 +539,37 @@ public interface Test { } + public void testLeftRightMovements() { + myFixture.configureByText("a.java", """ + class Foo { + void foo(String iterable) { + ter + } + } + """) + type('i') + def offset = myFixture.editor.caretModel.offset + assertSameElements myFixture.lookupElementStrings, "if", "iterable", "int" + + edt { myFixture.performEditorAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT) } + assert myFixture.editor.caretModel.offset == offset + 1 + assertOrderedEquals myFixture.lookupElementStrings, "iterable" + assertEquals 'iterable', lookup.currentItem.lookupString + + edt { myFixture.performEditorAction(IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT) } + assert myFixture.editor.caretModel.offset == offset + assertSameElements myFixture.lookupElementStrings, "if", "iterable", "int" + assertEquals 'iterable', lookup.currentItem.lookupString + + edt { myFixture.performEditorAction(IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT) } + joinAlarm() + joinCompletion() + assert lookup.items.size() > 3 + + for (i in 0.."iter".size()) { + edt { myFixture.performEditorAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT) } + } + assert !lookup + } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/BackspaceHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/BackspaceHandler.java index 9b77ae0ddd89..5087e8a4e055 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/BackspaceHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/BackspaceHandler.java @@ -37,10 +37,15 @@ public class BackspaceHandler extends EditorActionHandler { return; } + truncatePrefix(dataContext, lookup, myOriginalHandler, lookup.getLookupStart()); + } + + static void truncatePrefix(final DataContext dataContext, LookupImpl lookup, final EditorActionHandler handler, final int hideOffset) { + final Editor editor = lookup.getEditor(); lookup.performGuardedChange(new Runnable() { @Override public void run() { - myOriginalHandler.execute(editor, dataContext); + handler.execute(editor, dataContext); } }); @@ -49,7 +54,7 @@ public class BackspaceHandler extends EditorActionHandler { return; } - if (lookup.getLookupStart() < editor.getCaretModel().getOffset()) { + if (hideOffset < editor.getCaretModel().getOffset()) { if (process != null) { process.scheduleRestart(); return; diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupActionHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupActionHandler.java index 0db5737f3096..1530694e87f1 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupActionHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupActionHandler.java @@ -17,6 +17,8 @@ package com.intellij.codeInsight.lookup.impl; import com.intellij.codeInsight.completion.CompletionProgressIndicator; +import com.intellij.codeInsight.completion.impl.CompletionServiceImpl; +import com.intellij.codeInsight.lookup.CharFilter; import com.intellij.codeInsight.lookup.Lookup; import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.ide.ui.UISettings; @@ -185,4 +187,51 @@ public abstract class LookupActionHandler extends EditorActionHandler { } } + public static class LeftHandler extends LookupActionHandler { + public LeftHandler(EditorActionHandler originalHandler) { + super(originalHandler, false); + } + + @Override + protected void executeInLookup(LookupImpl lookup, DataContext context) { + BackspaceHandler.truncatePrefix(context, lookup, myOriginalHandler, lookup.getLookupStart() - 1); + } + } + public static class RightHandler extends LookupActionHandler { + public RightHandler(EditorActionHandler originalHandler) { + super(originalHandler, false); + } + + @Override + protected void executeInLookup(LookupImpl lookup, DataContext context) { + final Editor editor = lookup.getEditor(); + final int offset = editor.getCaretModel().getOffset(); + CharSequence seq = editor.getDocument().getCharsSequence(); + if (seq.length() <= offset) { + myOriginalHandler.execute(editor, context); + return; + } + + char c = seq.charAt(offset); + CharFilter.Result lookupAction = TypedHandler.getLookupAction(c, lookup); + if (lookupAction != CharFilter.Result.ADD_TO_PREFIX || Character.isWhitespace(c)) { + myOriginalHandler.execute(editor, context); + return; + } + + lookup.performGuardedChange(new Runnable() { + @Override + public void run() { + editor.getCaretModel().moveToOffset(offset + 1); + } + }); + + lookup.appendPrefix(c); + final CompletionProgressIndicator completion = CompletionServiceImpl.getCompletionService().getCurrentCompletion(); + if (completion != null) { + completion.prefixUpdated(); + } + } + } + } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/TypedHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/TypedHandler.java index 2039109fca7e..4dcab26920a2 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/TypedHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/TypedHandler.java @@ -26,10 +26,8 @@ import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.featureStatistics.FeatureUsageTracker; import com.intellij.openapi.actionSystem.DataContext; -import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.EditorBundle; import com.intellij.openapi.editor.EditorModificationUtil; import com.intellij.openapi.editor.actionSystem.TypedActionHandler; import com.intellij.openapi.extensions.Extensions; @@ -55,8 +53,7 @@ public class TypedHandler implements TypedActionHandler { return; } - final LookupElement currentItem = lookup.getCurrentItem(); - final CharFilter.Result result = getLookupAction(charTyped, currentItem, lookup); + final CharFilter.Result result = getLookupAction(charTyped, lookup); lookup.performGuardedChange(new Runnable() { public void run() { EditorModificationUtil.deleteSelectedText(editor); @@ -99,7 +96,8 @@ public class TypedHandler implements TypedActionHandler { } } - private static CharFilter.Result getLookupAction(final char charTyped, final LookupElement currentItem, final LookupImpl lookup) { + static CharFilter.Result getLookupAction(final char charTyped, final LookupImpl lookup) { + final LookupElement currentItem = lookup.getCurrentItem(); if (currentItem != null && charTyped != ' ') { String postfix = lookup.getAdditionalPrefix() + charTyped; final PrefixMatcher matcher = currentItem.getPrefixMatcher(); diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 84ff14dabe0a..b009446fd8ba 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -493,6 +493,8 @@ + +