summaryrefslogtreecommitdiffstats
path: root/Lib/idlelib/AutoCompleteWindow.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/idlelib/AutoCompleteWindow.py')
-rw-r--r--Lib/idlelib/AutoCompleteWindow.py63
1 files changed, 40 insertions, 23 deletions
diff --git a/Lib/idlelib/AutoCompleteWindow.py b/Lib/idlelib/AutoCompleteWindow.py
index 7f8adaf..56433cd 100644
--- a/Lib/idlelib/AutoCompleteWindow.py
+++ b/Lib/idlelib/AutoCompleteWindow.py
@@ -10,13 +10,14 @@ HIDE_SEQUENCES = ("<FocusOut>", "<ButtonPress>")
KEYPRESS_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keypress>>"
# We need to bind event beyond <Key> so that the function will be called
# before the default specific IDLE function
-KEYPRESS_SEQUENCES = ("<Key>", "<Key-BackSpace>", "<Key-Return>",
- "<Key-Up>", "<Key-Down>", "<Key-Home>", "<Key-End>")
+KEYPRESS_SEQUENCES = ("<Key>", "<Key-BackSpace>", "<Key-Return>", "<Key-Tab>",
+ "<Key-Up>", "<Key-Down>", "<Key-Home>", "<Key-End>",
+ "<Key-Prior>", "<Key-Next>")
KEYRELEASE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keyrelease>>"
KEYRELEASE_SEQUENCE = "<KeyRelease>"
-LISTUPDATE_SEQUENCE = "<ButtonRelease>"
+LISTUPDATE_SEQUENCE = "<B1-ButtonRelease>"
WINCONFIG_SEQUENCE = "<Configure>"
-DOUBLECLICK_SEQUENCE = "<Double-ButtonRelease>"
+DOUBLECLICK_SEQUENCE = "<B1-Double-ButtonRelease>"
class AutoCompleteWindow:
@@ -49,6 +50,8 @@ class AutoCompleteWindow:
# event ids
self.hideid = self.keypressid = self.listupdateid = self.winconfigid \
= self.keyreleaseid = self.doubleclickid = None
+ # Flag set if last keypress was a tab
+ self.lastkey_was_tab = False
def _change_start(self, newstart):
i = 0
@@ -118,11 +121,6 @@ class AutoCompleteWindow:
i = 0
while i < len(lts) and i < len(selstart) and lts[i] == selstart[i]:
i += 1
- previous_completion = self.completions[cursel - 1]
- while cursel > 0 and selstart[:i] <= previous_completion:
- i += 1
- if selstart == previous_completion:
- break # maybe we have a duplicate?
newstart = selstart[:i]
self._change_start(newstart)
@@ -206,7 +204,7 @@ class AutoCompleteWindow:
self.keyrelease_event)
self.widget.event_add(KEYRELEASE_VIRTUAL_EVENT_NAME,KEYRELEASE_SEQUENCE)
self.listupdateid = listbox.bind(LISTUPDATE_SEQUENCE,
- self.listupdate_event)
+ self.listselect_event)
self.winconfigid = acw.bind(WINCONFIG_SEQUENCE, self.winconfig_event)
self.doubleclickid = listbox.bind(DOUBLECLICK_SEQUENCE,
self.doubleclick_event)
@@ -215,24 +213,34 @@ class AutoCompleteWindow:
if not self.is_active():
return
# Position the completion list window
+ text = self.widget
+ text.see(self.startindex)
+ x, y, cx, cy = text.bbox(self.startindex)
acw = self.autocompletewindow
- self.widget.see(self.startindex)
- x, y, cx, cy = self.widget.bbox(self.startindex)
- acw.wm_geometry("+%d+%d" % (x + self.widget.winfo_rootx(),
- y + self.widget.winfo_rooty() \
- -acw.winfo_height()))
-
+ acw_width, acw_height = acw.winfo_width(), acw.winfo_height()
+ text_width, text_height = text.winfo_width(), text.winfo_height()
+ new_x = text.winfo_rootx() + min(x, max(0, text_width - acw_width))
+ new_y = text.winfo_rooty() + y
+ if (text_height - (y + cy) >= acw_height # enough height below
+ or y < acw_height): # not enough height above
+ # place acw below current line
+ new_y += cy
+ else:
+ # place acw above current line
+ new_y -= acw_height
+ acw.wm_geometry("+%d+%d" % (new_x, new_y))
def hide_event(self, event):
if not self.is_active():
return
self.hide_window()
- def listupdate_event(self, event):
+ def listselect_event(self, event):
if not self.is_active():
return
self.userwantswindow = True
- self._selection_changed()
+ cursel = int(self.listbox.curselection()[0])
+ self._change_start(self.completions[cursel])
def doubleclick_event(self, event):
# Put the selected completion in the text, and close the list
@@ -248,7 +256,8 @@ class AutoCompleteWindow:
state = event.mc_state
else:
state = 0
-
+ if keysym != "Tab":
+ self.lastkey_was_tab = False
if (len(keysym) == 1 or keysym in ("underscore", "BackSpace")
or (self.mode==AutoComplete.COMPLETE_FILES and keysym in
("period", "minus"))) \
@@ -330,13 +339,21 @@ class AutoCompleteWindow:
self.listbox.select_clear(cursel)
self.listbox.select_set(newsel)
self._selection_changed()
+ self._change_start(self.completions[newsel])
return "break"
elif (keysym == "Tab" and not state):
- # The user wants a completion, but it is handled by AutoComplete
- # (not AutoCompleteWindow), so ignore.
- self.userwantswindow = True
- return
+ if self.lastkey_was_tab:
+ # two tabs in a row; insert current selection and close acw
+ cursel = int(self.listbox.curselection()[0])
+ self._change_start(self.completions[cursel])
+ self.hide_window()
+ return "break"
+ else:
+ # first tab; let AutoComplete handle the completion
+ self.userwantswindow = True
+ self.lastkey_was_tab = True
+ return
elif any(s in keysym for s in ("Shift", "Control", "Alt",
"Meta", "Command", "Option")):