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