Fix handling of temporary tags file on Windows

Fixes https://github.com/davidhalter/jedi-vim/issues/482.
Closes https://github.com/davidhalter/jedi-vim/pull/489.
This commit is contained in:
Jacob Niehus
2015-10-30 11:14:49 -07:00
committed by Daniel Hahler
parent 84f5cd92c7
commit 2cf1d42fff

View File

@@ -9,7 +9,7 @@ import re
import os import os
import sys import sys
from shlex import split as shsplit from shlex import split as shsplit
from tempfile import NamedTemporaryFile from contextlib import contextmanager
try: try:
from itertools import zip_longest from itertools import zip_longest
except ImportError: except ImportError:
@@ -205,6 +205,17 @@ def completions():
vim.command('return ' + strout) vim.command('return ' + strout)
@contextmanager
def tempfile(content):
# Using this instead of the tempfile module because Windows won't read
# from a file not yet written to disk
with open(vim_eval('tempname()'), 'w') as f:
f.write(content)
try:
yield f
finally:
os.unlink(f.name)
@_check_jedi_availability(show_error=True) @_check_jedi_availability(show_error=True)
@catch_and_print_exceptions @catch_and_print_exceptions
def goto(mode="goto", no_output=False): def goto(mode="goto", no_output=False):
@@ -255,21 +266,22 @@ def goto(mode="goto", no_output=False):
if not result: if not result:
return [] return []
if d.module_path and using_tagstack: if d.module_path and using_tagstack:
with NamedTemporaryFile('w', prefix=vim_eval('tempname()')) as f:
tagname = d.name tagname = d.name
f.write('{0}\t{1}\t{2}'.format(tagname, d.module_path, with tempfile('{0}\t{1}\t{2}'.format(tagname, d.module_path,
'call cursor({0}, {1})'.format(d.line, d.column + 1))) 'call cursor({0}, {1})'.format(d.line, d.column + 1))) as f:
f.flush()
old_tags = vim.eval('&tags') old_tags = vim.eval('&tags')
old_wildignore = vim.eval('&wildignore') old_wildignore = vim.eval('&wildignore')
try: try:
# Clear wildignore to ensure tag file isn't ignored # Clear wildignore to ensure tag file isn't ignored
vim.command('set wildignore=') vim.command('set wildignore=')
vim.command('set tags=%s' % f.name) vim.command('let &tags = %s' %
repr(PythonToVimStr(f.name)))
vim.command('tjump %s' % tagname) vim.command('tjump %s' % tagname)
finally: finally:
vim.command('set tags=%s' % old_tags) vim.command('let &tags = %s' %
vim.command('let &wildignore = "%s"' % old_wildignore) repr(PythonToVimStr(old_tags)))
vim.command('let &wildignore = %s' %
repr(PythonToVimStr(old_wildignore)))
vim.current.window.cursor = d.line, d.column vim.current.window.cursor = d.line, d.column
else: else:
# multiple solutions # multiple solutions