]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/vim-lsp/autoload/lsp/ui/vim/utils.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge commit 'd49e95aa7ba744f0a7f544aca43afdb6aab41f24' as '.vim/bundle/asyncomplete...
[etc/vim.git] / .vim / bundle / vim-lsp / autoload / lsp / ui / vim / utils.vim
1 let s:default_symbol_kinds = {
2     \ '1': 'file',
3     \ '2': 'module',
4     \ '3': 'namespace',
5     \ '4': 'package',
6     \ '5': 'class',
7     \ '6': 'method',
8     \ '7': 'property',
9     \ '8': 'field',
10     \ '9': 'constructor',
11     \ '10': 'enum',
12     \ '11': 'interface',
13     \ '12': 'function',
14     \ '13': 'variable',
15     \ '14': 'constant',
16     \ '15': 'string',
17     \ '16': 'number',
18     \ '17': 'boolean',
19     \ '18': 'array',
20     \ '19': 'object',
21     \ '20': 'key',
22     \ '21': 'null',
23     \ '22': 'enum member',
24     \ '23': 'struct',
25     \ '24': 'event',
26     \ '25': 'operator',
27     \ '26': 'type parameter',
28     \ }
29
30 let s:symbol_kinds = {}
31
32 let s:diagnostic_severity = {
33     \ 1: 'Error',
34     \ 2: 'Warning',
35     \ 3: 'Information',
36     \ 4: 'Hint',
37     \ }
38
39 function! s:symbols_to_loc_list_children(server, path, list, symbols, depth) abort
40     for l:symbol in a:symbols
41         let [l:line, l:col] = lsp#utils#position#lsp_to_vim(a:path, l:symbol['range']['start'])
42
43         call add(a:list, {
44             \ 'filename': a:path,
45             \ 'lnum': l:line,
46             \ 'col': l:col,
47             \ 'text': lsp#ui#vim#utils#_get_symbol_text_from_kind(a:server, l:symbol['kind']) . ' : ' . printf('%' . a:depth. 's', '  ') . l:symbol['name'],
48             \ })
49         if has_key(l:symbol, 'children') && !empty(l:symbol['children'])
50             call s:symbols_to_loc_list_children(a:server, a:path, a:list, l:symbol['children'], a:depth + 1)
51         endif
52     endfor
53 endfunction
54
55 function! lsp#ui#vim#utils#symbols_to_loc_list(server, result) abort
56     if !has_key(a:result['response'], 'result')
57         return []
58     endif
59
60     let l:list = []
61
62     let l:locations = type(a:result['response']['result']) == type({}) ? [a:result['response']['result']] : a:result['response']['result']
63
64     if !empty(l:locations) " some servers also return null so check to make sure it isn't empty
65         for l:symbol in a:result['response']['result']
66             if has_key(l:symbol, 'location')
67                 let l:location = l:symbol['location']
68                 if lsp#utils#is_file_uri(l:location['uri'])
69                     let l:path = lsp#utils#uri_to_path(l:location['uri'])
70                     let [l:line, l:col] = lsp#utils#position#lsp_to_vim(l:path, l:location['range']['start'])
71                     call add(l:list, {
72                         \ 'filename': l:path,
73                         \ 'lnum': l:line,
74                         \ 'col': l:col,
75                         \ 'text': lsp#ui#vim#utils#_get_symbol_text_from_kind(a:server, l:symbol['kind']) . ' : ' . (g:lsp_document_symbol_detail ? l:symbol['detail'] : l:symbol['name']),
76                         \ })
77                 endif
78             else
79                 let l:location = a:result['request']['params']['textDocument']['uri']
80                 if lsp#utils#is_file_uri(l:location)
81                     let l:path = lsp#utils#uri_to_path(l:location)
82                     let [l:line, l:col] = lsp#utils#position#lsp_to_vim(l:path, l:symbol['range']['start'])
83                     call add(l:list, {
84                         \ 'filename': l:path,
85                         \ 'lnum': l:line,
86                         \ 'col': l:col,
87                         \ 'text': lsp#ui#vim#utils#_get_symbol_text_from_kind(a:server, l:symbol['kind']) . ' : ' . (g:lsp_document_symbol_detail ? l:symbol['detail'] : l:symbol['name']),
88                         \ })
89                     if has_key(l:symbol, 'children') && !empty(l:symbol['children'])
90                         call s:symbols_to_loc_list_children(a:server, l:path, l:list, l:symbol['children'], 1)
91                     endif
92                 endif
93             endif
94         endfor
95     endif
96
97     return l:list
98 endfunction
99
100 function! lsp#ui#vim#utils#diagnostics_to_loc_list(result) abort
101     if !has_key(a:result['response'], 'params')
102         return
103     endif
104
105     let l:uri = a:result['response']['params']['uri']
106     let l:diagnostics = lsp#utils#iteratable(a:result['response']['params']['diagnostics'])
107
108     let l:list = []
109
110     if !empty(l:diagnostics) && lsp#utils#is_file_uri(l:uri)
111         let l:path = lsp#utils#uri_to_path(l:uri)
112         for l:item in l:diagnostics
113             let l:severity_text = ''
114             if has_key(l:item, 'severity') && !empty(l:item['severity'])
115                 let l:severity_text = s:get_diagnostic_severity_text(l:item['severity'])
116             endif
117             let l:text = ''
118             if has_key(l:item, 'source') && !empty(l:item['source'])
119                 let l:text .= l:item['source'] . ':'
120             endif
121             if l:severity_text !=# ''
122                 let l:text .= l:severity_text . ':'
123             endif
124             if has_key(l:item, 'code') && !empty(l:item['code'])
125                 let l:text .= l:item['code'] . ':'
126             endif
127             let l:text .= l:item['message']
128             let [l:line, l:col] = lsp#utils#position#lsp_to_vim(l:path, l:item['range']['start'])
129             let l:location_item = {
130                 \ 'filename': l:path,
131                 \ 'lnum': l:line,
132                 \ 'col': l:col,
133                 \ 'text': l:text,
134                 \ }
135             if l:severity_text !=# ''
136                 " 'E' for error, 'W' for warning, 'I' for information, 'H' for hint
137                 let l:location_item['type'] = l:severity_text[0]
138             endif
139             call add(l:list, l:location_item)
140         endfor
141     endif
142
143     return l:list
144 endfunction
145
146 function! lsp#ui#vim#utils#_get_symbol_text_from_kind(server, kind) abort
147     if !has_key(s:symbol_kinds, a:server)
148         let l:server_info = lsp#get_server_info(a:server)
149         if has_key (l:server_info, 'config') && has_key(l:server_info['config'], 'symbol_kinds')
150             let s:symbol_kinds[a:server] = extend(copy(s:default_symbol_kinds), l:server_info['config']['symbol_kinds'])
151         else
152             let s:symbol_kinds[a:server] = s:default_symbol_kinds
153         endif
154     endif
155     return get(s:symbol_kinds[a:server], a:kind, 'unknown symbol ' . a:kind)
156 endfunction
157
158 function! lsp#ui#vim#utils#get_symbol_kinds() abort
159     return map(keys(s:default_symbol_kinds), {idx, key -> str2nr(key)})
160 endfunction
161
162 function! s:get_diagnostic_severity_text(severity) abort
163     return s:diagnostic_severity[a:severity]
164 endfunction
165
166 function! lsp#ui#vim#utils#setqflist(list, type) abort
167   if has('patch-8.2.2147')
168     call setqflist(a:list)
169     call setqflist([], 'a', {'title': a:type})
170   else
171     call setqflist([])
172     call setqflist(a:list)
173   endif
174 endfunction