pydoc works now also if the cursor is on the class / created context possibility for completions (backward tokenizer)

This commit is contained in:
David Halter
2012-09-05 14:19:40 +02:00
parent 37df49d598
commit abcd3f328a
4 changed files with 52 additions and 7 deletions

View File

@@ -10,6 +10,8 @@ import debug
import imports
import settings
from _compatibility import next
__all__ = ['complete', 'goto', 'get_definition',
'NotFoundError', 'set_debug_function']
@@ -76,7 +78,7 @@ class Definition(object):
try:
# is an array
return self.definition.type
except:
except AttributeError:
# is a statement
return self.definition.get_code()
@@ -252,6 +254,10 @@ def get_definition(source, line, column, source_path):
f = modules.ModuleWithCursor(source_path, source=source, position=pos)
goto_path = f.get_path_under_cursor()
context = f.get_context()
if next(context) in ('class', 'def'):
scopes = [f.parser.user_scope]
else:
scopes = _prepare_goto(source, pos, source_path, f, goto_path)
d = [Definition(s) for s in set(scopes)]
_clear_caches()

View File

@@ -52,14 +52,19 @@ class ModuleWithCursor(Module):
def get_path_until_cursor(self):
""" Get the path under the cursor. """
self._is_first = True
result = self._get_path_until_cursor()
self._start_cursor_pos = self._line_temp + 1, self._column_temp
return result
def _get_path_until_cursor(self, start_pos=None):
def fetch_line():
line = self.get_line(self._line_temp)
if self._is_first:
self._is_first = False
line = line[:self.position[1]]
self._line_length = self._column_temp
line = line[:self._column_temp]
else:
self._line_length = len(line)
line = line + '\n'
# add lines with a backslash at the end
while 1:
@@ -71,7 +76,12 @@ class ModuleWithCursor(Module):
break
return line[::-1]
self._is_first = True
if start_pos is None:
self._line_temp = self.position[0]
self._column_temp = self.position[1]
else:
self._line_temp, self._column_temp = start_pos
force_point = False
open_brackets = ['(', '[', '{']
@@ -107,6 +117,7 @@ class ModuleWithCursor(Module):
break
string += tok
self._column_temp = self._line_length - end[1]
except tokenize.TokenError:
debug.warning("Tokenize couldn't finish", sys.exc_info)
@@ -121,6 +132,12 @@ class ModuleWithCursor(Module):
after = re.search("[\w\d]*", line[self.position[1]:]).group(0)
return self.get_path_until_cursor() + after
def get_context(self):
pos = self._start_cursor_pos
while pos > 0:
yield self._get_path_until_cursor(start_pos=pos)
pos = self._line_temp, self._column_temp
def get_line(self, line_nr):
if not self._line_cache:
self._line_cache = self.source.split('\n')

View File

@@ -109,9 +109,11 @@ if 1:
vim.command('normal! K')
vim.command('return')
else:
docs = ['|Docstring of %s|\n%s' % (d, d.doc) for d in definitions]
text = ('-' * 79).join(docs)
docs = ['|Docstring for %s|\n%s' % (d, 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)))
vim.command('let l:doc_lines = %s' % len(text.split('\n')))
PYTHONEOF
if bufnr("__doc__") > 0
" If the __doc__ buffer is open in the current window, jump to it
@@ -119,6 +121,7 @@ PYTHONEOF
else
split '__doc__'
endif
setlocal modifiable
setlocal noswapfile
setlocal buftype=nofile
@@ -128,6 +131,11 @@ PYTHONEOF
setlocal nomodifiable
setlocal nomodified
if l:doc_lines > 20 " max 20 lines for plugin
let l:doc_lines = 20
endif
execute "resize ".l:doc_lines
" TODO more highlightings
highlight jedi_doc ctermbg=green guibg=green
match jedi_doc /^|.*|\n/

View File

@@ -120,3 +120,17 @@ def func():
#! 8 ['def b']
func().b()
# -----------------
# get_definition
# -----------------
#! 7 ['class ClassDef']
class ClassDef():
""" abc """
pass
##? 6 ClassDef2()
class ClassDef2():
""" abc """
pass