Skip to content

Commit

Permalink
Add support for findReferences
Browse files Browse the repository at this point in the history
Call textDocument/references and put the result in the quickfix list,
then open it. Only supports the file and position.
  • Loading branch information
natebosch committed May 14, 2017
1 parent 97928fa commit 6de26c0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Bug fix: Newlines in diagnostics are replace with '\n' to avoid multiline
messages
- Add support for `textDocument/references` request. References are shown in
quickfix list.

# 0.1.2

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ moves again the options will be provided using vim's built in completion.
While the cursor is on any identifier call `lsc#reference#goToDefinition()` (see
above for recommended mapping) to jump to the location of the definition. If the
cursor moves before the server responds the response will be ignored.

## Find references

While the cursor is on any identifier call `lsc#reference#findReferences()` to
populate the quickfix list with usage locations.
43 changes: 40 additions & 3 deletions autoload/lsc/reference.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,48 @@ function! lsc#reference#goToDefinition() abort
let character = location.range.start.character + 1
call s:goTo(file, line, character)
endfunction
let params = { 'textDocument': {'uri': lsc#util#documentUri()},
call lsc#server#call(&filetype, 'textDocument/definition',
\ s:TextDocumentPositionParams(), data.trigger)
endfunction

function! s:TextDocumentPositionParams() abort
return { 'textDocument': {'uri': lsc#util#documentUri()},
\ 'position': {'line': line('.') - 1, 'character': col('.') - 1}
\ }
call lsc#server#call(&filetype, 'textDocument/definition',
\ params, data.trigger)
endfunction

function! lsc#reference#findReferences() abort
call lsc#file#flushChanges()
let params = s:TextDocumentPositionParams()
let params.context = {'includeDeclaration': v:true}
call lsc#server#call(&filetype, 'textDocument/references',
\ params, function('<SID>setQuickFixReferences'))
endfunction

function! s:setQuickFixReferences(results) abort
call setqflist(map(a:results, 's:quickFixItem(v:val)'))
copen
endfunction

" Convert an LSP Location to a item suitable for the vim quickfix list.
"
" Both representations are dictionaries.
"
" Location:
" 'uri': file:// URI
" 'range': {'start': {'line', 'character'}, 'end': {'line', 'character'}}
"
" QuickFix Item: (as used)
" 'filename': file path
" 'lnum': line number
" 'col': column number
"
" LSP line and column are zero-based, vim is one-based.
function! s:quickFixItem(location) abort
return {'filename': lsc#util#documentPath(a:location.uri),
\ 'lnum': a:location.range.start.line + 1,
\ 'col': a:location.range.start.character + 1
\}
endfunction

if !exists('s:initialized')
Expand Down

0 comments on commit 6de26c0

Please sign in to comment.