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 imports
import settings import settings
from _compatibility import next
__all__ = ['complete', 'goto', 'get_definition', __all__ = ['complete', 'goto', 'get_definition',
'NotFoundError', 'set_debug_function'] 'NotFoundError', 'set_debug_function']
@@ -76,7 +78,7 @@ class Definition(object):
try: try:
# is an array # is an array
return self.definition.type return self.definition.type
except: except AttributeError:
# is a statement # is a statement
return self.definition.get_code() return self.definition.get_code()
@@ -252,7 +254,11 @@ def get_definition(source, line, column, source_path):
f = modules.ModuleWithCursor(source_path, source=source, position=pos) f = modules.ModuleWithCursor(source_path, source=source, position=pos)
goto_path = f.get_path_under_cursor() goto_path = f.get_path_under_cursor()
scopes = _prepare_goto(source, pos, source_path, f, goto_path) 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)] d = [Definition(s) for s in set(scopes)]
_clear_caches() _clear_caches()
return d return d

View File

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

View File

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

View File

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