4 ms·
I really like the "instant" auto-completion. Can someone throw light on how it was done?
by xtacy 14y ago
I really like the "instant" auto-completion. Can someone throw light on how it was done?
- btn 14y agoThe code that generates the completions [1] and displays them [2] is fairly simple. It uses a mix of scanning for a function's docstring, Python's built-in rlcompleter module [3] to generate the possible identifiers and keywords, its own module scanner for imports [4]. [1] https://bitbucket.org/bobf/bpython/src/2b3fb2cb6500/bpython/repl.py#cl-583 https://bitbucket.org/bobf/bpython/src/2b3fb2cb6500/bpython/... [2] https://bitbucket.org/bobf/bpython/src/2b3fb2cb6500/bpython/cli.py#cl-409 https://bitbucket.org/bobf/bpython/src/2b3fb2cb6500/bpython/... [3] http://docs.python.org/library/rlcompleter.html http://docs.python.org/library/rlcompleter.html [4] https://bitbucket.org/bobf/bpython/src/2b3fb2cb6500/bpython/importcompletion.py https://bitbucket.org/bobf/bpython/src/2b3fb2cb6500/bpython/...
- xtacy 14y agoThat's very useful. I wish shells like bash/zsh had this instant auto-completion!
- bobfarrell 14y agoHi. It's done using ncurses for the display (if you've ever worked with curses you can imagine how much of a headache the whole thing was). The introspection is done using the `inspect` module for the arg spec/doc string, and either inspect.getmembers or just dir() to get the attributes (it's been a while since I wrote it). We had to do some pretty weird stuff to get the attribute inspection to not trigger __getattribute__ for objects that implement it (i.e. to introspect without side-effects) but it seems to work pretty well.