Improve use_tag_stack-hack: use context managers, short 'tags'

Use a NamedTemporaryFile context manager, which removes the temporary
file again, and use it as the only entry for the 'tags' setting to
improve lookup performance.
This commit is contained in:
Daniel Hahler
2015-10-18 22:10:36 +02:00
parent 005cb5225f
commit 267df8b3da

View File

@@ -8,9 +8,8 @@ import traceback # for exception output
import re import re
import os import os
import sys import sys
import string
import random
from shlex import split as shsplit from shlex import split as shsplit
from tempfile import NamedTemporaryFile
try: try:
from itertools import zip_longest from itertools import zip_longest
except ImportError: except ImportError:
@@ -254,18 +253,17 @@ def goto(mode="goto", no_output=False):
if not result: if not result:
return [] return []
if d.module_path and vim_eval('g:jedi#use_tag_stack') == '1': if d.module_path and vim_eval('g:jedi#use_tag_stack') == '1':
with open(vim_eval('tempname()'), 'w') as f: with NamedTemporaryFile('w', prefix=vim_eval('tempname()')) as f:
tagname = d.name tagname = d.name
while vim_eval('taglist("^%s$")' % tagname) != []:
tagname = d.name + ' ' + ''.join(
[random.choice(string.ascii_lowercase)
for _ in range(4)])
f.write('{0}\t{1}\t{2}'.format(tagname, d.module_path, f.write('{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)))
f.seek(0) f.flush()
vim.command('set tags+=%s' % f.name) old_tags = vim.eval('&tags')
vim.command('tjump %s' % tagname) try:
vim.command('set tags-=%s' % f.name) vim.command('set tags=%s' % f.name)
vim.command('tjump %s' % tagname)
finally:
vim.command('set tags=%s' % old_tags)
vim.current.window.cursor = d.line, d.column vim.current.window.cursor = d.line, d.column
else: else:
# multiple solutions # multiple solutions