Highlight usages

Use matchaddpos to highlight usages in the current file.
This commit is contained in:
Dave Halter
2017-10-15 10:47:01 +02:00
parent 2bb7677b43
commit 84b8eeb954
3 changed files with 51 additions and 19 deletions

View File

@@ -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
endif
endif
hi jediUsage term=NONE cterm=NONE ctermfg=None guifg=None gui=NONE ctermbg=3 guibg=Yellow

View File

@@ -270,7 +270,16 @@ function! jedi#goto_definitions() abort
endfunction
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
function! jedi#rename(...) abort

View File

@@ -286,15 +286,13 @@ def tempfile(content):
@catch_and_print_exceptions
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
:rtype: list
"""
script = get_script()
if mode == "goto":
definitions = script.goto_assignments(follow_imports=True)
elif mode == "related_name":
definitions = script.usages()
elif mode == "definition":
definitions = script.goto_definitions()
elif mode == "assignment":
@@ -340,24 +338,47 @@ def goto(mode="goto", no_output=False):
repr(PythonToVimStr(old_wildignore)))
vim.current.window.cursor = d.line, d.column
else:
# multiple solutions
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)) + ')')
show_goto_multi_results()
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)
@catch_and_print_exceptions
def show_documentation():