Skip to content

Commit

Permalink
Sort references in quickfix list
Browse files Browse the repository at this point in the history
- Always set filename instead of bufnr for easier sorting
- Add CompareQuickFixItems method to sort by filename, then by line when
  filenames are equal
  • Loading branch information
natebosch committed May 14, 2017
1 parent 47790c4 commit 2e42c9b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions autoload/lsc/reference.vim
Expand Up @@ -41,7 +41,9 @@ function! lsc#reference#findReferences() abort
endfunction

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

Expand All @@ -61,21 +63,28 @@ endfunction
" 'text': The content of the referenced line
"
" LSP line and column are zero-based, vim is one-based.
function! s:quickFixItem(location) abort
function! s:QuickFixItem(location) abort
let item = {'lnum': a:location.range.start.line + 1,
\ 'col': a:location.range.start.character + 1}
let file_path = lsc#util#documentPath(a:location.uri)
let item.filename = file_path
let bufnr = bufnr(file_path)
if bufnr == -1
let item.filename = file_path
let item.text = readfile(file_path, '', item.lnum)[item.lnum - 1]
else
let item.bufnr = bufnr
let item.text = getbufline(bufnr, item.lnum)[0]
endif
return item
endfunction

function! s:CompareQuickFixItems(i1, i2) abort
if a:i1.filename == a:i2.filename
return a:i1.lnum - a:i2.lnum
else
return a:i1.filename > a:i2.filename ? 1 : -1
endif
endfunction

if !exists('s:initialized')
let s:goto_definition_id = 1
let s:initialized = v:true
Expand Down

0 comments on commit 2e42c9b

Please sign in to comment.