Skip to content

Commit 427ab9c

Browse files
committed
don't close lookup on appending prefix during completion
1 parent 56ca617 commit 427ab9c

File tree

4 files changed

+66
-35
lines changed

4 files changed

+66
-35
lines changed

java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,19 @@ class JavaAutoPopupTest extends CompletionAutoPopupTestCase {
316316
assert 'ArrayIndexOutOfBoundsException' in myFixture.lookupElementStrings
317317
}
318318

319+
public void testTypingDuringExplicitCompletion() {
320+
myFixture.configureByText("a.java", """
321+
class A {
322+
{ Runnable r = new <caret> }
323+
}
324+
""")
325+
myFixture.complete CompletionType.SMART
326+
edt { myFixture.type 'Thr' }
327+
joinCompletion()
328+
assert lookup
329+
assert 'Thread' in myFixture.lookupElementStrings
330+
}
331+
332+
333+
319334
}

platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private void doComplete(final int invocationCount, CompletionInitializationConte
285285

286286
final AtomicReference<LookupElement[]> data = startCompletionThread(parameters, indicator, initContext);
287287

288-
if (!invokedExplicitly && (!ApplicationManager.getApplication().isUnitTestMode() || CompletionAutoPopupHandler.ourTestingAutopopup)) {
288+
if ((!invokedExplicitly && !ApplicationManager.getApplication().isUnitTestMode()) || CompletionAutoPopupHandler.ourTestingAutopopup) {
289289
indicator.notifyBackgrounded();
290290
return;
291291
}

platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -401,16 +401,14 @@ public void closeAndFinish(boolean hideLookup) {
401401
LOG.assertTrue(this == CompletionServiceImpl.getCompletionService().getCurrentCompletion());
402402

403403
Lookup lookup = LookupManager.getActiveLookup(myEditor);
404-
if (lookup != null) {
405-
LOG.assertTrue(lookup == myLookup);
406-
myLookup.removeLookupListener(myLookupListener);
407-
finishCompletionProcess();
404+
LOG.assertTrue(lookup == myLookup);
405+
myLookup.removeLookupListener(myLookupListener);
406+
finishCompletionProcess();
407+
myState.assertDisposed();
408+
CompletionServiceImpl.assertPhase(CompletionPhase.NoCompletion.getClass());
408409

409-
if (hideLookup) {
410-
LookupManager.getInstance(getProject()).hideActiveLookup();
411-
}
412-
} else {
413-
myState.assertDisposed();
410+
if (hideLookup) {
411+
LookupManager.getInstance(getProject()).hideActiveLookup();
414412
}
415413
}
416414

@@ -451,10 +449,19 @@ public void stop() {
451449
public void run() {
452450
if (isOutdated()) return;
453451
if (!isBackgrounded()) return;
454-
if (isCanceled() && !myState.isRestartScheduled()) return;
452+
453+
if (isCanceled() && myState.isRestartScheduled()) {
454+
CompletionServiceImpl.assertPhase(CompletionPhase.Restarted.class);
455+
return;
456+
}
455457

456458
myLookup.setCalculating(false);
457459

460+
if (isCanceled()) {
461+
CompletionServiceImpl.assertPhase(CompletionPhase.NoCompletion.getClass());
462+
return;
463+
}
464+
458465
if (CompletionServiceImpl.isPhase(CompletionPhase.BgCalculation.class) && hideAutopopupIfMeaningless()) {
459466
return;
460467
}
@@ -466,7 +473,8 @@ public void run() {
466473
final CompletionProgressIndicator current = CompletionServiceImpl.getCompletionService().getCurrentCompletion();
467474
LOG.assertTrue(current == null, current + "!=" + CompletionProgressIndicator.this);
468475

469-
CompletionServiceImpl.setCompletionPhase(myHandler.handleEmptyLookup(getProject(), myEditor, myParameters, CompletionProgressIndicator.this));
476+
CompletionServiceImpl
477+
.setCompletionPhase(myHandler.handleEmptyLookup(getProject(), myEditor, myParameters, CompletionProgressIndicator.this));
470478
}
471479
}
472480
else {

platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.intellij.openapi.application.ApplicationManager;
5555
import com.intellij.openapi.application.ModalityState;
5656
import com.intellij.openapi.application.Result;
57+
import com.intellij.openapi.command.CommandProcessor;
5758
import com.intellij.openapi.command.WriteCommandAction;
5859
import com.intellij.openapi.editor.*;
5960
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
@@ -839,35 +840,42 @@ public LookupElement[] complete(CompletionType type) {
839840
public LookupElement[] complete(final CompletionType type, final int invocationCount) {
840841
assertInitialized();
841842
myEmptyLookup = false;
842-
new WriteCommandAction(getProject()) {
843+
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
843844
@Override
844-
protected void run(Result result) throws Exception {
845-
final CodeCompletionHandlerBase handler = new CodeCompletionHandlerBase(type) {
845+
public void run() {
846+
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
846847
@Override
847-
protected PsiFile createFileCopy(final PsiFile file) {
848-
final PsiFile copy = super.createFileCopy(file);
849-
if (myFileContext != null) {
850-
final PsiElement contextCopy = myFileContext.copy();
851-
final PsiFile containingFile = contextCopy.getContainingFile();
852-
if (containingFile instanceof PsiFileImpl) {
853-
((PsiFileImpl)containingFile).setOriginalFile(myFileContext.getContainingFile());
848+
public void run() {
849+
final CodeCompletionHandlerBase handler = new CodeCompletionHandlerBase(type) {
850+
@Override
851+
protected PsiFile createFileCopy(final PsiFile file) {
852+
final PsiFile copy = super.createFileCopy(file);
853+
if (myFileContext != null) {
854+
final PsiElement contextCopy = myFileContext.copy();
855+
final PsiFile containingFile = contextCopy.getContainingFile();
856+
if (containingFile instanceof PsiFileImpl) {
857+
((PsiFileImpl)containingFile).setOriginalFile(myFileContext.getContainingFile());
858+
}
859+
setContext(copy, contextCopy);
860+
}
861+
return copy;
854862
}
855-
setContext(copy, contextCopy);
856-
}
857-
return copy;
858-
}
859863

860-
@Override
861-
protected void completionFinished(final int offset1, final int offset2, final CompletionProgressIndicator indicator,
862-
final LookupElement[] items) {
863-
myEmptyLookup = items.length == 0;
864-
super.completionFinished(offset1, offset2, indicator, items);
864+
@Override
865+
protected void completionFinished(final int offset1, final int offset2, final CompletionProgressIndicator indicator,
866+
final LookupElement[] items) {
867+
myEmptyLookup = items.length == 0;
868+
super.completionFinished(offset1, offset2, indicator, items);
869+
}
870+
};
871+
Editor editor = getCompletionEditor();
872+
handler.invokeCompletion(getProject(), editor, PsiUtilBase.getPsiFileInEditor(editor, getProject()), invocationCount);
873+
865874
}
866-
};
867-
Editor editor = getCompletionEditor();
868-
handler.invokeCompletion(getProject(), editor, PsiUtilBase.getPsiFileInEditor(editor, getProject()), invocationCount);
875+
}, null, null);
869876
}
870-
}.execute();
877+
});
878+
871879
return getLookupElements();
872880
}
873881

0 commit comments

Comments
 (0)