2017-09-10 18:19:47 -03:00
|
|
|
"""Complete either attribute names or file names.
|
2005-11-18 18:05:48 -04:00
|
|
|
|
2017-09-10 18:19:47 -03:00
|
|
|
Either on demand or after a user-selected delay after a key character,
|
|
|
|
pop up a list of candidates.
|
2005-11-18 18:05:48 -04:00
|
|
|
"""
|
2019-03-24 18:12:28 -03:00
|
|
|
import __main__
|
2005-11-18 18:05:48 -04:00
|
|
|
import os
|
|
|
|
import string
|
2016-08-31 01:50:55 -03:00
|
|
|
import sys
|
2005-11-18 18:05:48 -04:00
|
|
|
|
2019-08-04 20:48:52 -03:00
|
|
|
# Two types of completions; defined here for autocomplete_w import below.
|
|
|
|
ATTRS, FILES = 0, 1
|
2016-05-28 14:22:31 -03:00
|
|
|
from idlelib import autocomplete_w
|
2016-08-31 01:50:55 -03:00
|
|
|
from idlelib.config import idleConf
|
2016-05-28 14:22:31 -03:00
|
|
|
from idlelib.hyperparser import HyperParser
|
2007-08-09 23:45:06 -03:00
|
|
|
|
2019-08-04 20:48:52 -03:00
|
|
|
# Tuples passed to open_completions.
|
|
|
|
# EvalFunc, Complete, WantWin, Mode
|
|
|
|
FORCE = True, False, True, None # Control-Space.
|
|
|
|
TAB = False, True, True, None # Tab.
|
|
|
|
TRY_A = False, False, False, ATTRS # '.' for attributes.
|
|
|
|
TRY_F = False, False, False, FILES # '/' in quotes for file name.
|
|
|
|
|
2016-08-31 01:50:55 -03:00
|
|
|
# This string includes all chars that may be in an identifier.
|
|
|
|
# TODO Update this here and elsewhere.
|
|
|
|
ID_CHARS = string.ascii_letters + string.digits + "_"
|
|
|
|
|
2019-08-04 20:48:52 -03:00
|
|
|
SEPS = f"{os.sep}{os.altsep if os.altsep else ''}"
|
|
|
|
TRIGGERS = f".{SEPS}"
|
2016-08-31 01:50:55 -03:00
|
|
|
|
2005-11-18 18:05:48 -04:00
|
|
|
class AutoComplete:
|
|
|
|
|
|
|
|
def __init__(self, editwin=None):
|
|
|
|
self.editwin = editwin
|
2019-08-04 20:48:52 -03:00
|
|
|
if editwin is not None: # not in subprocess or no-gui test
|
2016-07-24 21:35:43 -03:00
|
|
|
self.text = editwin.text
|
2019-08-04 20:48:52 -03:00
|
|
|
self.autocompletewindow = None
|
|
|
|
# id of delayed call, and the index of the text insert when
|
|
|
|
# the delayed call was issued. If _delayed_completion_id is
|
|
|
|
# None, there is no delayed call.
|
|
|
|
self._delayed_completion_id = None
|
|
|
|
self._delayed_completion_index = None
|
2005-11-18 18:05:48 -04:00
|
|
|
|
2017-09-10 18:19:47 -03:00
|
|
|
@classmethod
|
|
|
|
def reload(cls):
|
|
|
|
cls.popupwait = idleConf.GetOption(
|
|
|
|
"extensions", "AutoComplete", "popupwait", type="int", default=0)
|
|
|
|
|
2019-08-04 20:48:52 -03:00
|
|
|
def _make_autocomplete_window(self): # Makes mocking easier.
|
2016-05-28 14:22:31 -03:00
|
|
|
return autocomplete_w.AutoCompleteWindow(self.text)
|
2005-11-18 18:05:48 -04:00
|
|
|
|
|
|
|
def _remove_autocomplete_window(self, event=None):
|
|
|
|
if self.autocompletewindow:
|
|
|
|
self.autocompletewindow.hide_window()
|
|
|
|
self.autocompletewindow = None
|
|
|
|
|
|
|
|
def force_open_completions_event(self, event):
|
2019-08-04 20:48:52 -03:00
|
|
|
"(^space) Open completion list, even if a function call is needed."
|
|
|
|
self.open_completions(FORCE)
|
2017-06-27 01:02:32 -03:00
|
|
|
return "break"
|
2005-11-18 18:05:48 -04:00
|
|
|
|
|
|
|
def autocomplete_event(self, event):
|
2019-08-04 20:48:52 -03:00
|
|
|
"(tab) Complete word or open list if multiple options."
|
2016-07-25 00:01:28 -03:00
|
|
|
if hasattr(event, "mc_state") and event.mc_state or\
|
|
|
|
not self.text.get("insert linestart", "insert").strip():
|
|
|
|
# A modifier was pressed along with the tab or
|
|
|
|
# there is only previous whitespace on this line, so tab.
|
2016-07-24 21:35:43 -03:00
|
|
|
return None
|
2005-11-18 18:05:48 -04:00
|
|
|
if self.autocompletewindow and self.autocompletewindow.is_active():
|
|
|
|
self.autocompletewindow.complete()
|
|
|
|
return "break"
|
|
|
|
else:
|
2019-08-04 20:48:52 -03:00
|
|
|
opened = self.open_completions(TAB)
|
2016-07-25 00:01:28 -03:00
|
|
|
return "break" if opened else None
|
2005-11-18 18:05:48 -04:00
|
|
|
|
2019-08-04 20:48:52 -03:00
|
|
|
def try_open_completions_event(self, event=None):
|
|
|
|
"(./) Open completion list after pause with no movement."
|
|
|
|
lastchar = self.text.get("insert-1c")
|
|
|
|
if lastchar in TRIGGERS:
|
|
|
|
args = TRY_A if lastchar == "." else TRY_F
|
|
|
|
self._delayed_completion_index = self.text.index("insert")
|
|
|
|
if self._delayed_completion_id is not None:
|
|
|
|
self.text.after_cancel(self._delayed_completion_id)
|
|
|
|
self._delayed_completion_id = self.text.after(
|
|
|
|
self.popupwait, self._delayed_open_completions, args)
|
|
|
|
|
|
|
|
def _delayed_open_completions(self, args):
|
|
|
|
"Call open_completions if index unchanged."
|
2005-11-18 18:05:48 -04:00
|
|
|
self._delayed_completion_id = None
|
2016-07-24 21:35:43 -03:00
|
|
|
if self.text.index("insert") == self._delayed_completion_index:
|
2019-08-04 20:48:52 -03:00
|
|
|
self.open_completions(args)
|
2005-11-18 18:05:48 -04:00
|
|
|
|
2019-08-04 20:48:52 -03:00
|
|
|
def open_completions(self, args):
|
2005-11-18 18:05:48 -04:00
|
|
|
"""Find the completions and create the AutoCompleteWindow.
|
|
|
|
Return True if successful (no syntax error or so found).
|
2019-03-24 20:33:12 -03:00
|
|
|
If complete is True, then if there's nothing to complete and no
|
2005-11-18 18:05:48 -04:00
|
|
|
start of completion, won't open completions and return False.
|
|
|
|
If mode is given, will open a completion list only in this mode.
|
|
|
|
"""
|
2019-08-04 20:48:52 -03:00
|
|
|
evalfuncs, complete, wantwin, mode = args
|
2005-11-18 18:05:48 -04:00
|
|
|
# Cancel another delayed call, if it exists.
|
|
|
|
if self._delayed_completion_id is not None:
|
|
|
|
self.text.after_cancel(self._delayed_completion_id)
|
|
|
|
self._delayed_completion_id = None
|
|
|
|
|
|
|
|
hp = HyperParser(self.editwin, "insert")
|
|
|
|
curline = self.text.get("insert linestart", "insert")
|
|
|
|
i = j = len(curline)
|
2019-08-04 20:48:52 -03:00
|
|
|
if hp.is_in_string() and (not mode or mode==FILES):
|
2019-03-24 20:33:12 -03:00
|
|
|
# Find the beginning of the string.
|
|
|
|
# fetch_completions will look at the file system to determine
|
|
|
|
# whether the string value constitutes an actual file name
|
|
|
|
# XXX could consider raw strings here and unescape the string
|
|
|
|
# value if it's not raw.
|
2005-11-18 18:05:48 -04:00
|
|
|
self._remove_autocomplete_window()
|
2019-08-04 20:48:52 -03:00
|
|
|
mode = FILES
|
2012-06-03 06:55:32 -03:00
|
|
|
# Find last separator or string start
|
|
|
|
while i and curline[i-1] not in "'\"" + SEPS:
|
2005-11-18 18:05:48 -04:00
|
|
|
i -= 1
|
|
|
|
comp_start = curline[i:j]
|
|
|
|
j = i
|
2012-06-03 06:55:32 -03:00
|
|
|
# Find string start
|
|
|
|
while i and curline[i-1] not in "'\"":
|
2005-11-18 18:05:48 -04:00
|
|
|
i -= 1
|
|
|
|
comp_what = curline[i:j]
|
2019-08-04 20:48:52 -03:00
|
|
|
elif hp.is_in_code() and (not mode or mode==ATTRS):
|
2005-11-18 18:05:48 -04:00
|
|
|
self._remove_autocomplete_window()
|
2019-08-04 20:48:52 -03:00
|
|
|
mode = ATTRS
|
2012-06-14 10:37:21 -03:00
|
|
|
while i and (curline[i-1] in ID_CHARS or ord(curline[i-1]) > 127):
|
2005-11-18 18:05:48 -04:00
|
|
|
i -= 1
|
|
|
|
comp_start = curline[i:j]
|
2019-08-04 20:48:52 -03:00
|
|
|
if i and curline[i-1] == '.': # Need object with attributes.
|
2005-11-18 18:05:48 -04:00
|
|
|
hp.set_index("insert-%dc" % (len(curline)-(i-1)))
|
|
|
|
comp_what = hp.get_expression()
|
2019-08-04 20:48:52 -03:00
|
|
|
if (not comp_what or
|
|
|
|
(not evalfuncs and comp_what.find('(') != -1)):
|
2016-07-24 21:35:43 -03:00
|
|
|
return None
|
2005-11-18 18:05:48 -04:00
|
|
|
else:
|
|
|
|
comp_what = ""
|
|
|
|
else:
|
2016-07-24 21:35:43 -03:00
|
|
|
return None
|
2005-11-18 18:05:48 -04:00
|
|
|
|
|
|
|
if complete and not comp_what and not comp_start:
|
2016-07-24 21:35:43 -03:00
|
|
|
return None
|
2005-11-18 18:05:48 -04:00
|
|
|
comp_lists = self.fetch_completions(comp_what, mode)
|
|
|
|
if not comp_lists[0]:
|
2016-07-24 21:35:43 -03:00
|
|
|
return None
|
2005-11-18 18:05:48 -04:00
|
|
|
self.autocompletewindow = self._make_autocomplete_window()
|
2013-09-11 16:46:27 -03:00
|
|
|
return not self.autocompletewindow.show_window(
|
|
|
|
comp_lists, "insert-%dc" % len(comp_start),
|
2019-08-04 20:48:52 -03:00
|
|
|
complete, mode, wantwin)
|
2005-11-18 18:05:48 -04:00
|
|
|
|
|
|
|
def fetch_completions(self, what, mode):
|
|
|
|
"""Return a pair of lists of completions for something. The first list
|
|
|
|
is a sublist of the second. Both are sorted.
|
|
|
|
|
|
|
|
If there is a Python subprocess, get the comp. list there. Otherwise,
|
|
|
|
either fetch_completions() is running in the subprocess itself or it
|
|
|
|
was called in an IDLE EditorWindow before any script had been run.
|
|
|
|
|
|
|
|
The subprocess environment is that of the most recently run script. If
|
|
|
|
two unrelated modules are being edited some calltips in the current
|
|
|
|
module may be inoperative if the module was not the last to run.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
rpcclt = self.editwin.flist.pyshell.interp.rpcclt
|
|
|
|
except:
|
|
|
|
rpcclt = None
|
|
|
|
if rpcclt:
|
|
|
|
return rpcclt.remotecall("exec", "get_the_completion_list",
|
|
|
|
(what, mode), {})
|
|
|
|
else:
|
2019-08-04 20:48:52 -03:00
|
|
|
if mode == ATTRS:
|
2005-11-18 18:05:48 -04:00
|
|
|
if what == "":
|
2019-03-24 18:12:28 -03:00
|
|
|
namespace = {**__main__.__builtins__.__dict__,
|
|
|
|
**__main__.__dict__}
|
2005-11-18 18:05:48 -04:00
|
|
|
bigl = eval("dir()", namespace)
|
|
|
|
bigl.sort()
|
|
|
|
if "__all__" in bigl:
|
2012-02-05 15:31:16 -04:00
|
|
|
smalll = sorted(eval("__all__", namespace))
|
2005-11-18 18:05:48 -04:00
|
|
|
else:
|
2007-08-09 23:41:21 -03:00
|
|
|
smalll = [s for s in bigl if s[:1] != '_']
|
2005-11-18 18:05:48 -04:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
entity = self.get_entity(what)
|
|
|
|
bigl = dir(entity)
|
|
|
|
bigl.sort()
|
|
|
|
if "__all__" in bigl:
|
2012-02-05 15:31:16 -04:00
|
|
|
smalll = sorted(entity.__all__)
|
2005-11-18 18:05:48 -04:00
|
|
|
else:
|
2007-08-09 23:41:21 -03:00
|
|
|
smalll = [s for s in bigl if s[:1] != '_']
|
2005-11-18 18:05:48 -04:00
|
|
|
except:
|
|
|
|
return [], []
|
|
|
|
|
2019-08-04 20:48:52 -03:00
|
|
|
elif mode == FILES:
|
2005-11-18 18:05:48 -04:00
|
|
|
if what == "":
|
|
|
|
what = "."
|
|
|
|
try:
|
|
|
|
expandedpath = os.path.expanduser(what)
|
|
|
|
bigl = os.listdir(expandedpath)
|
|
|
|
bigl.sort()
|
2007-08-09 23:41:21 -03:00
|
|
|
smalll = [s for s in bigl if s[:1] != '.']
|
2005-11-18 18:05:48 -04:00
|
|
|
except OSError:
|
|
|
|
return [], []
|
|
|
|
|
|
|
|
if not smalll:
|
|
|
|
smalll = bigl
|
|
|
|
return smalll, bigl
|
|
|
|
|
|
|
|
def get_entity(self, name):
|
2019-03-24 18:12:28 -03:00
|
|
|
"Lookup name in a namespace spanning sys.modules and __main.dict__."
|
|
|
|
return eval(name, {**sys.modules, **__main__.__dict__})
|
2014-06-03 21:54:21 -03:00
|
|
|
|
|
|
|
|
2017-09-10 18:19:47 -03:00
|
|
|
AutoComplete.reload()
|
|
|
|
|
2014-06-03 21:54:21 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from unittest import main
|
|
|
|
main('idlelib.idle_test.test_autocomplete', verbosity=2)
|