diff --git a/README.mdown b/README.mdown index 9c13a32d..18e24393 100644 --- a/README.mdown +++ b/README.mdown @@ -171,6 +171,10 @@ The goto is by default on \. If you want to change that: let g:jedi#get_definition_command = d +Showing the pydoc is by default on `K` If you want to change that: + + let g:jedi#pydoc = "K" + If you are a person who likes to use VIM-tabs not buffers, you might want to put that in your `.vimrc`: diff --git a/evaluate.py b/evaluate.py index 06820750..7f49908d 100644 --- a/evaluate.py +++ b/evaluate.py @@ -265,7 +265,8 @@ class Instance(use_metaclass(CachedMetaClass, Executable)): return [] def __getattr__(self, name): - if name not in ['start_pos', 'end_pos', 'name', 'get_imports']: + if name not in ['start_pos', 'end_pos', 'name', 'get_imports', + 'docstr']: raise AttributeError("Instance %s: Don't touch this (%s)!" % (self, name)) return getattr(self.base, name) @@ -381,7 +382,7 @@ class Class(use_metaclass(CachedMetaClass, parsing.Base)): def __getattr__(self, name): if name not in ['start_pos', 'end_pos', 'parent', 'subscopes', - 'get_imports', 'get_parent_until']: + 'get_imports', 'get_parent_until', 'docstr']: raise AttributeError("Don't touch this (%s)!" % name) return getattr(self.base, name) diff --git a/functions.py b/functions.py index b4bf8888..6770ba28 100644 --- a/functions.py +++ b/functions.py @@ -121,6 +121,14 @@ class Definition(object): d = d.get_code().replace('\n', '') return d + @property + def doc(self): + try: + return str(self.definition.docstr) + except AttributeError: + return '' + + def __str__(self): if self.module_path[0] == os.path.sep: position = '@%s' % (self.line_nr) diff --git a/plugin/jedi.vim b/plugin/jedi.vim index f190b475..3e141bd5 100644 --- a/plugin/jedi.vim +++ b/plugin/jedi.vim @@ -85,6 +85,54 @@ function! jedi#get_definition() python _goto(is_definition=True) endfunction +" ------------------------------------------------------------------------ +" show_pydoc +" ------------------------------------------------------------------------ +function! jedi#show_pydoc() +python << PYTHONEOF +if 1: + row, column = vim.current.window.cursor + buf_path = vim.current.buffer.name + source = '\n'.join(vim.current.buffer) + try: + definitions = functions.get_definition(source, row, column, buf_path) + except functions.NotFoundError: + definitions = [] + except Exception: + # print to stdout, will be in :messages + definitions = [] + print("Exception, this shouldn't happen.") + print(traceback.format_exc()) + + if not definitions: + # just execute the default vim commands + 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) + vim.command('let l:doc = %s' % repr(PythonToVimStr(text))) +PYTHONEOF + if bufnr("__doc__") > 0 + " If the __doc__ buffer is open in the current window, jump to it + silent execute "sbuffer" bufnr("__doc__") + else + split '__doc__' + endif + setlocal modifiable + setlocal noswapfile + setlocal buftype=nofile + silent normal! ggdG + silent $put=l:doc + silent normal! 1Gdd + setlocal nomodifiable + setlocal nomodified + + " TODO more highlightings + highlight jedi_doc ctermbg=green guibg=green + match jedi_doc /^|.*|\n/ +endfunction + " ------------------------------------------------------------------------ " helper functions " ------------------------------------------------------------------------ @@ -176,14 +224,20 @@ endif if !exists("g:jedi#popup_on_dot") let g:jedi#popup_on_dot = 1 endif +if !exists("g:jedi#pydoc") + let g:jedi#pydoc = "K" +endif if g:jedi#auto_initialization autocmd FileType python set omnifunc=jedi#complete " map ctrl+space for autocompletion imap - execute "map ".g:jedi#goto_command." :call jedi#goto()" - execute "map ".g:jedi#get_definition_command." :call jedi#get_definition()" + " goto / get_definition + execute "noremap ".g:jedi#goto_command." :call jedi#goto()" + execute "noremap ".g:jedi#get_definition_command." :call jedi#get_definition()" + + execute "nnoremap ".g:jedi#pydoc." :call jedi#show_pydoc()" end if g:jedi#popup_on_dot