From 35eae74e14fd69339270a61b622af43485aee358 2020-02-28 23:54:37 From: Matthias Bussonnier Date: 2020-02-28 23:54:37 Subject: [PATCH] Merge pull request #12157 from meeseeksmachine/auto-backport-of-pr-12128-on-7.x Backport PR #12128 on branch 7.x (Improves detection of whether tab-completion is in a string and suppresses Jedi) --- diff --git a/IPython/core/completer.py b/IPython/core/completer.py index 9a81d63..985ac5a 100644 --- a/IPython/core/completer.py +++ b/IPython/core/completer.py @@ -1375,18 +1375,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