|
| 1 | +let s:folding_ranges = {} |
| 2 | +let s:textprop_name = 'vim-lsp-folding-linenr' |
| 3 | + |
| 4 | +function! s:find_servers() abort |
| 5 | + return filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_folding_range_provider(v:val)') |
| 6 | +endfunction |
| 7 | + |
| 8 | +function! lsp#ui#vim#folding#fold(sync) abort |
| 9 | + let l:servers = s:find_servers() |
| 10 | + |
| 11 | + if len(l:servers) == 0 |
| 12 | + call lsp#utils#error('Folding not supported for ' . &filetype) |
| 13 | + return |
| 14 | + endif |
| 15 | + |
| 16 | + let l:server = l:servers[0] |
| 17 | + call lsp#ui#vim#folding#send_request(l:server, bufnr('%'), a:sync) |
| 18 | +endfunction |
| 19 | + |
| 20 | +function! s:set_textprops(buf) abort |
| 21 | + " Use zero-width text properties to act as a sort of "mark" in the buffer. |
| 22 | + " This is used to remember the line numbers at the time the request was |
| 23 | + " sent. We will let Vim handle updating the line numbers when the user |
| 24 | + " inserts or deletes text. |
| 25 | + |
| 26 | + " Create text property, if not already defined |
| 27 | + silent! call prop_type_add(s:textprop_name, {'bufnr': a:buf}) |
| 28 | + |
| 29 | + " First, clear all markers from the previous run |
| 30 | + call prop_remove({'type': s:textprop_name, 'bufnr': a:buf}) |
| 31 | + |
| 32 | + " Add markers to each line |
| 33 | + let l:i = 1 |
| 34 | + while l:i <= line('$') |
| 35 | + call prop_add(l:i, 1, {'bufnr': a:buf, 'type': s:textprop_name, 'id': l:i}) |
| 36 | + let l:i += 1 |
| 37 | + endwhile |
| 38 | +endfunction |
| 39 | + |
| 40 | +function! lsp#ui#vim#folding#send_request(server_name, buf, sync) abort |
| 41 | + if !lsp#capabilities#has_folding_range_provider(a:server_name) |
| 42 | + return |
| 43 | + endif |
| 44 | + |
| 45 | + if !g:lsp_fold_enabled |
| 46 | + call lsp#log('Skip sending fold request: folding was disabled explicitly') |
| 47 | + return |
| 48 | + endif |
| 49 | + |
| 50 | + if has('textprop') |
| 51 | + call s:set_textprops(a:buf) |
| 52 | + endif |
| 53 | + |
| 54 | + call lsp#send_request(a:server_name, { |
| 55 | + \ 'method': 'textDocument/foldingRange', |
| 56 | + \ 'params': { |
| 57 | + \ 'textDocument': lsp#get_text_document_identifier(a:buf) |
| 58 | + \ }, |
| 59 | + \ 'on_notification': function('s:handle_fold_request', [a:server_name]), |
| 60 | + \ 'sync': a:sync |
| 61 | + \ }) |
| 62 | +endfunction |
| 63 | + |
| 64 | +function! s:foldexpr(server, buf, linenr) abort |
| 65 | + let l:foldlevel = 0 |
| 66 | + let l:prefix = '' |
| 67 | + |
| 68 | + for l:folding_range in s:folding_ranges[a:server][a:buf] |
| 69 | + if type(l:folding_range) == type({}) && |
| 70 | + \ has_key(l:folding_range, 'startLine') && |
| 71 | + \ has_key(l:folding_range, 'endLine') |
| 72 | + let l:start = l:folding_range['startLine'] + 1 |
| 73 | + let l:end = l:folding_range['endLine'] + 1 |
| 74 | + |
| 75 | + if (l:start <= a:linenr) && (a:linenr <= l:end) |
| 76 | + let l:foldlevel += 1 |
| 77 | + endif |
| 78 | + |
| 79 | + if l:start == a:linenr |
| 80 | + let l:prefix = '>' |
| 81 | + elseif l:end == a:linenr |
| 82 | + let l:prefix = '<' |
| 83 | + endif |
| 84 | + endif |
| 85 | + endfor |
| 86 | + |
| 87 | + " Only return marker if a fold starts/ends at this line. |
| 88 | + " Otherwise, return '='. |
| 89 | + return (l:prefix ==# '') ? '=' : (l:prefix . l:foldlevel) |
| 90 | +endfunction |
| 91 | + |
| 92 | +" Searches for text property of the correct type on the given line. |
| 93 | +" Returns the original linenr on success, or -1 if no textprop of the correct |
| 94 | +" type is associated with this line. |
| 95 | +function! s:get_textprop_line(linenr) abort |
| 96 | + let l:props = filter(prop_list(a:linenr), {idx, prop -> prop['type'] ==# s:textprop_name}) |
| 97 | + |
| 98 | + if empty(l:props) |
| 99 | + return -1 |
| 100 | + else |
| 101 | + return l:props[0]['id'] |
| 102 | + endif |
| 103 | +endfunction |
| 104 | + |
| 105 | +function! lsp#ui#vim#folding#foldexpr() abort |
| 106 | + let l:servers = s:find_servers() |
| 107 | + |
| 108 | + if len(l:servers) == 0 |
| 109 | + return |
| 110 | + endif |
| 111 | + |
| 112 | + let l:server = l:servers[0] |
| 113 | + |
| 114 | + if has('textprop') |
| 115 | + " Does the current line have a textprop with original line info? |
| 116 | + let l:textprop_line = s:get_textprop_line(v:lnum) |
| 117 | + |
| 118 | + if l:textprop_line == -1 |
| 119 | + " No information for current line available, so use indent for |
| 120 | + " previous line. |
| 121 | + return '=' |
| 122 | + else |
| 123 | + " Info available, use foldexpr as it would be with original line |
| 124 | + " number |
| 125 | + return s:foldexpr(l:server, bufnr('%'), l:textprop_line) |
| 126 | + endif |
| 127 | + else |
| 128 | + return s:foldexpr(l:server, bufnr('%'), v:lnum) |
| 129 | + endif |
| 130 | +endfunction |
| 131 | + |
| 132 | +function! lsp#ui#vim#folding#foldtext() abort |
| 133 | + let l:num_lines = v:foldend - v:foldstart + 1 |
| 134 | + let l:summary = getline(v:foldstart) . '...' |
| 135 | + |
| 136 | + " Join all lines in the fold |
| 137 | + let l:combined_lines = '' |
| 138 | + let l:i = v:foldstart |
| 139 | + while l:i <= v:foldend |
| 140 | + let l:combined_lines .= getline(l:i) . ' ' |
| 141 | + let l:i += 1 |
| 142 | + endwhile |
| 143 | + |
| 144 | + " Check if we're in a comment |
| 145 | + let l:comment_regex = '\V' . substitute(&l:commentstring, '%s', '\\.\\*', '') |
| 146 | + if l:combined_lines =~? l:comment_regex |
| 147 | + let l:summary = l:combined_lines |
| 148 | + endif |
| 149 | + |
| 150 | + return l:summary . ' (' . l:num_lines . ' ' . (l:num_lines == 1 ? 'line' : 'lines') . ') ' |
| 151 | +endfunction |
| 152 | + |
| 153 | +function! s:handle_fold_request(server, data) abort |
| 154 | + if lsp#client#is_error(a:data) || !has_key(a:data, 'response') || !has_key(a:data['response'], 'result') |
| 155 | + return |
| 156 | + endif |
| 157 | + |
| 158 | + let l:result = a:data['response']['result'] |
| 159 | + |
| 160 | + if type(l:result) != type([]) |
| 161 | + return |
| 162 | + endif |
| 163 | + |
| 164 | + let l:uri = a:data['request']['params']['textDocument']['uri'] |
| 165 | + let l:path = lsp#utils#uri_to_path(l:uri) |
| 166 | + let l:bufnr = bufnr(l:path) |
| 167 | + |
| 168 | + if l:bufnr < 0 |
| 169 | + return |
| 170 | + endif |
| 171 | + |
| 172 | + if !has_key(s:folding_ranges, a:server) |
| 173 | + let s:folding_ranges[a:server] = {} |
| 174 | + endif |
| 175 | + let s:folding_ranges[a:server][l:bufnr] = l:result |
| 176 | + |
| 177 | + " Set 'foldmethod' back to 'expr', which forces a re-evaluation of |
| 178 | + " 'foldexpr'. Only do this if the user hasn't changed 'foldmethod', |
| 179 | + " and this is the correct buffer. |
| 180 | + let l:current_window = winnr() |
| 181 | + windo if &l:foldmethod ==# 'expr' && bufnr('%') == l:bufnr | let &l:foldmethod = 'expr' | endif |
| 182 | + execute l:current_window . 'wincmd w' |
| 183 | +endfunction |
0 commit comments