From fb0abc20aa02d30e9b22b9ec4f560ec6151a9235 2020-02-13 15:39:40 From: Coon, Ethan T Date: 2020-02-13 15:39:40 Subject: [PATCH] Improves detection of whether tab-completion is in a string and supresses Jedi. Refs #10926 and #11530 Jedi results swamp file_matches and dict_key_matches in tab-completion, which is a real nuisance. The logic in the jedi completor tried to catch cases where it was "in a string", but that logic only looked at the previous character, and was a little fragile, breaking in lots of cases such as: './ "mypath/ etc. This seems a bit more robust in that it searchs for the first token in the current parser tree and checks if its value starts with ' or ". This detection of "in a string" then turns of jedi and returns some sanity to the set of matches. --- diff --git a/IPython/core/completer.py b/IPython/core/completer.py index 0942798..5fd92f3 100644 --- a/IPython/core/completer.py +++ b/IPython/core/completer.py @@ -1371,18 +1371,18 @@ class IPCompleter(Completer): try_jedi = True try: - # should we check the type of the node is Error ? + # find the first token in the current tree -- if it is a ' or " then we are in a string + completing_string = False try: - # jedi < 0.11 - from jedi.parser.tree import ErrorLeaf - except ImportError: - # jedi >= 0.11 - from parso.tree import ErrorLeaf + first_child = next(c for c in interpreter._get_module().tree_node.children if hasattr(c, 'value')) + except StopIteration: + pass + else: + # note the value may be ', ", or it may also be ''' or """, or + # in some cases, """what/you/typed..., but all of these are + # strings. + completing_string = len(first_child.value) > 0 and first_child.value[0] in {"'", '"'} - next_to_last_tree = interpreter._get_module().tree_node.children[-2] - completing_string = False - if isinstance(next_to_last_tree, ErrorLeaf): - completing_string = next_to_last_tree.value.lstrip()[0] in {'"', "'"} # if we are in a string jedi is likely not the right candidate for # now. Skip it. try_jedi = not completing_string