From a93f695b7d9ce96083e1e3c8f87e6e5a4fdbee45 Mon Sep 17 00:00:00 2001 From: David Halter Date: Mon, 10 Sep 2012 11:06:31 +0200 Subject: [PATCH] again refactorings --- dynamic.py | 15 --------------- functions.py | 29 ++++++++++++++++++++++++++--- keywords.py | 17 +++++++++++------ plugin/jedi.vim | 4 ++-- test/completion/renaming.py | 11 +++++++++++ test/regression.py | 2 +- 6 files changed, 51 insertions(+), 27 deletions(-) diff --git a/dynamic.py b/dynamic.py index 6d324107..8bd42b5f 100644 --- a/dynamic.py +++ b/dynamic.py @@ -325,21 +325,6 @@ def related_names(definitions, search_name, modules): return result names = [] - for d in definitions: - if isinstance(d, parsing.Statement): - def add_array(arr): - calls = _scan_array(arr, search_name) - for call in calls: - for n in call.name.names: - if n == search_name: - names.append(RelatedName(n, d)) - for op, arr in d.assignment_details: - add_array(arr) - if not d.assignment_details: - add_array(d.get_assignment_calls()) - else: - names.append(RelatedName(d.name.names[0], d)) - # TODO check modules in the same directoy for m in modules: try: diff --git a/functions.py b/functions.py index d3cf145a..2ddac667 100644 --- a/functions.py +++ b/functions.py @@ -229,12 +229,12 @@ def get_definition(source, line, column, source_path): scopes = set([f.parser.user_scope]) elif not goto_path: op = f.get_operator_under_cursor() - scopes = set([keywords.get_operator(op)] if op else []) + scopes = set([keywords.get_operator(op, pos)] if op else []) else: scopes = _prepare_goto(source, pos, source_path, f, goto_path) # add keywords - scopes |= keywords.get_keywords(string=goto_path) + scopes |= keywords.get_keywords(string=goto_path, pos=pos) d = set([Definition(s) for s in scopes]) _clear_caches() @@ -288,7 +288,7 @@ def related_names(source, line, column, source_path): else: e = evaluate.Class(f.parser.user_scope) definitions = [e] - elif isinstance(f.parser.user_stmt, parsing.Param): + elif isinstance(f.parser.user_stmt, (parsing.Param, parsing.Import)): definitions = [f.parser.user_stmt] else: scopes = _prepare_goto(source, pos, source_path, f, goto_path) @@ -297,6 +297,29 @@ def related_names(source, line, column, source_path): module = set([d.get_parent_until() for d in definitions]) module.add(f.parser.module) names = dynamic.related_names(definitions, search_name, module) + + for d in definitions: + if isinstance(d, parsing.Statement): + def add_array(arr): + calls = dynamic._scan_array(arr, search_name) + for call in calls: + for n in call.name.names: + if n == search_name: + names.append(dynamic.RelatedName(n, d)) + for op, arr in d.assignment_details: + add_array(arr) + if not d.assignment_details: + add_array(d.get_assignment_calls()) + elif isinstance(d, parsing.Import): + for name in [d.namespace, d.alias, d.from_ns]: + if name: + for n in name.names: + print n.start_pos, pos, n.end_pos + if n.start_pos <= pos <= n.end_pos: + names.append(n, d) + else: + names.append(dynamic.RelatedName(d.name.names[0], d)) + _clear_caches() return names diff --git a/keywords.py b/keywords.py index f7dd0817..eeee7bf9 100644 --- a/keywords.py +++ b/keywords.py @@ -20,22 +20,23 @@ else: keys = keyword.kwlist + ['None', 'False', 'True'] -def get_keywords(string='', all=False): +def get_keywords(string='', pos=(0, 0), all=False): if all: - return set([Keyword(k) for k in keys]) + return set([Keyword(k, pos) for k in keys]) if string in keys: - return set([Keyword(string)]) + return set([Keyword(string, pos)]) return set() -def get_operator(string): - return Keyword(string) +def get_operator(string, pos): + return Keyword(string, pos) class Keyword(object): - def __init__(self, name): + def __init__(self, name, pos): self.name = name self.parent = lambda: None + self.start_pos = pos def get_parent_until(self): return builtin.builtin_scope @@ -49,6 +50,10 @@ class Keyword(object): def imitate_pydoc(string): + """ + It's not possible to get the pydoc's without starting the annoying pager + stuff. + """ h = pydoc.help try: # try to access symbols diff --git a/plugin/jedi.vim b/plugin/jedi.vim index cd501876..b6a4cc5f 100644 --- a/plugin/jedi.vim +++ b/plugin/jedi.vim @@ -75,7 +75,7 @@ endfunction " goto " ------------------------------------------------------------------------ function! jedi#goto() - python _goto(is_definition=False) + python _goto() endfunction " ------------------------------------------------------------------------ @@ -89,7 +89,7 @@ endfunction " related_names " ------------------------------------------------------------------------ function! jedi#related_names() - python _goto(related_names=True) + python _goto(is_related_name=True) endfunction " ------------------------------------------------------------------------ diff --git a/test/completion/renaming.py b/test/completion/renaming.py index 120b36b3..99ace8e7 100644 --- a/test/completion/renaming.py +++ b/test/completion/renaming.py @@ -58,3 +58,14 @@ def blub(): #< (55,4) (59,1) @blub def a(): pass + + + +#< (65,7) (68,0) +import colorama + +#< (65,7) (68,0) +colorama + +#< 3 +import abc diff --git a/test/regression.py b/test/regression.py index 029f5921..1819dd42 100755 --- a/test/regression.py +++ b/test/regression.py @@ -88,7 +88,7 @@ class TestRegression(unittest.TestCase): s = ("def abc(): pass\n" "abc.d.a.abc.d" ) - functions.get_related_names(s, 2, 2, '/') + functions.related_names(s, 2, 2, '/') if __name__ == '__main__':