mirror of
https://github.com/davidhalter/jedi-vim.git
synced 2025-12-07 19:24:36 +08:00
Highlight usages
Use matchaddpos to highlight usages in the current file.
This commit is contained in:
@@ -30,3 +30,5 @@ if g:jedi#show_call_signatures > 0 && has('conceal')
|
|||||||
hi def jediFat term=bold,underline cterm=bold,underline gui=bold,underline ctermbg=0 guibg=#555555
|
hi def jediFat term=bold,underline cterm=bold,underline gui=bold,underline ctermbg=0 guibg=#555555
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
hi jediUsage term=NONE cterm=NONE ctermfg=None guifg=None gui=NONE ctermbg=3 guibg=Yellow
|
||||||
|
|||||||
@@ -270,7 +270,16 @@ function! jedi#goto_definitions() abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! jedi#usages() abort
|
function! jedi#usages() abort
|
||||||
PythonJedi jedi_vim.goto(mode="related_name")
|
call jedi#remove_usages()
|
||||||
|
PythonJedi jedi_vim.usages()
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! jedi#remove_usages() abort
|
||||||
|
for match in getmatches()
|
||||||
|
if stridx(match['group'], 'jediUsage') == 0
|
||||||
|
call matchdelete(match['id'])
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! jedi#rename(...) abort
|
function! jedi#rename(...) abort
|
||||||
|
|||||||
@@ -286,15 +286,13 @@ def tempfile(content):
|
|||||||
@catch_and_print_exceptions
|
@catch_and_print_exceptions
|
||||||
def goto(mode="goto", no_output=False):
|
def goto(mode="goto", no_output=False):
|
||||||
"""
|
"""
|
||||||
:param str mode: "related_name", "definition", "assignment", "auto"
|
:param str mode: "definition", "assignment", "goto"
|
||||||
:return: list of definitions/assignments
|
:return: list of definitions/assignments
|
||||||
:rtype: list
|
:rtype: list
|
||||||
"""
|
"""
|
||||||
script = get_script()
|
script = get_script()
|
||||||
if mode == "goto":
|
if mode == "goto":
|
||||||
definitions = script.goto_assignments(follow_imports=True)
|
definitions = script.goto_assignments(follow_imports=True)
|
||||||
elif mode == "related_name":
|
|
||||||
definitions = script.usages()
|
|
||||||
elif mode == "definition":
|
elif mode == "definition":
|
||||||
definitions = script.goto_definitions()
|
definitions = script.goto_definitions()
|
||||||
elif mode == "assignment":
|
elif mode == "assignment":
|
||||||
@@ -340,24 +338,47 @@ def goto(mode="goto", no_output=False):
|
|||||||
repr(PythonToVimStr(old_wildignore)))
|
repr(PythonToVimStr(old_wildignore)))
|
||||||
vim.current.window.cursor = d.line, d.column
|
vim.current.window.cursor = d.line, d.column
|
||||||
else:
|
else:
|
||||||
# multiple solutions
|
show_goto_multi_results()
|
||||||
lst = []
|
|
||||||
for d in definitions:
|
|
||||||
if d.in_builtin_module():
|
|
||||||
lst.append(dict(text=PythonToVimStr('Builtin ' + d.description)))
|
|
||||||
elif d.module_path is None:
|
|
||||||
# Typically a namespace, in the future maybe other things as
|
|
||||||
# well.
|
|
||||||
lst.append(dict(text=PythonToVimStr(d.description)))
|
|
||||||
else:
|
|
||||||
lst.append(dict(filename=PythonToVimStr(d.module_path),
|
|
||||||
lnum=d.line, col=d.column + 1,
|
|
||||||
text=PythonToVimStr(d.description)))
|
|
||||||
vim_eval('setqflist(%s)' % repr(lst))
|
|
||||||
vim_eval('jedi#add_goto_window(' + str(len(lst)) + ')')
|
|
||||||
return definitions
|
return definitions
|
||||||
|
|
||||||
|
|
||||||
|
def show_goto_multi_results(definitions):
|
||||||
|
"""Create a quickfix list for multiple definitions."""
|
||||||
|
lst = []
|
||||||
|
for d in definitions:
|
||||||
|
if d.in_builtin_module():
|
||||||
|
lst.append(dict(text=PythonToVimStr('Builtin ' + d.description)))
|
||||||
|
elif d.module_path is None:
|
||||||
|
# Typically a namespace, in the future maybe other things as
|
||||||
|
# well.
|
||||||
|
lst.append(dict(text=PythonToVimStr(d.description)))
|
||||||
|
else:
|
||||||
|
text = '[%s] %s' % (d.description, d.get_line_code().strip())
|
||||||
|
lst.append(dict(filename=PythonToVimStr(d.module_path),
|
||||||
|
lnum=d.line, col=d.column + 1,
|
||||||
|
text=PythonToVimStr(text)))
|
||||||
|
vim_eval('setqflist(%s)' % repr(lst))
|
||||||
|
vim_eval('jedi#add_goto_window(' + str(len(lst)) + ')')
|
||||||
|
|
||||||
|
|
||||||
|
@catch_and_print_exceptions
|
||||||
|
def usages():
|
||||||
|
script = get_script()
|
||||||
|
definitions = script.usages()
|
||||||
|
if not definitions:
|
||||||
|
echo_highlight("No usages found here.")
|
||||||
|
return
|
||||||
|
for definition in definitions:
|
||||||
|
# Only color the current module/buffer.
|
||||||
|
if (definition.module_path or '') == vim.current.buffer.name:
|
||||||
|
# mathaddpos needs a list of positions where a position is a list
|
||||||
|
# of (line, column, length).
|
||||||
|
# The column starts with 1 and not 0.
|
||||||
|
positions = [[definition.line, definition.column + 1, len(definition.name)]]
|
||||||
|
vim_eval("matchaddpos('jediUsage', %s)" % repr(positions))
|
||||||
|
|
||||||
|
show_goto_multi_results(definitions)
|
||||||
|
|
||||||
@_check_jedi_availability(show_error=True)
|
@_check_jedi_availability(show_error=True)
|
||||||
@catch_and_print_exceptions
|
@catch_and_print_exceptions
|
||||||
def show_documentation():
|
def show_documentation():
|
||||||
|
|||||||
Reference in New Issue
Block a user