mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-10 07:41:51 +08:00
Catch an additional case for get_context where the cursor is e.g. on the function name
This commit is contained in:
@@ -420,14 +420,25 @@ class Script(object):
|
|||||||
|
|
||||||
@validate_line_column
|
@validate_line_column
|
||||||
def get_context(self, line=None, column=None):
|
def get_context(self, line=None, column=None):
|
||||||
leaf = self._module_node.get_leaf_for_position((line, column), include_prefixes=True)
|
pos = (line, column)
|
||||||
if leaf.start_pos > (line, column) or leaf.type == 'endmarker':
|
leaf = self._module_node.get_leaf_for_position(pos, include_prefixes=True)
|
||||||
|
if leaf.start_pos > pos or leaf.type == 'endmarker':
|
||||||
previous_leaf = leaf.get_previous_leaf()
|
previous_leaf = leaf.get_previous_leaf()
|
||||||
if previous_leaf is not None:
|
if previous_leaf is not None:
|
||||||
leaf = previous_leaf
|
leaf = previous_leaf
|
||||||
|
|
||||||
module_context = self._get_module_context()
|
module_context = self._get_module_context()
|
||||||
context = module_context.create_context(leaf)
|
|
||||||
|
n = tree.search_ancestor(leaf, 'funcdef', 'classdef')
|
||||||
|
if n is not None and n.start_pos < pos <= n.children[-1].start_pos:
|
||||||
|
# This is a bit of a special case. The context of a function/class
|
||||||
|
# name/param/keyword is always it's parent context, not the
|
||||||
|
# function itself. Catch all the cases here where we are before the
|
||||||
|
# suite object, but still in the function.
|
||||||
|
context = module_context.create_value(n).as_context()
|
||||||
|
else:
|
||||||
|
context = module_context.create_context(leaf)
|
||||||
|
|
||||||
while context.name is None:
|
while context.name is None:
|
||||||
context = context.parent_context # comprehensions
|
context = context.parent_context # comprehensions
|
||||||
|
|
||||||
|
|||||||
@@ -61,8 +61,9 @@ def x():
|
|||||||
|
|
||||||
(func_code, 1, 0, 'myfile', []),
|
(func_code, 1, 0, 'myfile', []),
|
||||||
(func_code, 1, None, 'myfile.func1', ['func1']),
|
(func_code, 1, None, 'myfile.func1', ['func1']),
|
||||||
#(func_code, 1, 4, 'myfile.func1', ['func1']),
|
(func_code, 1, 1, 'myfile.func1', ['func1']),
|
||||||
#(func_code, 1, 10, 'myfile.func1', ['func1']),
|
(func_code, 1, 4, 'myfile.func1', ['func1']),
|
||||||
|
(func_code, 1, 10, 'myfile.func1', ['func1']),
|
||||||
|
|
||||||
(func_code, 3, 0, 'myfile', []),
|
(func_code, 3, 0, 'myfile', []),
|
||||||
(func_code, 5, None, 'myfile.func2', ['func2']),
|
(func_code, 5, None, 'myfile.func2', ['func2']),
|
||||||
@@ -84,6 +85,7 @@ def x():
|
|||||||
(cls_code, 4, 5, 'myfile.Foo.x', ['Foo', 'x']),
|
(cls_code, 4, 5, 'myfile.Foo.x', ['Foo', 'x']),
|
||||||
(cls_code, 4, 8, 'myfile.Foo.x', ['Foo', 'x']),
|
(cls_code, 4, 8, 'myfile.Foo.x', ['Foo', 'x']),
|
||||||
(cls_code, 4, 12, None, ['Foo', 'x', 'y']),
|
(cls_code, 4, 12, None, ['Foo', 'x', 'y']),
|
||||||
|
(cls_code, 1, 1, 'myfile.Foo', ['Foo']),
|
||||||
|
|
||||||
(cls_nested, 4, None, 'myfile.C.D.f', ['C', 'D', 'f']),
|
(cls_nested, 4, None, 'myfile.C.D.f', ['C', 'D', 'f']),
|
||||||
(cls_nested, 4, 3, 'myfile.C', ['C']),
|
(cls_nested, 4, 3, 'myfile.C', ['C']),
|
||||||
|
|||||||
Reference in New Issue
Block a user