moved some Pyimport stuff to autoload and added a new_buffer option

This commit is contained in:
David Halter
2013-08-22 11:27:38 +04:30
parent 748a47e6e9
commit c58fc7c952
3 changed files with 71 additions and 54 deletions

View File

@@ -83,18 +83,65 @@ function! jedi#show_documentation()
let b:current_syntax = "rst"
endfunction
" ------------------------------------------------------------------------
" helper functions
" ------------------------------------------------------------------------
function! jedi#py_import(args)
Python << EOF
# args are the same as for the :edit command
if 1:
import vim
import jedi
import os.path as osp
from shlex import split as shsplit
args = shsplit(vim.eval('a:args'))
text = 'import %s' % args.pop()
scr = jedi.Script(text, 1, len(text), '')
try:
path = scr.goto_assignments()[0].module_path
except IndexError:
path = None
if path and osp.isfile(path):
cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args])
jedi_vim.new_buffer(path, cmd_args)
EOF
endfun
function! jedi#py_import_completion(argl, cmdl, pos)
Python << EOF
if 1:
import vim
import re
import json
argl = vim.eval('a:argl')
try:
import jedi
except ImportError as err:
print('Pyimport completion requires jedi module: https://github.com/davidhalter/jedi')
comps = []
else:
text = 'import %s' % argl
script=jedi.Script(text, 1, len(text), '')
comps = ['%s%s' % (argl, c.complete) for c in script.completions()]
vim.command("let comps = '%s'" % '\n'.join(comps))
EOF
return comps
endfun
" ------------------------------------------------------------------------
" helper functions
" ------------------------------------------------------------------------
function! jedi#new_buffer(path)
function! jedi#new_buffer(path, options)
" options are what you can to edit the edit options
if g:jedi#use_tabs_not_buffers
Python jedi_vim.tabnew(jedi_vim.escape_file_path(vim.eval('a:path')))
Python jedi_vim.tabnew(jedi_vim.escape_file_path(vim.eval('a:path')), vim.eval('a:options'))
else
if !&hidden && &modified
w
endif
Python vim.command('edit ' + jedi_vim.escape_file_path(vim.eval('a:path')))
Python vim.command('edit ' + vim.eval('a:options') + jedi_vim.escape_file_path(vim.eval('a:path')))
endif
" sometimes syntax is being disabled and the filetype not set.
if !exists("g:syntax_on")
@@ -105,6 +152,7 @@ function! jedi#new_buffer(path)
endif
endfunction
function! jedi#add_goto_window()
set lazyredraw
cclose
@@ -158,6 +206,7 @@ function! jedi#do_popup_on_dot()
return 1
endfunc
function! jedi#configure_call_signatures()
autocmd InsertLeave <buffer> Python jedi_vim.clear_call_signatures()
autocmd CursorMovedI <buffer> call jedi#show_call_signatures()

View File

@@ -79,50 +79,10 @@ if g:jedi#auto_initialization
autocmd FileType Python setlocal omnifunc=jedi#completions switchbuf=useopen " needed for documentation/pydoc
endif
fun! Pyimport(args)
Python << EOF
# args are the same as for the :edit command
# cmd: one of edit, split, vsplit, tabedit, ...
if 1:
import vim
import jedi
import os.path as osp
from shlex import split as shsplit
cmd = vim.eval('a:cmd')
args = shsplit(vim.eval('a:args'))
text = 'import %s' % args.pop()
scr = jedi.Script(text, 1, len(text), '')
try:
path = scr.goto_assignments()[0].module_path
except IndexError:
path = None
if path and osp.isfile(path):
cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args])
vim.eval("jedi#new_buffer('%s')" % path)
EOF
endfun
" ------------------------------------------------------------------------
" PyImport command
" ------------------------------------------------------------------------
command! -nargs=1 -complete=custom,jedi#py_import_completion Pyimport :call jedi#py_import(<q-args>)
fun! Pyimport_completion(argl, cmdl, pos)
Python << EOF
if 1:
import vim
import re
import json
argl = vim.eval('a:argl')
try:
import jedi
except ImportError as err:
print('Pyimport completion requires jedi module: https://github.com/davidhalter/jedi')
comps = []
else:
text = 'import %s' % argl
script=jedi.Script(text, 1, len(text), '')
comps = ['%s%s' % (argl, c.complete) for c in script.completions()]
vim.command("let comps = '%s'" % '\n'.join(comps))
EOF
return comps
endfun
command! -nargs=1 -complete=custom,Pyimport_completion Pyimport :call Pyimport(<q-args>)
" vim: set et ts=4:

View File

@@ -140,8 +140,7 @@ def goto(is_definition=False, is_related_name=False, no_output=False):
% d.module_path)
else:
if d.module_path != vim.current.buffer.name:
vim.eval('jedi#new_buffer(%s)' % \
repr(PythonToVimStr(d.module_path)))
new_buffer(d.module_path)
vim.current.window.cursor = d.line, d.column
vim.command('normal! zt') # cursor at top of screen
else:
@@ -302,21 +301,30 @@ def rename():
continue
if vim.current.buffer.name != r.module_path:
vim.eval("jedi#new_buffer('%s')" % r.module_path)
new_buffer(r.module_path)
vim.current.window.cursor = r.start_pos
vim.command('normal! cw%s' % replace)
vim.eval("jedi#new_buffer('%s')" % window_path)
new_buffer(window_path)
vim.current.window.cursor = cursor
echo_highlight('Jedi did %s renames!' % len(temp_rename))
def tabnew(path):
"Open a file in a new tab or switch to an existing one"
def new_buffer(path, options=''):
path = repr(PythonToVimStr(path))
vim.eval("jedi#new_buffer(%s, '%s')" % (path, options))
def tabnew(path, options=''):
"""
Open a file in a new tab or switch to an existing one.
:param options: `:tabnew` options, read vim help.
"""
path = os.path.abspath(path)
if vim.eval('has("gui")') == '1':
vim.command('tab drop %s' % path)
vim.command('tab drop %s %s' % (options, path))
return
for tab_nr in range(int(vim.eval("tabpagenr('$')"))):