use safe vim.eval/vim.command if possible (better tracebacks)

This commit is contained in:
David Halter
2013-08-24 01:59:41 +04:30
parent bdf86d53f1
commit 2b52548b37
2 changed files with 39 additions and 37 deletions

View File

@@ -55,7 +55,7 @@ def vim_command(string):
def echo_highlight(msg): def echo_highlight(msg):
vim.command('echohl WarningMsg | echom "%s" | echohl None' % msg) vim_command('echohl WarningMsg | echom "%s" | echohl None' % msg)
if not hasattr(jedi, '__version__') or jedi.__version__ < (0, 7, 0): if not hasattr(jedi, '__version__') or jedi.__version__ < (0, 7, 0):
@@ -88,7 +88,7 @@ def get_script(source=None, column=None):
if column is None: if column is None:
column = vim.current.window.cursor[1] column = vim.current.window.cursor[1]
buf_path = vim.current.buffer.name buf_path = vim.current.buffer.name
encoding = vim.eval('&encoding') or 'latin1' encoding = vim_eval('&encoding') or 'latin1'
return jedi.Script(source, row, column, buf_path, encoding) return jedi.Script(source, row, column, buf_path, encoding)
@@ -166,7 +166,7 @@ def goto(is_definition=False, is_related_name=False, no_output=False):
# just add some mark to add the current position to the jumplist. # just add some mark to add the current position to the jumplist.
# this is ugly, because it overrides the mark for '`', so if anyone # this is ugly, because it overrides the mark for '`', so if anyone
# has a better idea, let me know. # has a better idea, let me know.
vim.command('normal! m`') vim_command('normal! m`')
d = list(definitions)[0] d = list(definitions)[0]
if d.in_builtin_module(): if d.in_builtin_module():
@@ -182,7 +182,7 @@ def goto(is_definition=False, is_related_name=False, no_output=False):
if not result: if not result:
return return
vim.current.window.cursor = d.line, d.column vim.current.window.cursor = d.line, d.column
vim.command('normal! zt') # cursor at top of screen vim_command('normal! zt') # cursor at top of screen
else: else:
# multiple solutions # multiple solutions
lst = [] lst = []
@@ -194,8 +194,8 @@ def goto(is_definition=False, is_related_name=False, no_output=False):
lst.append(dict(filename=PythonToVimStr(d.module_path), lst.append(dict(filename=PythonToVimStr(d.module_path),
lnum=d.line, col=d.column + 1, lnum=d.line, col=d.column + 1,
text=PythonToVimStr(d.description))) text=PythonToVimStr(d.description)))
vim.eval('setqflist(%s)' % repr(lst)) vim_eval('setqflist(%s)' % repr(lst))
vim.eval('jedi#add_goto_window()') vim_eval('jedi#add_goto_window()')
return definitions return definitions
@@ -225,23 +225,23 @@ def show_documentation():
@catch_and_print_exceptions @catch_and_print_exceptions
def clear_call_signatures(): def clear_call_signatures():
cursor = vim.current.window.cursor cursor = vim.current.window.cursor
e = vim.eval('g:jedi#call_signature_escape') e = vim_eval('g:jedi#call_signature_escape')
regex = r'%sjedi=([0-9]+), ([^%s]*)%s.*%sjedi%s'.replace('%s', e) regex = r'%sjedi=([0-9]+), ([^%s]*)%s.*%sjedi%s'.replace('%s', e)
for i, line in enumerate(vim.current.buffer): for i, line in enumerate(vim.current.buffer):
match = re.search(r'%s' % regex, line) match = re.search(r'%s' % regex, line)
if match is not None: if match is not None:
vim_regex = r'\v' + regex.replace('=', r'\=') + '.{%s}' % \ vim_regex = r'\v' + regex.replace('=', r'\=') + '.{%s}' % \
int(match.group(1)) int(match.group(1))
vim.command(r'try | %s,%ss/%s/\2/g | catch | endtry' \ vim_command(r'try | %s,%ss/%s/\2/g | catch | endtry' \
% (i + 1, i + 1, vim_regex)) % (i + 1, i + 1, vim_regex))
vim.eval('histdel("search", -1)') vim_eval('histdel("search", -1)')
vim.command('let @/ = histget("search", -1)') vim_command('let @/ = histget("search", -1)')
vim.current.window.cursor = cursor vim.current.window.cursor = cursor
@catch_and_print_exceptions @catch_and_print_exceptions
def show_call_signatures(signatures=()): def show_call_signatures(signatures=()):
if vim.eval("has('conceal') && g:jedi#show_call_signatures") == '0': if vim_eval("has('conceal') && g:jedi#show_call_signatures") == '0':
return return
if signatures == (): if signatures == ():
@@ -262,7 +262,7 @@ def show_call_signatures(signatures=()):
break break
# TODO check if completion menu is above or below # TODO check if completion menu is above or below
line = vim.eval("getline(%s)" % line_to_replace) line = vim_eval("getline(%s)" % line_to_replace)
params = [p.get_code().replace('\n', '') for p in signature.params] params = [p.get_code().replace('\n', '') for p in signature.params]
try: try:
@@ -281,7 +281,7 @@ def show_call_signatures(signatures=()):
# Need to decode it with utf8, because vim returns always a python 2 # Need to decode it with utf8, because vim returns always a python 2
# string even if it is unicode. # string even if it is unicode.
e = vim.eval('g:jedi#call_signature_escape') e = vim_eval('g:jedi#call_signature_escape')
if hasattr(e, 'decode'): if hasattr(e, 'decode'):
e = e.decode('UTF-8') e = e.decode('UTF-8')
# replace line before with cursor # replace line before with cursor
@@ -303,7 +303,7 @@ def show_call_signatures(signatures=()):
tup = '%s, %s' % (len(add), replace) tup = '%s, %s' % (len(add), replace)
repl = prefix + (regex % (tup, text)) + add + line[end_column:] repl = prefix + (regex % (tup, text)) + add + line[end_column:]
vim.eval('setline(%s, %s)' % (line_to_replace, repr(PythonToVimStr(repl)))) vim_eval('setline(%s, %s)' % (line_to_replace, repr(PythonToVimStr(repl))))
@catch_and_print_exceptions @catch_and_print_exceptions
@@ -311,24 +311,24 @@ def rename():
if not int(vim.eval('a:0')): if not int(vim.eval('a:0')):
_rename_cursor = vim.current.window.cursor _rename_cursor = vim.current.window.cursor
vim.command('normal A ') # otherwise startinsert doesn't work well vim_command('normal A ') # otherwise startinsert doesn't work well
vim.current.window.cursor = _rename_cursor vim.current.window.cursor = _rename_cursor
vim.command('augroup jedi_rename') vim_command('augroup jedi_rename')
vim.command('autocmd InsertLeave <buffer> call jedi#rename(1)') vim_command('autocmd InsertLeave <buffer> call jedi#rename(1)')
vim.command('augroup END') vim_command('augroup END')
vim.command('normal! diw') vim_command('normal! diw')
vim.command(':startinsert') vim_command(':startinsert')
else: else:
window_path = vim.current.buffer.name window_path = vim.current.buffer.name
# reset autocommand # reset autocommand
vim.command('autocmd! jedi_rename InsertLeave') vim_command('autocmd! jedi_rename InsertLeave')
replace = vim.eval("expand('<cword>')") replace = vim_eval("expand('<cword>')")
vim.command('normal! u') # undo new word vim_command('normal! u') # undo new word
cursor = vim.current.window.cursor cursor = vim.current.window.cursor
vim.command('normal! u') # undo the space at the end vim_command('normal! u') # undo the space at the end
vim.current.window.cursor = cursor vim.current.window.cursor = cursor
if replace is None: if replace is None:
@@ -349,7 +349,7 @@ def rename():
return return
vim.current.window.cursor = r.start_pos vim.current.window.cursor = r.start_pos
vim.command('normal! cw%s' % replace) vim_command('normal! cw%s' % replace)
result = new_buffer(window_path) result = new_buffer(window_path)
if not result: if not result:
@@ -395,7 +395,7 @@ def py_import_completions():
@catch_and_print_exceptions @catch_and_print_exceptions
def new_buffer(path, options=''): def new_buffer(path, options=''):
# options are what you can to edit the edit options # options are what you can to edit the edit options
if vim.eval('g:jedi#use_tabs_not_buffers') == '1': if vim_eval('g:jedi#use_tabs_not_buffers') == '1':
_tabnew(path, options) _tabnew(path, options)
else: else:
if vim_eval("!&hidden && &modified") == '1': if vim_eval("!&hidden && &modified") == '1':
@@ -403,13 +403,13 @@ def new_buffer(path, options=''):
echo_highlight('Cannot open a new buffer, use `:set hidden` or save your buffer') echo_highlight('Cannot open a new buffer, use `:set hidden` or save your buffer')
return False return False
else: else:
vim.command('w') vim_command('w')
vim_command('edit %s %s' % (options, escape_file_path(path))) vim_command('edit %s %s' % (options, escape_file_path(path)))
# sometimes syntax is being disabled and the filetype not set. # sometimes syntax is being disabled and the filetype not set.
if vim.eval('!exists("g:syntax_on")') == '1': if vim_eval('!exists("g:syntax_on")') == '1':
vim.command('syntax enable') vim_command('syntax enable')
if vim.eval("&filetype != 'python'") == '1': if vim_eval("&filetype != 'python'") == '1':
vim.command('set filetype=python') vim_command('set filetype=python')
return True return True
@@ -421,12 +421,12 @@ def _tabnew(path, options=''):
:param options: `:tabnew` options, read vim help. :param options: `:tabnew` options, read vim help.
""" """
path = os.path.abspath(path) path = os.path.abspath(path)
if vim.eval('has("gui")') == '1': if vim_eval('has("gui")') == '1':
vim.command('tab drop %s %s' % (options, escape_file_path(path))) vim_command('tab drop %s %s' % (options, escape_file_path(path)))
return return
for tab_nr in range(int(vim.eval("tabpagenr('$')"))): for tab_nr in range(int(vim_eval("tabpagenr('$')"))):
for buf_nr in vim.eval("tabpagebuflist(%i + 1)" % tab_nr): for buf_nr in vim_eval("tabpagebuflist(%i + 1)" % tab_nr):
buf_nr = int(buf_nr) - 1 buf_nr = int(buf_nr) - 1
try: try:
buf_path = vim.buffers[buf_nr].name buf_path = vim.buffers[buf_nr].name
@@ -437,14 +437,14 @@ def _tabnew(path, options=''):
else: else:
if buf_path == path: if buf_path == path:
# tab exists, just switch to that tab # tab exists, just switch to that tab
vim.command('tabfirst | tabnext %i' % (tab_nr + 1)) vim_command('tabfirst | tabnext %i' % (tab_nr + 1))
break break
else: else:
continue continue
break break
else: else:
# tab doesn't exist, add a new one. # tab doesn't exist, add a new one.
vim.command('tabnew %s' % escape_file_path(path)) vim_command('tabnew %s' % escape_file_path(path))
def escape_file_path(path): def escape_file_path(path):

View File

@@ -45,6 +45,7 @@ describe 'goto_simple'
end end
end end
describe 'goto_with_tabs' describe 'goto_with_tabs'
before before
set filetype=python set filetype=python
@@ -72,6 +73,7 @@ describe 'goto_with_tabs'
end end
end end
describe 'goto_with_buffers' describe 'goto_with_buffers'
before before
set filetype=python set filetype=python