mirror of
https://github.com/davidhalter/jedi-vim.git
synced 2025-12-08 19:44:52 +08:00
Use the new API instead of the deprecated one
This commit is contained in:
@@ -255,7 +255,7 @@ def get_known_environments():
|
|||||||
|
|
||||||
|
|
||||||
@catch_and_print_exceptions
|
@catch_and_print_exceptions
|
||||||
def get_script(source=None, column=None):
|
def get_script(source=None):
|
||||||
jedi.settings.additional_dynamic_modules = [
|
jedi.settings.additional_dynamic_modules = [
|
||||||
b.name for b in vim.buffers if (
|
b.name for b in vim.buffers if (
|
||||||
b.name is not None and
|
b.name is not None and
|
||||||
@@ -263,16 +263,16 @@ def get_script(source=None, column=None):
|
|||||||
b.options['buflisted'])]
|
b.options['buflisted'])]
|
||||||
if source is None:
|
if source is None:
|
||||||
source = '\n'.join(vim.current.buffer)
|
source = '\n'.join(vim.current.buffer)
|
||||||
|
buf_path = vim.current.buffer.name
|
||||||
|
|
||||||
|
return jedi.Script(source, path=buf_path, environment=get_environment())
|
||||||
|
|
||||||
|
|
||||||
|
def get_pos(column=None):
|
||||||
row = vim.current.window.cursor[0]
|
row = vim.current.window.cursor[0]
|
||||||
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
|
return row, column
|
||||||
|
|
||||||
return jedi.Script(
|
|
||||||
source, row, column, buf_path,
|
|
||||||
encoding=vim_eval('&encoding') or 'latin1',
|
|
||||||
environment=get_environment(),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@_check_jedi_availability(show_error=False)
|
@_check_jedi_availability(show_error=False)
|
||||||
@@ -303,9 +303,9 @@ def completions():
|
|||||||
# here again hacks, because jedi has a different interface than vim
|
# here again hacks, because jedi has a different interface than vim
|
||||||
column += len(base)
|
column += len(base)
|
||||||
try:
|
try:
|
||||||
script = get_script(source=source, column=column)
|
script = get_script(source=source)
|
||||||
completions = script.completions()
|
completions = script.complete(*get_pos(column))
|
||||||
signatures = script.call_signatures()
|
signatures = script.get_signatures(*get_pos(column))
|
||||||
|
|
||||||
add_info = "preview" in vim.eval("&completeopt").split(",")
|
add_info = "preview" in vim.eval("&completeopt").split(",")
|
||||||
out = []
|
out = []
|
||||||
@@ -358,14 +358,15 @@ def goto(mode="goto"):
|
|||||||
:rtype: list
|
:rtype: list
|
||||||
"""
|
"""
|
||||||
script = get_script()
|
script = get_script()
|
||||||
|
pos = get_pos()
|
||||||
if mode == "goto":
|
if mode == "goto":
|
||||||
definitions = script.goto_assignments(follow_imports=True)
|
definitions = script.goto(*pos, follow_imports=True)
|
||||||
elif mode == "definition":
|
elif mode == "definition":
|
||||||
definitions = script.goto_definitions()
|
definitions = script.infer(*pos)
|
||||||
elif mode == "assignment":
|
elif mode == "assignment":
|
||||||
definitions = script.goto_assignments()
|
definitions = script.goto(*pos)
|
||||||
elif mode == "stubs":
|
elif mode == "stubs":
|
||||||
definitions = script.goto_assignments(follow_imports=True, only_stubs=True)
|
definitions = script.goto(*pos, follow_imports=True, only_stubs=True)
|
||||||
|
|
||||||
if not definitions:
|
if not definitions:
|
||||||
echo_highlight("Couldn't find any definitions for this.")
|
echo_highlight("Couldn't find any definitions for this.")
|
||||||
@@ -376,19 +377,20 @@ def goto(mode="goto"):
|
|||||||
echo_highlight("Cannot get the definition of Python keywords.")
|
echo_highlight("Cannot get the definition of Python keywords.")
|
||||||
else:
|
else:
|
||||||
echo_highlight("Builtin modules cannot be displayed (%s)."
|
echo_highlight("Builtin modules cannot be displayed (%s)."
|
||||||
% d.desc_with_module)
|
% (d.full_name or d.name))
|
||||||
else:
|
else:
|
||||||
using_tagstack = int(vim_eval('g:jedi#use_tag_stack')) == 1
|
using_tagstack = int(vim_eval('g:jedi#use_tag_stack')) == 1
|
||||||
if (d.module_path or '') != vim.current.buffer.name:
|
module_path = str(d.module_path)
|
||||||
result = new_buffer(d.module_path,
|
if (module_path or '') != vim.current.buffer.name:
|
||||||
|
result = new_buffer(module_path,
|
||||||
using_tagstack=using_tagstack)
|
using_tagstack=using_tagstack)
|
||||||
if not result:
|
if not result:
|
||||||
return []
|
return []
|
||||||
if (using_tagstack and d.module_path and
|
if (using_tagstack and module_path and
|
||||||
os.path.exists(d.module_path)):
|
os.path.exists(module_path)):
|
||||||
tagname = d.name
|
tagname = d.name
|
||||||
with tempfile('{0}\t{1}\t{2}'.format(
|
with tempfile('{0}\t{1}\t{2}'.format(
|
||||||
tagname, d.module_path, 'call cursor({0}, {1})'.format(
|
tagname, module_path, 'call cursor({0}, {1})'.format(
|
||||||
d.line, d.column + 1))) as f:
|
d.line, d.column + 1))) as f:
|
||||||
old_tags = vim.eval('&tags')
|
old_tags = vim.eval('&tags')
|
||||||
old_wildignore = vim.eval('&wildignore')
|
old_wildignore = vim.eval('&wildignore')
|
||||||
@@ -445,7 +447,7 @@ def show_goto_multi_results(definitions, mode):
|
|||||||
lst.append(dict(text=PythonToVimStr(d.description)))
|
lst.append(dict(text=PythonToVimStr(d.description)))
|
||||||
else:
|
else:
|
||||||
text = annotate_description(d)
|
text = annotate_description(d)
|
||||||
lst.append(dict(filename=PythonToVimStr(relpath(d.module_path)),
|
lst.append(dict(filename=PythonToVimStr(relpath(str(d.module_path))),
|
||||||
lnum=d.line, col=d.column + 1,
|
lnum=d.line, col=d.column + 1,
|
||||||
text=PythonToVimStr(text)))
|
text=PythonToVimStr(text)))
|
||||||
|
|
||||||
@@ -497,7 +499,7 @@ def _same_definitions(a, b):
|
|||||||
@catch_and_print_exceptions
|
@catch_and_print_exceptions
|
||||||
def usages(visuals=True):
|
def usages(visuals=True):
|
||||||
script = get_script()
|
script = get_script()
|
||||||
definitions = script.usages()
|
definitions = script.get_references(*get_pos())
|
||||||
if not definitions:
|
if not definitions:
|
||||||
echo_highlight("No usages found here.")
|
echo_highlight("No usages found here.")
|
||||||
return definitions
|
return definitions
|
||||||
@@ -590,7 +592,7 @@ def highlight_usages():
|
|||||||
defs_per_buf = {}
|
defs_per_buf = {}
|
||||||
for definition in definitions:
|
for definition in definitions:
|
||||||
try:
|
try:
|
||||||
buf = bufs[definition.module_path]
|
buf = bufs[str(definition.module_path)]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
defs_per_buf.setdefault(buf, []).append(definition)
|
defs_per_buf.setdefault(buf, []).append(definition)
|
||||||
@@ -693,7 +695,7 @@ def highlight_usages_for_vim_win():
|
|||||||
if definitions:
|
if definitions:
|
||||||
buffer_path = vim.current.buffer.name
|
buffer_path = vim.current.buffer.name
|
||||||
for definition in definitions:
|
for definition in definitions:
|
||||||
if (definition.module_path or '') == buffer_path:
|
if (str(definition.module_path) or '') == buffer_path:
|
||||||
positions = [
|
positions = [
|
||||||
[definition.line,
|
[definition.line,
|
||||||
definition.column + 1,
|
definition.column + 1,
|
||||||
@@ -719,7 +721,7 @@ def highlight_usages_for_vim_win():
|
|||||||
def show_documentation():
|
def show_documentation():
|
||||||
script = get_script()
|
script = get_script()
|
||||||
try:
|
try:
|
||||||
definitions = script.goto_definitions()
|
definitions = script.help(*get_pos())
|
||||||
except Exception:
|
except Exception:
|
||||||
# print to stdout, will be in :messages
|
# print to stdout, will be in :messages
|
||||||
definitions = []
|
definitions = []
|
||||||
@@ -735,7 +737,7 @@ def show_documentation():
|
|||||||
for d in definitions:
|
for d in definitions:
|
||||||
doc = d.docstring()
|
doc = d.docstring()
|
||||||
if doc:
|
if doc:
|
||||||
title = 'Docstring for %s' % d.desc_with_module
|
title = 'Docstring for %s' % (d.full_name or d.name)
|
||||||
underline = '=' * len(title)
|
underline = '=' * len(title)
|
||||||
docs.append('%s\n%s\n%s' % (title, underline, doc))
|
docs.append('%s\n%s\n%s' % (title, underline, doc))
|
||||||
else:
|
else:
|
||||||
@@ -783,7 +785,7 @@ def show_call_signatures(signatures=()):
|
|||||||
# buffer.
|
# buffer.
|
||||||
clear_call_signatures()
|
clear_call_signatures()
|
||||||
if signatures == ():
|
if signatures == ():
|
||||||
signatures = get_script().call_signatures()
|
signatures = get_script().get_signatures(*get_pos())
|
||||||
|
|
||||||
if not signatures:
|
if not signatures:
|
||||||
return
|
return
|
||||||
@@ -1006,18 +1008,19 @@ def do_rename(replace, orig=None):
|
|||||||
# Sort the whole thing reverse (positions at the end of the line
|
# Sort the whole thing reverse (positions at the end of the line
|
||||||
# must be first, because they move the stuff before the position).
|
# must be first, because they move the stuff before the position).
|
||||||
temp_rename = sorted(temp_rename, reverse=True,
|
temp_rename = sorted(temp_rename, reverse=True,
|
||||||
key=lambda x: (x.module_path, x.line, x.column))
|
key=lambda x: (str(x.module_path), x.line, x.column))
|
||||||
buffers = set()
|
buffers = set()
|
||||||
for r in temp_rename:
|
for r in temp_rename:
|
||||||
if r.in_builtin_module():
|
if r.in_builtin_module():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if os.path.abspath(vim.current.buffer.name) != r.module_path:
|
module_path = r.module_path
|
||||||
assert r.module_path is not None
|
if os.path.abspath(vim.current.buffer.name) != str(module_path):
|
||||||
result = new_buffer(r.module_path)
|
assert module_path is not None
|
||||||
|
result = new_buffer(module_path)
|
||||||
if not result:
|
if not result:
|
||||||
echo_highlight('Failed to create buffer window for %s!' % (
|
echo_highlight('Failed to create buffer window for %s!' % (
|
||||||
r.module_path))
|
module_path))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
buffers.add(vim.current.buffer.name)
|
buffers.add(vim.current.buffer.name)
|
||||||
@@ -1055,7 +1058,7 @@ def py_import():
|
|||||||
echo_highlight('%s is a builtin module.' % import_path)
|
echo_highlight('%s is a builtin module.' % import_path)
|
||||||
else:
|
else:
|
||||||
cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args])
|
cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args])
|
||||||
new_buffer(completion.module_path, cmd_args)
|
new_buffer(str(completion.module_path), cmd_args)
|
||||||
|
|
||||||
|
|
||||||
@catch_and_print_exceptions
|
@catch_and_print_exceptions
|
||||||
@@ -1068,8 +1071,8 @@ def py_import_completions():
|
|||||||
comps = []
|
comps = []
|
||||||
else:
|
else:
|
||||||
text = 'import %s' % argl
|
text = 'import %s' % argl
|
||||||
script = jedi.Script(text, 1, len(text), '', environment=get_environment())
|
script = jedi.Script(text, path='', environment=get_environment())
|
||||||
comps = ['%s%s' % (argl, c.complete) for c in script.completions()]
|
comps = ['%s%s' % (argl, c.complete) for c in script.complete(1, len(text))]
|
||||||
vim.command("return '%s'" % '\n'.join(comps))
|
vim.command("return '%s'" % '\n'.join(comps))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user