Use relative paths when previewing file locations (#2238)

* Use relative paths when previewing file locations

Example: ALEFindReferences -relative
This commit is contained in:
Alvin Chan 2019-01-27 02:18:20 -08:00 committed by w0rp
parent b8bf7b220d
commit 6288c8b08e
4 changed files with 59 additions and 16 deletions

View file

@ -41,16 +41,23 @@ endfunction
" Show a location selection preview window, given some items.
" Each item should have 'filename', 'line', and 'column' keys.
function! ale#preview#ShowSelection(item_list) abort
function! ale#preview#ShowSelection(item_list, ...) abort
let l:options = get(a:000, 0, {})
let l:sep = has('win32') ? '\' : '/'
let l:lines = []
" Create lines to display to users.
for l:item in a:item_list
let l:match = get(l:item, 'match', '')
let l:filename = l:item.filename
if get(l:options, 'use_relative_paths')
let l:filename = substitute(l:item.filename, '^' . getcwd() . l:sep, '', '') " no-custom-checks
endif
call add(
\ l:lines,
\ l:item.filename
\ l:filename
\ . ':' . l:item.line
\ . ':' . l:item.column
\ . (!empty(l:match) ? ' ' . l:match : ''),

View file

@ -17,7 +17,7 @@ endfunction
function! ale#references#HandleTSServerResponse(conn_id, response) abort
if get(a:response, 'command', '') is# 'references'
\&& has_key(s:references_map, a:response.request_seq)
call remove(s:references_map, a:response.request_seq)
let l:options = remove(s:references_map, a:response.request_seq)
if get(a:response, 'success', v:false) is v:true
let l:item_list = []
@ -34,7 +34,7 @@ function! ale#references#HandleTSServerResponse(conn_id, response) abort
if empty(l:item_list)
call ale#util#Execute('echom ''No references found.''')
else
call ale#preview#ShowSelection(l:item_list)
call ale#preview#ShowSelection(l:item_list, l:options)
endif
endif
endif
@ -43,7 +43,7 @@ endfunction
function! ale#references#HandleLSPResponse(conn_id, response) abort
if has_key(a:response, 'id')
\&& has_key(s:references_map, a:response.id)
call remove(s:references_map, a:response.id)
let l:options = remove(s:references_map, a:response.id)
" The result can be a Dictionary item, a List of the same, or null.
let l:result = get(a:response, 'result', [])
@ -60,12 +60,12 @@ function! ale#references#HandleLSPResponse(conn_id, response) abort
if empty(l:item_list)
call ale#util#Execute('echom ''No references found.''')
else
call ale#preview#ShowSelection(l:item_list)
call ale#preview#ShowSelection(l:item_list, l:options)
endif
endif
endfunction
function! s:OnReady(linter, lsp_details, line, column, ...) abort
function! s:OnReady(linter, lsp_details, line, column, options, ...) abort
let l:buffer = a:lsp_details.buffer
let l:id = a:lsp_details.connection_id
@ -91,10 +91,12 @@ function! s:OnReady(linter, lsp_details, line, column, ...) abort
let l:request_id = ale#lsp#Send(l:id, l:message)
let s:references_map[l:request_id] = {}
let s:references_map[l:request_id] = {
\ 'use_relative_paths': has_key(a:options, 'use_relative_paths') ? a:options.use_relative_paths : 0
\}
endfunction
function! s:FindReferences(linter) abort
function! s:FindReferences(linter, options) abort
let l:buffer = bufnr('')
let [l:line, l:column] = getcurpos()[1:2]
@ -111,14 +113,24 @@ function! s:FindReferences(linter) abort
let l:id = l:lsp_details.connection_id
call ale#lsp#WaitForCapability(l:id, 'references', function('s:OnReady', [
\ a:linter, l:lsp_details, l:line, l:column
\ a:linter, l:lsp_details, l:line, l:column, a:options
\]))
endfunction
function! ale#references#Find() abort
function! ale#references#Find(...) abort
let l:options = {}
if len(a:000) > 0
for l:option in a:000
if l:option is? '-relative'
let l:options.use_relative_paths = 1
endif
endfor
endif
for l:linter in ale#linter#Get(&filetype)
if !empty(l:linter.lsp)
call s:FindReferences(l:linter)
call s:FindReferences(l:linter, l:options)
endif
endfor
endfunction

View file

@ -202,7 +202,7 @@ command! -bar ALEGoToTypeDefinitionInSplit :call ale#definition#GoToType({'open_
command! -bar ALEGoToTypeDefinitionInVSplit :call ale#definition#GoToType({'open_in': 'vertical-split'})
" Find references for tsserver and LSP
command! -bar ALEFindReferences :call ale#references#Find()
command! -bar -nargs=* ALEFindReferences :call ale#references#Find(<f-args>)
" Show summary information for the cursor.
command! -bar ALEHover :call ale#hover#ShowAtCursor()

View file

@ -8,6 +8,7 @@ Before:
let g:message_list = []
let g:preview_called = 0
let g:item_list = []
let g:options = {}
let g:capability_checked = ''
let g:conn_id = v:null
let g:WaitCallback = v:null
@ -48,9 +49,10 @@ Before:
call add(g:expr_list, a:expr)
endfunction
function! ale#preview#ShowSelection(item_list) abort
function! ale#preview#ShowSelection(item_list, options) abort
let g:preview_called = 1
let g:item_list = a:item_list
let g:options = a:options
endfunction
After:
@ -70,6 +72,7 @@ After:
unlet! g:message_list
unlet! g:expr_list
unlet! b:ale_linters
unlet! g:options
unlet! g:item_list
unlet! g:preview_called
@ -180,7 +183,17 @@ Execute(tsserver reference requests should be sent):
AssertEqual
\ [[0, 'ts@references', {'file': expand('%:p'), 'line': 2, 'offset': 5}]],
\ g:message_list
AssertEqual {'42': {}}, ale#references#GetMap()
AssertEqual {'42': {'use_relative_paths': 0}}, ale#references#GetMap()
Execute('-relative' argument should enable 'use_relative_paths' in HandleTSServerResponse):
runtime ale_linters/typescript/tsserver.vim
call setpos('.', [bufnr(''), 2, 5, 0])
ALEFindReferences -relative
call call(g:WaitCallback, [g:conn_id, '/foo/bar'])
AssertEqual {'42': {'use_relative_paths': 1}}, ale#references#GetMap()
Given python(Some Python file):
foo
@ -276,4 +289,15 @@ Execute(LSP reference requests should be sent):
\ ],
\ g:message_list
AssertEqual {'42': {}}, ale#references#GetMap()
AssertEqual {'42': {'use_relative_paths': 0}}, ale#references#GetMap()
Execute('-relative' argument should enable 'use_relative_paths' in HandleLSPResponse):
runtime ale_linters/python/pyls.vim
let b:ale_linters = ['pyls']
call setpos('.', [bufnr(''), 1, 5, 0])
ALEFindReferences -relative
call call(g:WaitCallback, [g:conn_id, '/foo/bar'])
AssertEqual {'42': {'use_relative_paths': 1}}, ale#references#GetMap()