From 150f042860a6004439a1b5d1cedc9fdfdf286271 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Wed, 13 Apr 2016 17:41:18 +0300 Subject: [PATCH] Completion: avoid using lookup listeners in case their order matters --- .../completion/CompletionLookupArranger.java | 7 +++++- .../CompletionProgressIndicator.java | 25 ++++++++----------- .../codeInsight/lookup/LookupArranger.java | 6 ++++- .../codeInsight/lookup/impl/LookupImpl.java | 8 +++--- .../lookup/impl/LookupManagerImpl.java | 12 ++++----- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionLookupArranger.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionLookupArranger.java index 7812e8062c8f..bd827d896e67 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionLookupArranger.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionLookupArranger.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -176,6 +176,11 @@ public class CompletionLookupArranger extends LookupArranger { trimToLimit(lookup, context); } + @Override + public void itemSelected(@Nullable LookupElement lookupItem, char completionChar) { + myProcess.itemSelected(lookupItem, completionChar); + } + private void trimToLimit(Lookup lookup, ProcessingContext context) { if (myItems.size() < myLimit) return; diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java index 0a91d8c72c27..c3f9f64c6f99 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -107,19 +107,6 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement private final OffsetMap myOffsetMap; private final List>> myRestartingPrefixConditions = ContainerUtil.createLockFreeCopyOnWriteList(); private final LookupAdapter myLookupListener = new LookupAdapter() { - @Override - public void itemSelected(LookupEvent event) { - LookupElement item = event.getItem(); - boolean dispose = item == null; - finishCompletionProcess(dispose); - if (dispose) return; - - setMergeCommand(); - - myHandler.lookupItemSelected(CompletionProgressIndicator.this, item, event.getCompletionChar(), myLookup.getItems()); - } - - @Override public void lookupCanceled(final LookupEvent event) { finishCompletionProcess(true); @@ -177,6 +164,16 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement } } + public void itemSelected(@Nullable LookupElement lookupItem, char completionChar) { + boolean dispose = lookupItem == null; + finishCompletionProcess(dispose); + if (dispose) return; + + setMergeCommand(); + + myHandler.lookupItemSelected(this, lookupItem, completionChar, myLookup.getItems()); + } + public OffsetMap getOffsetMap() { return myOffsetMap; } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/LookupArranger.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/LookupArranger.java index aea154223bac..26099019e9a2 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/LookupArranger.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/LookupArranger.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Pair; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.*; @@ -66,6 +67,9 @@ public abstract class LookupArranger { return matcher.prefixMatches(item); } + public void itemSelected(@Nullable LookupElement lookupItem, char completionChar) { + } + public void prefixChanged(Lookup lookup) { myAdditionalPrefix = ((LookupImpl)lookup).getAdditionalPrefix(); rebuildItemCache(lookup); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupImpl.java index 2b22c258044a..10f38992c897 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -122,9 +122,9 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable, private volatile LookupArranger myArranger; private LookupArranger myPresentableArranger; private final Map myMatchers = - ContainerUtil.createConcurrentWeakMap(ContainerUtil.identityStrategy()); + ContainerUtil.createConcurrentWeakMap(ContainerUtil.identityStrategy()); private final Map myCustomFonts = ContainerUtil.createConcurrentWeakMap(10, 0.75f, Runtime.getRuntime().availableProcessors(), - ContainerUtil.identityStrategy()); + ContainerUtil.identityStrategy()); private boolean myStartCompletionWhenNothingMatches; boolean myResizePending; private boolean myFinishing; @@ -813,7 +813,7 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable, public void fireItemSelected(@Nullable final LookupElement item, char completionChar){ PsiDocumentManager.getInstance(myProject).commitAllDocuments(); - + myArranger.itemSelected(item, completionChar); if (!myListeners.isEmpty()){ LookupEvent event = new LookupEvent(this, item, completionChar); for (LookupListener listener : myListeners) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupManagerImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupManagerImpl.java index 02828d1b9b0e..11021728ee10 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupManagerImpl.java @@ -183,15 +183,15 @@ public class LookupManagerImpl extends LookupManager { private void lookupClosed() { ApplicationManager.getApplication().assertIsDispatchThread(); - alarm.cancelAllRequests(); - LookupImpl lookup = myActiveLookup; - if (lookup == null) return; - - LOG.assertTrue(lookup.isLookupDisposed()); + lookup.removeLookupListener(this); + } + }); + Disposer.register(lookup, new Disposable() { + @Override + public void dispose() { myActiveLookup = null; myActiveLookupEditor = null; - lookup.removeLookupListener(this); myPropertyChangeSupport.firePropertyChange(PROP_ACTIVE_LOOKUP, lookup, null); } });