Skip to content

Commit

Permalink
Add :LSClientDocumentSymbol
Browse files Browse the repository at this point in the history
  • Loading branch information
natebosch committed Jan 25, 2018
1 parent e7f2252 commit c9955ea
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.2.10-dev

- Add `:LSClientDocumentSymbol` command to populate the quickfix list with
symbols in the current document.

# 0.2.9+1

- Fix error in calling function message hooks.
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ let g:lsc_auto_map = {
\ 'GoToDefinition': '<C-]>',
\ 'FindReferences': 'gr',
\ 'FindCodeActions': 'ga',
\ 'DocumentSymbol': 'gs',
\ 'ShowHover': 'K',
\ 'Completion': 'completefunc',
\}
Expand Down Expand Up @@ -115,6 +116,12 @@ cursor moves before the server responds the response will be ignored.
While the cursor is on any identifier call `LSClientFindReferences` (`gr` if
using the default mappings) to populate the quickfix list with usage locations.

### Document Symbols

In any enabled buffer call `LSClientDocumentSymbol` (`gs` if using the default
mappings) to populate the quickfix list with the locations of all symbols in
that document.

### Hover

While the cursor is on any identifier call `LSClientShowHover` (`K` if using the
Expand Down
2 changes: 2 additions & 0 deletions autoload/lsc/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let s:default_maps = {
\ 'FindCodeActions': 'ga',
\ 'Rename': 'gR',
\ 'ShowHover': 'K',
\ 'DocumentSymbol': 'gs',
\ 'Completion': 'completefunc',
\}

Expand All @@ -25,6 +26,7 @@ function! lsc#config#mapKeys() abort
for command in [
\ 'GoToDefinition',
\ 'FindReferences',
\ 'DocumentSymbol',
\ 'ShowHover',
\ 'FindCodeActions',
\]
Expand Down
14 changes: 14 additions & 0 deletions autoload/lsc/params.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function! lsc#params#textDocument(...) abort
if a:0 >= 1
let file_path = a:1
else
let file_path = expand('%:p')
endif
return {'textDocument': {'uri': lsc#uri#documentUri(file_path)}}
endfunction

function! lsc#params#documentPosition() abort
return { 'textDocument': {'uri': lsc#uri#documentUri()},
\ 'position': {'line': line('.') - 1, 'character': col('.') - 1}
\ }
endfunction
146 changes: 137 additions & 9 deletions autoload/lsc/reference.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function! lsc#reference#goToDefinition() abort
call lsc#file#flushChanges()
call lsc#server#userCall('textDocument/definition',
\ s:TextDocumentPositionParams(),
\ lsc#params#documentPosition(),
\ lsc#util#gateResult('GoToDefinition', function('<SID>GoToDefinition')))
endfunction

Expand All @@ -22,15 +22,9 @@ function! s:GoToDefinition(result) abort
call s:goTo(file, line, character)
endfunction

function! s:TextDocumentPositionParams() abort
return { 'textDocument': {'uri': lsc#uri#documentUri()},
\ 'position': {'line': line('.') - 1, 'character': col('.') - 1}
\ }
endfunction

function! lsc#reference#findReferences() abort
call lsc#file#flushChanges()
let params = s:TextDocumentPositionParams()
let params = lsc#params#documentPosition()
let params.context = {'includeDeclaration': v:true}
call lsc#server#userCall('textDocument/references', params,
\ function('<SID>setQuickFixReferences'))
Expand Down Expand Up @@ -92,7 +86,7 @@ endfunction

function! lsc#reference#hover() abort
call lsc#file#flushChanges()
let params = s:TextDocumentPositionParams()
let params = lsc#params#documentPosition()
call lsc#server#userCall('textDocument/hover', params,
\ function('<SID>showHover'))
endfunction
Expand All @@ -112,3 +106,137 @@ function! s:showHover(result) abort
let lines = split(contents, "\n")
call lsc#util#displayAsPreview(lines)
endfunction

" Request a list of symbols in the current document and populate the quickfix
" list.
function! lsc#reference#documentSymbols() abort
call lsc#file#flushChanges()
call lsc#server#userCall('textDocument/documentSymbol',
\ lsc#params#textDocument(),
\ function('<SID>setQuickFixSymbols'))
endfunction

function! s:setQuickFixSymbols(results) abort
if empty(a:results)
call lsc#message#show('No symbols found')
endif

let file_path = lsc#uri#documentPath(a:results[0].location.uri)
call map(a:results, {_, symbol -> s:QuickFixSymbol(bufnr(file_path), symbol)})
call sort(a:results, 'lsc#util#compareQuickFixItems')
call setqflist(a:results)
copen
endfunction

" Conver an LSP SymbolInformation to a quick fix item.
"
" Both representations are dictionaries.
"
" SymbolInformation:
" 'location':
" 'uri': file:// URI
" 'range': {'start': {'line', 'characater'}, 'end': {'line', 'character'}}
" 'name': The symbol's name
" 'kind': Integer kind
" 'containerName': The element this symbol is inside
"
" QuickFix Item: (as used)
" 'bufnr': This buffer
" 'lnum': line number
" 'col': column number
" 'text': "SymbolName" [kind] (in containerName)?
function! s:QuickFixSymbol(bufnr, symbol) abort
let item = {'lnum': a:symbol.location.range.start.line + 1,
\ 'col': a:symbol.location.range.start.character + 1,
\ 'bufnr': a:bufnr}
let text = '"'.a:symbol.name.'"'
if !empty(a:symbol.kind)
let text .= ' ['.s:SymbolKind(a:symbol.kind).']'
endif
if !empty(a:symbol.containerName)
let text .= ' in '.a:symbol.containerName
endif
let item.text = text
return item
endfunction

function! s:SymbolKind(kind) abort
if a:kind == 1
return 'File'
endif
if a:kind == 2
return 'Module'
endif
if a:kind == 3
return 'Namespace'
endif
if a:kind == 4
return 'Package'
endif
if a:kind == 5
return 'Class'
endif
if a:kind == 6
return 'Method'
endif
if a:kind == 7
return 'Property'
endif
if a:kind == 8
return 'Field'
endif
if a:kind == 9
return 'Constructor'
endif
if a:kind == 10
return 'Enum'
endif
if a:kind == 11
return 'Interface'
endif
if a:kind == 12
return 'Function'
endif
if a:kind == 13
return 'Variable'
endif
if a:kind == 14
return 'Constant'
endif
if a:kind == 15
return 'String'
endif
if a:kind == 16
return 'Number'
endif
if a:kind == 17
return 'Boolean'
endif
if a:kind == 18
return 'Array'
endif
if a:kind == 19
return 'Object'
endif
if a:kind == 20
return 'Key'
endif
if a:kind == 21
return 'Null'
endif
if a:kind == 22
return 'EnumMember'
endif
if a:kind == 23
return 'Struct'
endif
if a:kind == 24
return 'Event'
endif
if a:kind == 25
return 'Operator'
endif
if a:kind == 26
return 'TypeParameter'
endif
endfunction
6 changes: 6 additions & 0 deletions doc/lsc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ under the cursor, including it's definition. Sends a "textDocument/references"
request with the location set to the cursor's position. With
|lsc-default-map| this command is bound to "gr".

*:LSClientDocumentSymbol*
Populate the |quickfix| with a list of the symbols defined in the current
buffer. Sends a "textDocument/documentSymbol" request. With |lsc-default-map|
this command is bound to "gs".

*:LSClientShowHover*
Open a |preview| window with hover information corresponding to the element
under the cursor. Sends a "textDocument/hover" request with the location set
Expand Down Expand Up @@ -192,6 +197,7 @@ The default mapping for keys, if you've opted in to "g:lsc_auto_map" are:

<C-]> |:LSClientGoToDefinition|
gr |:LSClientFindReferences|
gs |:LSClientDocumentSymbol|
ga |:LSClientFindCodeActions|
gR |:LSClientRename|
K |:LSClientShowHover|
Expand Down
1 change: 1 addition & 0 deletions plugin/lsc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ endif
command! LSClientGoToDefinition call lsc#reference#goToDefinition()
command! LSClientFindReferences call lsc#reference#findReferences()
command! LSClientShowHover call lsc#reference#hover()
command! LSClientDocumentSymbol call lsc#reference#documentSymbols()
command! LSClientFindCodeActions call lsc#edit#findCodeActions()
command! LSClientAllDiagnostics call lsc#diagnostics#showInQuickFix()
command! LSClientRestartServer call <SID>IfEnabled('lsc#server#restart')
Expand Down

0 comments on commit c9955ea

Please sign in to comment.