ska-sa / katpoint

Coordinate library for the MeerKAT project
BSD 3-Clause "New" or "Revised" License
1 stars 3 forks source link

Modernise tab completion (take 2) #39

Closed ludwigschwardt closed 6 years ago

ludwigschwardt commented 6 years ago

Since IPython 5.0 (PR ipython/ipython#9289 specifically) the customisation of dict key lookups has been considerably simplified by adding a specific method (_ipython_key_completions_) to your object. Strip out all the old tab completion machinery as a result.

Unfortunately the rest of the IPython machinery is still a bit clunky and the default completions now include all known tokens with that prefix, not only the ones returned by _ipython_key_completions_. This can be fixed by monkeypatching IPython in one of its startup files (ask the author).

This is the second attempt at PR #33.

ludwigschwardt commented 6 years ago

For the record, here is my new and shiny startup file that goes into ~/.ipython/profile_default/startup/06-fix-completer.py:

# XXX Hack to work around default IPython completer behaviour
try:
    _ip = get_ipython()  # IPython 0.11 and above
except NameError:
    pass
else:
    _comp = _ip.Completer
    if not hasattr(_comp, '_original_dcc'):
        _comp._original_dcc = _comp.dispatch_custom_completer
        def _better_dcc(self, text):
            """Do usual custom completion or give the dict completer a go."""
            custom_res = self._original_dcc(text)
            if custom_res is not None:
                return custom_res
            dict_res = self.dict_key_matches(text)
            return dict_res if dict_res else None
        _comp.dispatch_custom_completer = _better_dcc.__get__(_comp)