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

add initial support for type hierarchy #641

Merged
merged 5 commits into from
Jan 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions LICENSE-THIRD-PARTY
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ their own copyright notices and license terms:
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


* vim-yggdrasil - https://github.com/m-pilia/vim-yggdrasil
* autoload/utils/tree.vim
====================================================================

Copyright 2019 Martino Pilia <martino.pilia@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Refer to `:h vim-lsp-semantic` for more info.
|`:LspRename`| Rename symbol |
|`:LspStatus` | Show the status of the language server |
|`:LspTypeDefinition`| Go to the type definition of the word under the cursor, and open in the current window |
|`:LspTypeHierarchy`| View type hierarchy of the symbol under the cursor |
|`:LspWorkspaceSymbol`| Search/Show workspace symbol |

### Diagnostics
Expand Down
3 changes: 2 additions & 1 deletion autoload/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ function! lsp#default_get_supported_capabilities(server_info) abort
\ },
\ 'semanticHighlightingCapabilities': {
\ 'semanticHighlighting': lsp#ui#vim#semantic#is_enabled()
\ }
\ },
\ 'typeHierarchy': v:false,
\ }
\ }
endfunction
Expand Down
4 changes: 4 additions & 0 deletions autoload/lsp/capabilities.vim
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ function! lsp#capabilities#has_type_definition_provider(server_name) abort
return s:has_bool_provider(a:server_name, 'typeDefinitionProvider')
endfunction

function! lsp#capabilities#has_type_hierarchy_provider(server_name) abort
return s:has_bool_provider(a:server_name, 'typeHierarchyProvider')
endfunction

function! lsp#capabilities#has_document_highlight_provider(server_name) abort
return s:has_bool_provider(a:server_name, 'documentHighlightProvider')
endfunction
Expand Down
104 changes: 104 additions & 0 deletions autoload/lsp/ui/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ function! lsp#ui#vim#type_definition(in_preview) abort
echo 'Retrieving type definition ...'
endfunction

function! lsp#ui#vim#type_hierarchy() abort
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_type_hierarchy_provider(v:val)')
let s:last_req_id = s:last_req_id + 1

if len(l:servers) == 0
call s:not_supported('Retrieving type hierarchy')
return
endif
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_req_id': s:last_req_id }
" direction 0 children, 1 parent, 2 both
for l:server in l:servers
call lsp#send_request(l:server, {
\ 'method': 'textDocument/typeHierarchy',
\ 'params': {
\ 'textDocument': lsp#get_text_document_identifier(),
\ 'position': lsp#get_position(),
\ 'direction': 2,
\ 'resolve': 1,
\ },
\ 'on_notification': function('s:handle_type_hierarchy', [l:ctx, l:server, 'type hierarchy']),
\ })
endfor

echo 'Retrieving type hierarchy ...'
endfunction

function! lsp#ui#vim#declaration(in_preview) abort
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_declaration_provider(v:val)')
let s:last_req_id = s:last_req_id + 1
Expand Down Expand Up @@ -630,6 +656,84 @@ function! s:handle_code_action(server, last_req_id, type, data) abort
endif
endfunction

function! s:handle_type_hierarchy(ctx, server, type, data) abort "ctx = {counter, list, last_req_id}
prabirshrestha marked this conversation as resolved.
Show resolved Hide resolved
if a:ctx['last_req_id'] != s:last_req_id
return
endif

if lsp#client#is_error(a:data['response'])
call lsp#utils#error('Failed to '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
return
endif

if empty(a:data['response']['result'])
echo 'No type hierarchy found'
return
endif

" Create new buffer in a split
let l:position = 'topleft'
let l:orientation = 'new'
exec l:position . ' ' . 10 . l:orientation

let l:provider = {
\ 'root': a:data['response']['result'],
\ 'root_state': 'expanded',
\ 'bufnr': bufnr('%'),
\ 'getChildren': function('s:get_children_for_tree_hierarchy'),
\ 'getParent': function('s:get_parent_for_tree_hierarchy'),
\ 'getTreeItem': function('s:get_treeitem_for_tree_hierarchy'),
\ }

call lsp#utils#tree#new(l:provider)

echo 'Retrieved type hierarchy'
endfunction

function! s:hierarchyitem_to_treeitem(hierarchyitem) abort
return {
\ 'id': a:hierarchyitem,
\ 'label': a:hierarchyitem['name'],
\ 'command': function('s:hierarchy_treeitem_command', [a:hierarchyitem]),
\ 'collapsibleState': has_key(a:hierarchyitem, 'parents') && !empty(a:hierarchyitem['parents']) ? 'expanded' : 'none',
\ }
endfunction

function! s:hierarchy_treeitem_command(hierarchyitem) abort
prabirshrestha marked this conversation as resolved.
Show resolved Hide resolved
bwipeout

let l:path = lsp#utils#uri_to_path(a:hierarchyitem['uri'])
let l:line = a:hierarchyitem['range']['start']['line'] + 1
let l:char = a:hierarchyitem['range']['start']['character']
let l:col = lsp#utils#to_col(l:path, l:line, l:char)

let l:buffer = bufnr(l:path)
if &modified && !&hidden
let l:cmd = l:buffer !=# -1 ? 'sb ' . l:buffer : 'split ' . fnameescape(l:path)
else
echom 'edit'
let l:cmd = l:buffer !=# -1 ? 'b ' . l:buffer : 'edit ' . fnameescape(l:path)
endif
execute l:cmd . ' | call cursor('.l:line.','.l:col.')'
endfunction

function! s:get_children_for_tree_hierarchy(Callback, ...) dict abort
prabirshrestha marked this conversation as resolved.
Show resolved Hide resolved
if a:0 == 0
call a:Callback('success', [l:self['root']])
return
else
call a:Callback('success', a:1['parents'])
endif
endfunction

function! s:get_parent_for_tree_hierarchy(...) dict abort
prabirshrestha marked this conversation as resolved.
Show resolved Hide resolved
" TODO
endfunction

function! s:get_treeitem_for_tree_hierarchy(Callback, object) dict abort
prabirshrestha marked this conversation as resolved.
Show resolved Hide resolved
call a:Callback('success', s:hierarchyitem_to_treeitem(a:object))
endfunction

" @params
" server - string
" comand_or_code_action - Command | CodeAction
Expand Down
Loading