mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
cleaned up the mess in the Definition class
This commit is contained in:
39
functions.py
39
functions.py
@@ -70,25 +70,12 @@ class Definition(object):
|
||||
|
||||
self.module_path = str(self.definition.get_parent_until().path)
|
||||
|
||||
def get_name(self):
|
||||
try:
|
||||
# is a func / class
|
||||
return self.definition.name
|
||||
except AttributeError:
|
||||
try:
|
||||
# is an array
|
||||
return self.definition.type
|
||||
except AttributeError:
|
||||
# is a statement
|
||||
return self.definition.get_code()
|
||||
|
||||
@property
|
||||
def module_name(self):
|
||||
path = self.module_path
|
||||
try:
|
||||
return path[path.rindex(os.path.sep) + 1:]
|
||||
except ValueError:
|
||||
return path
|
||||
sep = os.path.sep
|
||||
p = re.sub(r'^.*?([\w\d]+)(%s__init__)?.py$' % sep, r'\1', path)
|
||||
return p
|
||||
|
||||
def in_builtin_module(self):
|
||||
return not self.module_path.endswith('.py')
|
||||
@@ -109,16 +96,15 @@ class Definition(object):
|
||||
if isinstance(d, evaluate.parsing.Name):
|
||||
d = d.parent()
|
||||
|
||||
if isinstance(d, (evaluate.Class, evaluate.Instance)):
|
||||
if isinstance(d, evaluate.Array):
|
||||
d = 'class ' + d.type
|
||||
elif isinstance(d, (evaluate.Class, evaluate.Instance)):
|
||||
d = 'class ' + str(d.name)
|
||||
elif isinstance(d, (evaluate.Function, evaluate.parsing.Function)):
|
||||
d = 'def ' + str(d.name)
|
||||
elif isinstance(d, evaluate.parsing.Module):
|
||||
p = str(d.path)
|
||||
# only show module name
|
||||
sep = os.path.sep
|
||||
p = re.sub(r'^.*?([\w\d]+)(%s__init__)?.py$' % sep, r'\1', p)
|
||||
d = 'module ' + p
|
||||
d = 'module %s' % self.module_name
|
||||
else:
|
||||
d = d.get_code().replace('\n', '')
|
||||
return d
|
||||
@@ -130,14 +116,15 @@ class Definition(object):
|
||||
except AttributeError:
|
||||
return ''
|
||||
|
||||
|
||||
def __str__(self):
|
||||
if self.module_path[0] == os.path.sep:
|
||||
@property
|
||||
def desc_with_module(self):
|
||||
if self.module_path.endswith('.py') \
|
||||
and not isinstance(self.definition, parsing.Module):
|
||||
position = '@%s' % (self.line_nr)
|
||||
else:
|
||||
# no path - is a builtin
|
||||
# is a builtin or module
|
||||
position = ''
|
||||
return "%s:%s%s" % (self.module_name, self.get_name(), position)
|
||||
return "%s:%s%s" % (self.module_name, self.description, position)
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %s>" % (self.__class__.__name__, self.definition)
|
||||
|
||||
@@ -109,7 +109,7 @@ if 1:
|
||||
vim.command('normal! K')
|
||||
vim.command('return')
|
||||
else:
|
||||
docs = ['|Docstring for %s|\n%s' % (d, d.doc) if d.doc
|
||||
docs = ['Docstring for %s\n%s\n%s' % (d.desc_with_module, '='*40, d.doc) if d.doc
|
||||
else '|No Docstring for %s|' % d for d in definitions]
|
||||
text = ('\n' + '-' * 79 + '\n').join(docs)
|
||||
vim.command('let l:doc = %s' % repr(PythonToVimStr(text)))
|
||||
@@ -130,6 +130,7 @@ PYTHONEOF
|
||||
silent normal! 1Gdd
|
||||
setlocal nomodifiable
|
||||
setlocal nomodified
|
||||
setlocal filetype=rst
|
||||
|
||||
if l:doc_lines > 20 " max 20 lines for plugin
|
||||
let l:doc_lines = 20
|
||||
@@ -137,8 +138,8 @@ PYTHONEOF
|
||||
execute "resize ".l:doc_lines
|
||||
|
||||
" TODO more highlightings
|
||||
highlight jedi_doc ctermbg=green guibg=green
|
||||
match jedi_doc /^|.*|\n/
|
||||
"highlight jedi_doc ctermbg=green guibg=green
|
||||
"match jedi_doc /^|.*|\n/
|
||||
endfunction
|
||||
|
||||
" ------------------------------------------------------------------------
|
||||
|
||||
@@ -72,8 +72,8 @@ def run_definition_test(correct, source, line_nr, index, line, correct_start,
|
||||
if print_debug:
|
||||
functions.set_debug_function(debug.print_to_stdout)
|
||||
# because the objects have different ids, `repr` it, then compare it.
|
||||
should_str = sorted(str(r) for r in should_be)
|
||||
is_str = sorted(set(str(r) for r in result))
|
||||
should_str = set(r.desc_with_module for r in should_be)
|
||||
is_str = set(r.desc_with_module for r in result)
|
||||
if is_str != should_str:
|
||||
print('Solution @%s not right, received %s, wanted %s' \
|
||||
% (line_nr - 1, is_str, should_str))
|
||||
|
||||
Reference in New Issue
Block a user