diff --git a/jedi/api/completion.py b/jedi/api/completion.py index f53c03f6..0602d489 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -80,7 +80,7 @@ class Completion: self._code_lines = code_lines # The first step of completions is to get the name - self._like_name = helpers.get_on_completion_name(module, position) + self._like_name = helpers.get_on_completion_name(module, code_lines, position) # The actual cursor position is not what we need to calculate # everything. We want the start of the name we're on. self._position = position[0], position[1] - len(self._like_name) diff --git a/jedi/api/helpers.py b/jedi/api/helpers.py index 7a7abfe1..d726da69 100644 --- a/jedi/api/helpers.py +++ b/jedi/api/helpers.py @@ -20,23 +20,16 @@ def sorted_definitions(defs): return sorted(defs, key=lambda x: (x.module_path or '', x.line or 0, x.column or 0)) -def get_on_completion_name(module, position): +def get_on_completion_name(module, lines, position): leaf = module.get_leaf_for_position(position) if leaf is None: return '' - elif leaf.type == 'string': + elif leaf.type in ('string', 'error_leaf'): # Completions inside strings are a bit special, we need to parse the # string. - lines = leaf.value.splitlines() - start_pos = leaf.start_pos - difference = position[0] - start_pos[0] - if difference == 0: - indent = start_pos[1] - else: - indent = 0 - line = lines[difference][:position[1] - indent] + line = lines[position[0] - 1] # The first step of completions is to get the name - return re.search(r'(?!\d)\w+$|$', line).group(0) + return re.search(r'(?!\d)\w+$|$', line[:position[1]]).group(0) elif leaf.type not in ('name', 'keyword'): return '' @@ -129,7 +122,6 @@ def get_stack_at_position(grammar, code_lines, module, pos): safeword = 'XXX_USER_WANTS_TO_COMPLETE_HERE_WITH_JEDI' # Remove as many indents from **all** code lines as possible. code = dedent(code + safeword) - print(repr(code)) p = parser.Parser(grammar, code, start_parsing=False) try: diff --git a/jedi/utils.py b/jedi/utils.py index 05da0401..2502620c 100644 --- a/jedi/utils.py +++ b/jedi/utils.py @@ -74,7 +74,7 @@ def setup_readline(namespace_module=__main__): lines = common.splitlines(text) position = (len(lines), len(lines[-1])) - name = get_on_completion_name(interpreter._get_module(), position) + name = get_on_completion_name(interpreter._get_module(), lines, position) before = text[:len(text) - len(name)] completions = interpreter.completions() finally: