Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to use quickfix for references. #4033

Merged
merged 2 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 56 additions & 13 deletions autoload/ale/references.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ function! ale#references#ClearLSPData() abort
let s:references_map = {}
endfunction

function! ale#references#FormatTSResponseItem(response_item, options) abort
if get(a:options, 'open_in') is# 'quickfix'
return {
\ 'filename': a:response_item.file,
\ 'lnum': a:response_item.start.line,
\ 'col': a:response_item.start.offset,
\}
else
return {
\ 'filename': a:response_item.file,
\ 'line': a:response_item.start.line,
\ 'column': a:response_item.start.offset,
\ 'match': substitute(a:response_item.lineText, '^\s*\(.\{-}\)\s*$', '\1', ''),
\}
endif
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)
Expand All @@ -25,23 +42,43 @@ function! ale#references#HandleTSServerResponse(conn_id, response) abort
let l:item_list = []

for l:response_item in a:response.body.refs
call add(l:item_list, {
\ 'filename': l:response_item.file,
\ 'line': l:response_item.start.line,
\ 'column': l:response_item.start.offset,
\ 'match': substitute(l:response_item.lineText, '^\s*\(.\{-}\)\s*$', '\1', ''),
\})
call add(
\ l:item_list,
\ ale#references#FormatTSResponseItem(l:response_item, l:options)
\)
endfor

if empty(l:item_list)
call ale#util#Execute('echom ''No references found.''')
else
call ale#preview#ShowSelection(l:item_list, l:options)
if get(l:options, 'open_in') is# 'quickfix'
call setqflist([], 'r')
call setqflist(l:item_list, 'a')
call ale#util#Execute('cc 1')
else
call ale#preview#ShowSelection(l:item_list, l:options)
endif
endif
endif
endif
endfunction

function! ale#references#FormatLSPResponseItem(response_item, options) abort
if get(a:options, 'open_in') is# 'quickfix'
return {
\ 'filename': ale#path#FromURI(a:response_item.uri),
\ 'lnum': a:response_item.range.start.line + 1,
\ 'col': a:response_item.range.start.character + 1,
\}
else
return {
\ 'filename': ale#path#FromURI(a:response_item.uri),
\ 'line': a:response_item.range.start.line + 1,
\ 'column': a:response_item.range.start.character + 1,
\}
endif
endfunction

function! ale#references#HandleLSPResponse(conn_id, response) abort
if has_key(a:response, 'id')
\&& has_key(s:references_map, a:response.id)
Expand All @@ -53,18 +90,22 @@ function! ale#references#HandleLSPResponse(conn_id, response) abort

if type(l:result) is v:t_list
for l:response_item in l:result
call add(l:item_list, {
\ 'filename': ale#path#FromURI(l:response_item.uri),
\ 'line': l:response_item.range.start.line + 1,
\ 'column': l:response_item.range.start.character + 1,
\})
call add(l:item_list,
\ ale#references#FormatLSPResponseItem(l:response_item, l:options)
\)
endfor
endif

if empty(l:item_list)
call ale#util#Execute('echom ''No references found.''')
else
call ale#preview#ShowSelection(l:item_list, l:options)
if get(l:options, 'open_in') is# 'quickfix'
call setqflist([], 'r')
call setqflist(l:item_list, 'a')
call ale#util#Execute('cc 1')
else
call ale#preview#ShowSelection(l:item_list, l:options)
endif
endif
endif
endfunction
Expand Down Expand Up @@ -119,6 +160,8 @@ function! ale#references#Find(...) abort
let l:options.open_in = 'split'
elseif l:option is? '-vsplit'
let l:options.open_in = 'vsplit'
elseif l:option is? '-quickfix'
let l:options.open_in = 'quickfix'
endif
endfor
endif
Expand Down
88 changes: 88 additions & 0 deletions test/test_find_references.vader
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,57 @@ Execute(Results should be shown for tsserver responses):
\ },
\ g:options

Execute(Results should be put to quickfix for tsserver responses):
call ale#references#SetMap(
\ {
\ 3: {
\ 'ignorethis': 'x',
\ 'open_in': 'quickfix',
\ }
\ }
\)
call ale#references#HandleTSServerResponse(1, {
\ 'command': 'references',
\ 'request_seq': 3,
\ 'success': v:true,
\ 'body': {
\ 'symbolStartOffset': 9,
\ 'refs': [
\ {
\ 'file': '/foo/bar/app.ts',
\ 'isWriteAccess': v:true,
\ 'lineText': 'import {doSomething} from ''./whatever''',
\ 'end': {'offset': 24, 'line': 9},
\ 'start': {'offset': 9, 'line': 9},
\ 'isDefinition': v:true,
\ },
\ {
\ 'file': '/foo/bar/app.ts',
\ 'isWriteAccess': v:false,
\ 'lineText': ' doSomething()',
\ 'end': {'offset': 18, 'line': 804},
\ 'start': {'offset': 3, 'line': 804},
\ 'isDefinition': v:false,
\ },
\ {
\ 'file': '/foo/bar/other/app.ts',
\ 'isWriteAccess': v:false,
\ 'lineText': ' doSomething()',
\ 'end': {'offset': 18, 'line': 51},
\ 'start': {'offset': 3, 'line': 51},
\ 'isDefinition': v:false,
\ },
\ ],
\ 'symbolDisplayString': 'import doSomething',
\ 'symbolName': 'doSomething()',
\ },
\})

AssertEqual
\ 3,
\ len(getqflist())
AssertEqual {}, ale#references#GetMap()

Execute(The preview window should not be opened for empty tsserver responses):
call ale#references#SetMap({3: {}})
call ale#references#HandleTSServerResponse(1, {
Expand Down Expand Up @@ -283,6 +334,16 @@ Execute(`-vsplit` should display results in vsplits):

AssertEqual {'42': {'open_in': 'vsplit', 'use_relative_paths': 0}}, ale#references#GetMap()

Execute(`-quickfix` should display results in quickfix):
runtime ale_linters/typescript/tsserver.vim
call setpos('.', [bufnr(''), 2, 5, 0])

ALEFindReferences -quickfix

call g:InitCallback()

AssertEqual {'42': {'open_in': 'quickfix', 'use_relative_paths': 0}}, ale#references#GetMap()

Given python(Some Python file):
foo
somelongerline
Expand Down Expand Up @@ -327,6 +388,33 @@ Execute(LSP reference responses should be handled):
\ g:item_list
AssertEqual {}, ale#references#GetMap()

Execute(LSP reference responses should be put to quickfix):
call ale#references#SetMap({3: { 'open_in': 'quickfix' }})
call ale#references#HandleLSPResponse(
\ 1,
\ {
\ 'id': 3,
\ 'result': [
\ {
\ 'uri': ale#path#ToURI(ale#path#Simplify(g:dir . '/completion_dummy_file')),
\ 'range': {
\ 'start': {'line': 2, 'character': 7},
\ },
\ },
\ {
\ 'uri': ale#path#ToURI(ale#path#Simplify(g:dir . '/other_file')),
\ 'range': {
\ 'start': {'line': 7, 'character': 15},
\ },
\ },
\ ],
\ }
\)

AssertEqual
\ 2,
\ len(getqflist())

Execute(Preview windows should not be opened for empty LSP reference responses):
call ale#references#SetMap({3: {}})
call ale#references#HandleLSPResponse(1, {'id': 3, 'result': []})
Expand Down