again refactorings

This commit is contained in:
David Halter
2012-09-10 11:06:31 +02:00
parent b29fdbdfde
commit a93f695b7d
6 changed files with 51 additions and 27 deletions

View File

@@ -325,21 +325,6 @@ def related_names(definitions, search_name, modules):
return result return result
names = [] 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 # TODO check modules in the same directoy
for m in modules: for m in modules:
try: try:

View File

@@ -229,12 +229,12 @@ def get_definition(source, line, column, source_path):
scopes = set([f.parser.user_scope]) scopes = set([f.parser.user_scope])
elif not goto_path: elif not goto_path:
op = f.get_operator_under_cursor() 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: else:
scopes = _prepare_goto(source, pos, source_path, f, goto_path) scopes = _prepare_goto(source, pos, source_path, f, goto_path)
# add keywords # 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]) d = set([Definition(s) for s in scopes])
_clear_caches() _clear_caches()
@@ -288,7 +288,7 @@ def related_names(source, line, column, source_path):
else: else:
e = evaluate.Class(f.parser.user_scope) e = evaluate.Class(f.parser.user_scope)
definitions = [e] 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] definitions = [f.parser.user_stmt]
else: else:
scopes = _prepare_goto(source, pos, source_path, f, goto_path) 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 = set([d.get_parent_until() for d in definitions])
module.add(f.parser.module) module.add(f.parser.module)
names = dynamic.related_names(definitions, search_name, 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() _clear_caches()
return names return names

View File

@@ -20,22 +20,23 @@ else:
keys = keyword.kwlist + ['None', 'False', 'True'] keys = keyword.kwlist + ['None', 'False', 'True']
def get_keywords(string='', all=False): def get_keywords(string='', pos=(0, 0), all=False):
if all: if all:
return set([Keyword(k) for k in keys]) return set([Keyword(k, pos) for k in keys])
if string in keys: if string in keys:
return set([Keyword(string)]) return set([Keyword(string, pos)])
return set() return set()
def get_operator(string): def get_operator(string, pos):
return Keyword(string) return Keyword(string, pos)
class Keyword(object): class Keyword(object):
def __init__(self, name): def __init__(self, name, pos):
self.name = name self.name = name
self.parent = lambda: None self.parent = lambda: None
self.start_pos = pos
def get_parent_until(self): def get_parent_until(self):
return builtin.builtin_scope return builtin.builtin_scope
@@ -49,6 +50,10 @@ class Keyword(object):
def imitate_pydoc(string): def imitate_pydoc(string):
"""
It's not possible to get the pydoc's without starting the annoying pager
stuff.
"""
h = pydoc.help h = pydoc.help
try: try:
# try to access symbols # try to access symbols

View File

@@ -75,7 +75,7 @@ endfunction
" goto " goto
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------
function! jedi#goto() function! jedi#goto()
python _goto(is_definition=False) python _goto()
endfunction endfunction
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------
@@ -89,7 +89,7 @@ endfunction
" related_names " related_names
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------
function! jedi#related_names() function! jedi#related_names()
python _goto(related_names=True) python _goto(is_related_name=True)
endfunction endfunction
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------

View File

@@ -58,3 +58,14 @@ def blub():
#< (55,4) (59,1) #< (55,4) (59,1)
@blub @blub
def a(): pass def a(): pass
#< (65,7) (68,0)
import colorama
#< (65,7) (68,0)
colorama
#< 3
import abc

View File

@@ -88,7 +88,7 @@ class TestRegression(unittest.TestCase):
s = ("def abc(): pass\n" s = ("def abc(): pass\n"
"abc.d.a.abc.d" "abc.d.a.abc.d"
) )
functions.get_related_names(s, 2, 2, '/') functions.related_names(s, 2, 2, '/')
if __name__ == '__main__': if __name__ == '__main__':