From 348a1be0f52b0deea69e937c676477e0c18435f2 Mon Sep 17 00:00:00 2001 From: "peter.gromov" Date: Thu, 21 Oct 2010 19:21:44 +0400 Subject: [PATCH] lookup should listen for editor/document events even before it's shown --- .../codeInsight/lookup/impl/LookupImpl.java | 79 +++++++++---------- 1 file changed, 38 insertions(+), 41 deletions(-) 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 76e808e900ab..cfec8f4dd080 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 @@ -83,10 +83,6 @@ public class LookupImpl extends LightweightHint implements Lookup, Disposable { private final LookupCellRenderer myCellRenderer; private Boolean myPositionedAbove = null; - private CaretListener myEditorCaretListener; - private SelectionListener myEditorSelectionListener; - private EditorMouseListener myEditorMouseListener; - private final ArrayList myListeners = new ArrayList(); private boolean myShown = false; @@ -141,6 +137,8 @@ public class LookupImpl extends LightweightHint implements Lookup, Disposable { updateListHeight(model); setArranger(arranger); + + addListeners(); } public void setArranger(LookupArranger arranger) { @@ -571,6 +569,18 @@ public class LookupImpl extends LightweightHint implements Lookup, Disposable { LOG.assertTrue(!myShown); myShown = true; + if (ApplicationManager.getApplication().isUnitTestMode()) return; + + getComponent().setBorder(null); + + Point p = calculatePosition(); + HintManagerImpl hintManager = HintManagerImpl.getInstanceImpl(); + hintManager.showEditorHint(this, myEditor, p, HintManagerImpl.HIDE_BY_ESCAPE | HintManagerImpl.UPDATE_BY_SCROLLING, 0, false); + + myShownStamp = System.currentTimeMillis(); + } + + private void addListeners() { myEditor.getDocument().addDocumentListener(new DocumentAdapter() { public void documentChanged(DocumentEvent e) { if (!myChangeGuard) { @@ -579,29 +589,41 @@ public class LookupImpl extends LightweightHint implements Lookup, Disposable { } }, this); - myEditorCaretListener = new CaretListener() { + final CaretListener caretListener = new CaretListener() { public void caretPositionChanged(CaretEvent e){ - caretOrSelectionChanged(); + if (!myChangeGuard) { + hide(); + } } }; - myEditorSelectionListener = new SelectionListener() { + final SelectionListener selectionListener = new SelectionListener() { public void selectionChanged(final SelectionEvent e) { - caretOrSelectionChanged(); + if (!myChangeGuard) { + hide(); + } } }; - myEditor.getCaretModel().addCaretListener(myEditorCaretListener); - myEditor.getSelectionModel().addSelectionListener(myEditorSelectionListener); - - myEditorMouseListener = new EditorMouseAdapter() { + final EditorMouseListener mouseListener = new EditorMouseAdapter() { public void mouseClicked(EditorMouseEvent e){ e.consume(); hide(); } }; - myEditor.addEditorMouseListener(myEditorMouseListener); + + myEditor.getCaretModel().addCaretListener(caretListener); + myEditor.getSelectionModel().addSelectionListener(selectionListener); + myEditor.addEditorMouseListener(mouseListener); + Disposer.register(this, new Disposable() { + @Override + public void dispose() { + myEditor.getCaretModel().removeCaretListener(caretListener); + myEditor.getSelectionModel().removeSelectionListener(selectionListener); + myEditor.removeEditorMouseListener(mouseListener); + } + }); myList.addListSelectionListener(new ListSelectionListener() { - private LookupElement oldItem = null; + private LookupElement oldItem = null; public void valueChanged(ListSelectionEvent e){ LookupElement item = getCurrentItem(); @@ -621,7 +643,7 @@ public class LookupImpl extends LightweightHint implements Lookup, Disposable { final int i = myList.locationToIndex(point); if (i >= 0) { final LookupElement selected = (LookupElement)myList.getModel().getElementAt(i); - if (selected != null && + if (selected != null && e.getClickCount() == 1 && point.x >= myList.getCellBounds(i, i).width - PopupIcons.EMPTY_ICON.getIconWidth() && ShowLookupActionsHandler.showItemActions(LookupImpl.this, selected)) { @@ -638,22 +660,6 @@ public class LookupImpl extends LightweightHint implements Lookup, Disposable { } } }); - - if (ApplicationManager.getApplication().isUnitTestMode()) return; - - getComponent().setBorder(null); - - Point p = calculatePosition(); - HintManagerImpl hintManager = HintManagerImpl.getInstanceImpl(); - hintManager.showEditorHint(this, myEditor, p, HintManagerImpl.HIDE_BY_ESCAPE | HintManagerImpl.UPDATE_BY_SCROLLING, 0, false); - - myShownStamp = System.currentTimeMillis(); - } - - private void caretOrSelectionChanged() { - if (!myChangeGuard) { - hide(); - } } private int calcLookupStart() { @@ -922,16 +928,7 @@ public class LookupImpl extends LightweightHint implements Lookup, Disposable { assert !myDisposed; Disposer.dispose(myProcessIcon); - if (myEditorCaretListener != null) { - myEditor.getCaretModel().removeCaretListener(myEditorCaretListener); - myEditor.getSelectionModel().removeSelectionListener(myEditorSelectionListener); - myEditorCaretListener = null; - myEditorSelectionListener = null; - } - if (myEditorMouseListener != null) { - myEditor.removeEditorMouseListener(myEditorMouseListener); - myEditorMouseListener = null; - } + myDisposed = true; }