diff --git a/jedi_vim.py b/jedi_vim.py index 266ee357..fccc694a 100644 --- a/jedi_vim.py +++ b/jedi_vim.py @@ -241,7 +241,7 @@ def goto(mode="goto", no_output=False): % d.desc_with_module) else: if d.module_path != vim.current.buffer.name: - result = new_buffer(d.module_path) + result = edit_buffer(d.module_path) if not result: return [] vim.current.window.cursor = d.line, d.column @@ -543,8 +543,7 @@ def py_import(): if completion.in_builtin_module(): echo_highlight('%s is a builtin module.' % import_path) else: - cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args]) - new_buffer(completion.module_path, cmd_args) + edit_buffer(completion.module_path) @catch_and_print_exceptions @@ -563,10 +562,24 @@ def py_import_completions(): @catch_and_print_exceptions -def new_buffer(path, options=''): - # options are what you can to edit the edit options +def edit_buffer(path): + """Edit the given path in the current window.""" + if vim_eval("!&hidden && &modified") == '1': + if vim_eval("bufname('%')") is None: + echo_highlight('Cannot open a new buffer, use `:set hidden` or save your buffer') + return False + else: + vim_command('update') + vim_command('edit %s' % escape_file_path(path)) + fix_buffer_options() + return True + + +@catch_and_print_exceptions +def new_buffer(path): + """Edit the given path in a new window.""" if vim_eval('g:jedi#use_tabs_not_buffers') == '1': - _tabnew(path, options) + _tabnew(path) elif not vim_eval('g:jedi#use_splits_not_buffers') == '1': user_split_option = vim_eval('g:jedi#use_splits_not_buffers') split_options = { @@ -583,19 +596,18 @@ def new_buffer(path, options=''): else: vim_command(split_options[user_split_option] + " %s" % path) else: - if vim_eval("!&hidden && &modified") == '1': - if vim_eval("bufname('%')") is None: - echo_highlight('Cannot open a new buffer, use `:set hidden` or save your buffer') - return False - else: - vim_command('w') - vim_command('edit %s %s' % (options, escape_file_path(path))) + edit_buffer(path) + + fix_buffer_options() + return True + + +def fix_buffer_options(): # 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') - return True @catch_and_print_exceptions @@ -606,9 +618,6 @@ def _tabnew(path, options=''): :param options: `:tabnew` options, read vim help. """ path = os.path.abspath(path) - if vim_eval('has("gui")') == '1': - vim_command('tab drop %s %s' % (options, escape_file_path(path))) - return for tab_nr in range(int(vim_eval("tabpagenr('$')"))): for buf_nr in vim_eval("tabpagebuflist(%i + 1)" % tab_nr):