move py_import function finally to jedi_vim.py

This commit is contained in:
David Halter
2013-08-22 11:49:08 +04:30
parent 63029f5ea2
commit 100e1ed8cd
3 changed files with 36 additions and 48 deletions

View File

@@ -42,6 +42,15 @@ function! jedi#disable_debugging()
Python jedi_vim.jedi.set_debug_function(None) Python jedi_vim.jedi.set_debug_function(None)
endfunction endfunction
function! jedi#py_import(args)
Python jedi_vim.py_import()
endfun
function! jedi#py_import_completions(argl, cmdl, pos)
Python jedi_vim.py_import_completions()
endfun
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------
" show_documentation " show_documentation
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------
@@ -83,53 +92,6 @@ function! jedi#show_documentation()
let b:current_syntax = "rst" let b:current_syntax = "rst"
endfunction 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 " helper functions
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------

View File

@@ -83,6 +83,6 @@ endif
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------
" PyImport command " PyImport command
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------
command! -nargs=1 -complete=custom,jedi#py_import_completion Pyimport :call jedi#py_import(<q-args>) command! -nargs=1 -complete=custom,jedi#py_import_completions Pyimport :call jedi#py_import(<q-args>)
" vim: set et ts=4: " vim: set et ts=4:

View File

@@ -6,6 +6,7 @@ with VIM.
import traceback # for exception output import traceback # for exception output
import re import re
import os import os
from shlex import split as shsplit
import vim import vim
import jedi import jedi
@@ -310,6 +311,31 @@ def rename():
vim.current.window.cursor = cursor vim.current.window.cursor = cursor
echo_highlight('Jedi did %s renames!' % len(temp_rename)) echo_highlight('Jedi did %s renames!' % len(temp_rename))
def py_import():
# args are the same as for the :edit command
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 os.path.isfile(path):
cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args])
new_buffer(path, cmd_args)
def py_import_completions():
argl = vim.eval('a:argl')
try:
import jedi
except ImportError:
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("return '%s'" % '\n'.join(comps))
def new_buffer(path, options=''): def new_buffer(path, options=''):
path = repr(PythonToVimStr(path)) path = repr(PythonToVimStr(path))