when there are matching variants in autopopup, hide the lookup (IDEA-60835)

This commit is contained in:
peter.gromov
2010-11-19 18:32:23 +03:00
parent 0c37910583
commit 71812e4702
5 changed files with 61 additions and 26 deletions
@@ -38,11 +38,12 @@ class JavaAutoPopupTest extends CompletionAutoPopupTestCase {
assertEquals 'iterable', lookup.currentItem.lookupString
type('er')
assertOrderedEquals myFixture.lookupElementStrings, "iter", "iterable"
assertEquals 'iter', lookup.currentItem.lookupString
assert !lookup
//assertOrderedEquals myFixture.lookupElementStrings, "iter", "iterable"
//assertEquals 'iter', lookup.currentItem.lookupString
}
public void testRecalculateItemsOnBackspace() {
public void _testRecalculateItemsOnBackspace() {
myFixture.configureByText("a.java", """
class Foo {
void foo(String iterable) {
@@ -195,4 +196,25 @@ class JavaAutoPopupTest extends CompletionAutoPopupTestCase {
"""
}
public void testHideAutopopupIfItContainsExactMatch() {
myFixture.configureByText("a.java", """
class Foo {
String foo() {
int abcd;
int abcde;
int abcdefg;
ab<caret>
}
}
""")
type 'c'
assert lookup
type 'd'
assert !lookup
type 'e'
assert !lookup
type 'f'
assert lookup
}
}
@@ -57,9 +57,11 @@ abstract class CompletionAutoPopupTestCase extends LightCodeInsightFixtureTestCa
void type(String s) {
for (i in 0..<s.size()) {
myFixture.type(s.charAt(i))
ApplicationManager.application.invokeAndWait({ println "wait1" } as Runnable, ModalityState.NON_MODAL) // for the autopopup's alarm
ApplicationManager.application.invokeAndWait({ println "wait2" } as Runnable, ModalityState.NON_MODAL) // for the restartCompletion's invokeLater
final c = s.charAt(i)
println "typing ${c}"
myFixture.type(c)
ApplicationManager.application.invokeAndWait({ println "wait1" } as Runnable, ModalityState.NON_MODAL) // for the autopopup handler's alarm, or the restartCompletion's invokeLater
ApplicationManager.application.invokeAndWait({ println "wait2" } as Runnable, ModalityState.NON_MODAL) // for invokeLater in CompletionProgressIndicator.stop()
}
}
@@ -164,6 +164,8 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
EditorUtil.fillVirtualSpaceUntilCaret(editor);
documentManager.commitAllDocuments();
assert editor.getDocument().getTextLength() == psiFile.getTextLength() : "unsuccessful commit";
final Ref<CompletionContributor> current = Ref.create(null);
initializationContext[0] = new CompletionInitializationContext(editor, psiFile, myCompletionType) {
CompletionContributor dummyIdentifierChanger;
@@ -272,7 +274,7 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
final AtomicReference<LookupElement[]> data = startCompletionThread(parameters, indicator, initContext);
if (!invokedExplicitly && !ApplicationManager.getApplication().isUnitTestMode()) {
if (!invokedExplicitly && (!ApplicationManager.getApplication().isUnitTestMode() || CompletionAutoPopupHandler.ourTestingAutopopup)) {
indicator.notifyBackgrounded();
return;
}
@@ -475,6 +477,7 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
final AutoCompletionDecision decision = shouldAutoComplete(indicator, items);
if (decision == AutoCompletionDecision.SHOW_LOOKUP) {
indicator.getLookup().setCalculating(false);
indicator.showLookup();
if (isAutocompleteCommonPrefixOnInvocation() && items.length > 1) {
indicator.fillInCommonPrefix(false);
@@ -20,7 +20,6 @@ import com.intellij.codeInsight.completion.impl.CompletionServiceImpl;
import com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler;
import com.intellij.codeInsight.lookup.*;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.CommandProcessor;
@@ -96,7 +95,7 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
private int myOldCaret;
private int myOldStart;
private int myOldEnd;
private boolean myBackgrounded = true;
private boolean myBackgrounded;
private OffsetMap myOffsetMap;
private final CopyOnWriteArrayList<Pair<Integer, ElementPattern<String>>> myRestartingPrefixConditions = ContainerUtil.createEmptyCOWList();
private final LookupAdapter myLookupListener = new LookupAdapter() {
@@ -373,6 +372,9 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
if (isOutdated()) return;
if (!myShownLookup) {
if (hideAutopopupIfMeaningless()) {
return;
}
myShownLookup = true;
if (StringUtil.isEmpty(myLookup.getAdvertisementText()) && !isAutopopupCompletion()) {
@@ -388,6 +390,7 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
if (!ApplicationManager.getApplication().isUnitTestMode()) {
LOG.assertTrue(myLookup.isVisible());
}
hideAutopopupIfMeaningless();
}
final boolean isInsideIdentifier() {
@@ -485,8 +488,11 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
myQueue.cancelAllUpdates();
myFreezeSemaphore.up();
invokeLaterIfNotDispatch(new Runnable() {
ApplicationManager.getApplication().invokeLater(new Runnable() {
public void run() {
if (CompletionAutoPopupHandler.ourTestingAutopopup) {
System.out.println("CompletionProgressIndicator.stop.later");
}
if (isOutdated()) return;
if (!isBackgrounded()) return;
if (isCanceled() && !myRestartScheduled) return;
@@ -503,43 +509,41 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
final CompletionProgressIndicator current = CompletionServiceImpl.getCompletionService().getCurrentCompletion();
assert current == null : current + "!=" + CompletionProgressIndicator.this;
if (!isAutopopupCompletion() ) {
if (!isAutopopupCompletion()) {
myHandler.handleEmptyLookup(getProject(), myEditor, myParameters, CompletionProgressIndicator.this);
}
} else {
}
else {
if (myFocusLookupWhenDone) {
myLookup.setFocused(true);
}
updateLookup();
}
}
});
}, myQueue.getModalityState());
}
public boolean hideAutopopupIfMeaningless() {
if (isAutopopupCompletion() && !myLookup.isCalculating()) {
if (isAutopopupCompletion() && !myLookup.isSelectionTouched()) {
myLookup.refreshUi();
final List<LookupElement> items = myLookup.getItems();
if (items.size() == 0 || items.size() == 1 && (items.get(0).getPrefixMatcher().getPrefix() + myLookup.getAdditionalPrefix()).equals(items.get(0).getLookupString())) {
if (items.isEmpty() && !myLookup.isCalculating()) {
myLookup.hideLookup(false);
assert CompletionServiceImpl.getCompletionService().getCurrentCompletion() == null;
return true;
}
for (LookupElement item : items) {
if ((item.getPrefixMatcher().getPrefix() + myLookup.getAdditionalPrefix()).equals(item.getLookupString())) {
myLookup.hideLookup(true); // so that the autopopup attempts to restart after the next typed character
assert CompletionServiceImpl.getCompletionService().getCurrentCompletion() == null;
return true;
}
}
}
return false;
}
private void invokeLaterIfNotDispatch(final Runnable runnable) {
final Application application = ApplicationManager.getApplication();
if (application.isUnitTestMode()) {
runnable.run();
} else {
application.invokeLater(runnable, myQueue.getModalityState());
}
}
public void cancelByWriteAction() {
if (myToRestart != null) {
LOG.assertTrue(myToRestart == Boolean.FALSE); //explicit completionFinished was invoked before this write action
@@ -897,6 +897,10 @@ public class LookupImpl extends LightweightHint implements Lookup, Disposable {
return myPositionedAbove != null && myPositionedAbove.booleanValue();
}
public boolean isSelectionTouched() {
return mySelectionTouched;
}
public void hide(){
hideLookup(true);
}