IDEA-66243 Support moving caret in lookups as in JB

This commit is contained in:
peter
2011-03-04 15:57:47 +01:00
parent e3e989cea4
commit 3ecdb0bd5e
5 changed files with 93 additions and 7 deletions
@@ -539,5 +539,37 @@ public interface Test {
}
public void testLeftRightMovements() {
myFixture.configureByText("a.java", """
class Foo {
void foo(String iterable) {
<caret>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
}
}
@@ -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;
@@ -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();
}
}
}
}
@@ -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();
@@ -493,6 +493,8 @@
<editorActionHandler action="EditorDown" implementationClass="com.intellij.codeInsight.lookup.impl.LookupActionHandler$DownHandler"/>
<editorActionHandler action="EditorPageUp" implementationClass="com.intellij.codeInsight.lookup.impl.LookupActionHandler$PageUpHandler"/>
<editorActionHandler action="EditorPageDown" implementationClass="com.intellij.codeInsight.lookup.impl.LookupActionHandler$PageDownHandler"/>
<editorActionHandler action="EditorLeft" implementationClass="com.intellij.codeInsight.lookup.impl.LookupActionHandler$LeftHandler"/>
<editorActionHandler action="EditorRight" implementationClass="com.intellij.codeInsight.lookup.impl.LookupActionHandler$RightHandler"/>
<editorActionHandler action="EditorLineStart" implementationClass="com.intellij.codeInsight.lookup.impl.HomeHandler"/>
<editorActionHandler action="EditorLineEnd" implementationClass="com.intellij.codeInsight.lookup.impl.EndHandler"/>
<editorActionHandler action="EditorBackSpace" implementationClass="com.intellij.codeInsight.lookup.impl.BackspaceHandler"/>