From 5306dfe8814ccf85163f3354c94e382a511654a3 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Mon, 10 Jun 2019 20:35:15 +0100 Subject: Updated pydoc plugin --- plugin/python_pydoc.vim | 106 ++++++++++++++++++++++++++++++++++++++++++------ vimrc | 3 ++ 2 files changed, 96 insertions(+), 13 deletions(-) diff --git a/plugin/python_pydoc.vim b/plugin/python_pydoc.vim index 30dcd65..3c5b52e 100644 --- a/plugin/python_pydoc.vim +++ b/plugin/python_pydoc.vim @@ -61,6 +61,21 @@ " " let g:pydoc_highlight=0 " +" If you want pydoc to switch to an already open tab with pydoc page, +" set this variable in your .vimrc (uses drop - requires vim compiled with +" gui!): +" +" let g:pydoc_use_drop=1 +" +" Pydoc files are open with 10 lines height, if you want to change this value +" put this in your .vimrc: +" +" let g:pydoc_window_lines=15 +" or +" let g:pydoc_window_lines=0.5 +" +" Float values specify a percentage of the current window. +" " " In order to install pydoc.vim download it from vim.org or clone the repository " on githubi and put it in your .vim/ftplugin directory. pydoc.vim is also fully @@ -95,7 +110,7 @@ if !exists('g:pydoc_highlight') endif if !exists('g:pydoc_cmd') - let g:pydoc_cmd = 'pydoc3' + let g:pydoc_cmd = 'pydoc' endif if !exists('g:pydoc_open_cmd') @@ -105,13 +120,26 @@ endif setlocal switchbuf=useopen highlight pydoc cterm=reverse gui=reverse -function s:ShowPyDoc(name, type) +function! s:GetWindowLine(value) + if a:value < 1 + return float2nr(winheight(0)*a:value) + else + return a:value + endif +endfunction + +" Args: name: lookup; type: 0: search, 1: lookup +function! s:ShowPyDoc(name, type) if a:name == '' return endif if g:pydoc_open_cmd == 'split' - let l:pydoc_wh = 10 + if exists('g:pydoc_window_lines') + let l:pydoc_wh = s:GetWindowLine(g:pydoc_window_lines) + else + let l:pydoc_wh = 10 + endif endif if bufloaded("__doc__") @@ -122,12 +150,16 @@ function s:ShowPyDoc(name, type) let l:pydoc_wh = -1 else " If the __doc__ buffer is open, jump to it - silent execute "sbuffer" bufnr("__doc__") + if exists("g:pydoc_use_drop") + execute "drop" "__doc__" + else + execute "sbuffer" bufnr("__doc__") + endif let l:pydoc_wh = -1 endif else let l:buf_is_new = 1 - silent execute g:pydoc_open_cmd '__doc__' + execute g:pydoc_open_cmd '__doc__' if g:pydoc_perform_mappings call s:PerformMappings() endif @@ -138,21 +170,26 @@ function s:ShowPyDoc(name, type) setlocal buftype=nofile setlocal bufhidden=delete setlocal syntax=man + setlocal nolist - silent normal ggdG + normal ggdG " Remove function/method arguments let s:name2 = substitute(a:name, '(.*', '', 'g' ) " Remove all colons let s:name2 = substitute(s:name2, ':', '', 'g' ) if a:type == 1 - execute "silent read !" g:pydoc_cmd s:name2 + let s:cmd = g:pydoc_cmd . ' ' . shellescape(s:name2) else - execute "silent read !" g:pydoc_cmd "-k" s:name2 + let s:cmd = g:pydoc_cmd . ' -k ' . shellescape(s:name2) + endif + if &verbose + echomsg "pydoc: calling " s:cmd endif + execute "silent read !" s:cmd normal 1G if exists('l:pydoc_wh') && l:pydoc_wh != -1 - execute "silent resize" l:pydoc_wh + execute "resize" l:pydoc_wh end if g:pydoc_highlight == 1 @@ -176,15 +213,56 @@ function s:ShowPyDoc(name, type) endif endfunction +function! s:ReplaceModuleAlias() + " Replace module aliases with their own name. + " + " For example: + " import foo as bar + " if `bar` is in the ExpandModulePath's return value, it should be + " replaced with `foo`. + let l:cur_col = col(".") + let l:cur_line = line(".") + let l:module_path = s:ExpandModulePath() + let l:module_names = split(l:module_path, '\.') + let l:module_orig_name = l:module_names[0] + if search('import \+[0-9a-zA-Z_.]\+ \+as \+' . l:module_orig_name) + let l:line = getline(".") + let l:name = matchlist(l:line, 'import \+\([a-zA-Z0-9_.]\+\) \+as')[1] + if l:name != '' + let l:module_orig_name = l:name + endif + endif + if l:module_names[0] != l:module_orig_name + let l:module_names[0] = l:module_orig_name + endif + call cursor(l:cur_line, l:cur_col) + return join(l:module_names, ".") +endfunction + +function! s:ExpandModulePath() + " Extract the 'word' at the cursor, expanding leftwards across identifiers + " and the . operator, and rightwards across the identifier only. + " + " For example: + " import xml.dom.minidom + " ^ ! + " + " With the cursor at ^ this returns 'xml'; at ! it returns 'xml.dom'. + let l:line = getline(".") + let l:pre = l:line[:col(".") - 1] + let l:suf = l:line[col("."):] + return matchstr(pre, "[A-Za-z0-9_.]*$") . matchstr(suf, "^[A-Za-z0-9_]*") +endfunction + " Mappings -function s:PerformMappings() +function! s:PerformMappings() nnoremap pw :call ShowPyDoc('', 1) nnoremap pW :call ShowPyDoc('', 1) nnoremap pk :call ShowPyDoc('', 0) nnoremap pK :call ShowPyDoc('', 0) " remap the K (or 'help') key - nnoremap K :call ShowPyDoc(expand(""), 1) + nnoremap K :call ShowPyDoc(ReplaceModuleAlias(), 1) endfunction if g:pydoc_perform_mappings @@ -192,5 +270,7 @@ if g:pydoc_perform_mappings endif " Commands -command -nargs=1 Pydoc :call s:ShowPyDoc('', 1) -command -nargs=* PydocSearch :call s:ShowPyDoc('', 0) +command! -nargs=1 Pydoc :call s:ShowPyDoc('', 1) +command! -nargs=* PydocSearch :call s:ShowPyDoc('', 0) +ca pyd Pydoc +ca pyds PydocSearch diff --git a/vimrc b/vimrc index 61669a2..6fe5f3f 100644 --- a/vimrc +++ b/vimrc @@ -57,6 +57,9 @@ let g:syntastic_mode_map = { " make easy to perform a check noremap :SyntasticCheck +" pydoc and python 3 +let g:pydoc_cmd = 'python3 -m pydoc' + "" vim-autoformat noremap :Autoformat -- cgit v1.2.3