huge refactoring, moved new_buffer completely to python code

This commit is contained in:
David Halter
2013-08-23 23:38:52 +04:30
parent dcf884a6ca
commit b5b31aa0e6
2 changed files with 88 additions and 82 deletions

View File

@@ -95,25 +95,6 @@ endfunction
" ------------------------------------------------------------------------
" helper functions
" ------------------------------------------------------------------------
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')), vim.eval('a:options'))
else
if !&hidden && &modified
w
endif
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")
syntax enable
endif
if &filetype != 'python'
set filetype=python
endif
endfunction
function! jedi#add_goto_window()
set lazyredraw
@@ -134,7 +115,7 @@ function! s:goto_window_on_enter()
if l:data.bufnr
" close goto_window buffer
normal ZQ
call jedi#new_buffer(bufname(l:data.bufnr))
Python jedi_vim.new_buffer(vim.eval('bufname(l:data.bufnr)'))
call cursor(l:data.lnum, l:data.col)
else
echohl WarningMsg | echo "Builtin module cannot be opened." | echohl None

View File

@@ -14,6 +14,16 @@ import jedi.keywords
from jedi._compatibility import unicode
def catch_and_print_exceptions(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
print(traceback.format_exc())
return None
return wrapper
def echo_highlight(msg):
vim.command('echohl WarningMsg | echom "%s" | echohl None' % msg)
@@ -38,6 +48,7 @@ class PythonToVimStr(unicode):
return '"%s"' % s.replace('\\', '\\\\').replace('"', r'\"')
@catch_and_print_exceptions
def get_script(source=None, column=None):
jedi.settings.additional_dynamic_modules = [b.name for b in vim.buffers
if b.name is not None and b.name.endswith('.py')]
@@ -51,6 +62,7 @@ def get_script(source=None, column=None):
return jedi.Script(source, row, column, buf_path, encoding)
@catch_and_print_exceptions
def completions():
row, column = vim.current.window.cursor
clear_call_signatures()
@@ -102,6 +114,7 @@ def completions():
vim.command('return ' + strout)
@catch_and_print_exceptions
def goto(is_definition=False, is_related_name=False, no_output=False):
definitions = []
script = get_script()
@@ -114,10 +127,6 @@ def goto(is_definition=False, is_related_name=False, no_output=False):
definitions = script.goto_assignments()
except jedi.NotFoundError:
echo_highlight( "Cannot follow nothing. Put your cursor on a valid name.")
except Exception:
# print to stdout, will be in :messages
echo_highlight("Some different eror, this shouldn't happen.")
print(traceback.format_exc())
else:
if no_output:
return definitions
@@ -158,6 +167,7 @@ def goto(is_definition=False, is_related_name=False, no_output=False):
return definitions
@catch_and_print_exceptions
def show_documentation():
script = get_script()
try:
@@ -180,6 +190,7 @@ def show_documentation():
vim.command('let l:doc_lines = %s' % len(text.split('\n')))
@catch_and_print_exceptions
def clear_call_signatures():
cursor = vim.current.window.cursor
e = vim.eval('g:jedi#call_signature_escape')
@@ -196,11 +207,11 @@ def clear_call_signatures():
vim.current.window.cursor = cursor
@catch_and_print_exceptions
def show_call_signatures(signatures=()):
if vim.eval("has('conceal') && g:jedi#show_call_signatures") == '0':
return
try:
if signatures == ():
signatures = get_script().call_signatures()
clear_call_signatures()
@@ -261,10 +272,9 @@ def show_call_signatures(signatures=()):
repl = prefix + (regex % (tup, text)) + add + line[end_column:]
vim.eval('setline(%s, %s)' % (line_to_replace, repr(PythonToVimStr(repl))))
except Exception:
print(traceback.format_exc())
@catch_and_print_exceptions
def rename():
if not int(vim.eval('a:0')):
_rename_cursor = vim.current.window.cursor
@@ -312,6 +322,7 @@ def rename():
echo_highlight('Jedi did %s renames!' % len(temp_rename))
@catch_and_print_exceptions
def py_import():
# args are the same as for the :edit command
args = shsplit(vim.eval('a:args'))
@@ -330,6 +341,7 @@ def py_import():
new_buffer(completion.module_path, cmd_args)
@catch_and_print_exceptions
def py_import_completions():
argl = vim.eval('a:argl')
try:
@@ -344,12 +356,25 @@ def py_import_completions():
vim.command("return '%s'" % '\n'.join(comps))
@catch_and_print_exceptions
def new_buffer(path, options=''):
path = repr(PythonToVimStr(path))
vim.eval("jedi#new_buffer(%s, '%s')" % (path, options))
# options are what you can to edit the edit options
if vim.eval('g:jedi#use_tabs_not_buffers') == '1':
_tabnew(path, options)
else:
if vim.eval("!&hidden && &modified && bufname('%') != ") == '1':
vim.command('w')
print 'x'
vim.command('edit %s %s' (options, escape_file_path(path)))
# sometimes syntax is being disabled and the filetype not set.
if vim.eval('!exists("g:syntax_on")') == '1':
vim.command('syntax enable')
if vim.eval("&filetype != 'python'") == '1':
vim.command('set filetype=python')
def tabnew(path, options=''):
@catch_and_print_exceptions
def _tabnew(path, options=''):
"""
Open a file in a new tab or switch to an existing one.
@@ -357,7 +382,7 @@ def tabnew(path, options=''):
"""
path = os.path.abspath(path)
if vim.eval('has("gui")') == '1':
vim.command('tab drop %s %s' % (options, path))
vim.command('tab drop %s %s' % (options, escape_file_path(path)))
return
for tab_nr in range(int(vim.eval("tabpagenr('$')"))):
@@ -379,7 +404,7 @@ def tabnew(path, options=''):
break
else:
# tab doesn't exist, add a new one.
vim.command('tabnew %s' % path)
vim.command('tabnew %s' % escape_file_path(path))
def escape_file_path(path):