Skip to content

Commit fff862d

Browse files
committed
Add :OmniSharpFold command to fold classes/methods
1 parent 032e10a commit fff862d

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

autoload/OmniSharp/actions/codestructure.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
let s:save_cpo = &cpoptions
22
set cpoptions&vim
33

4-
function! OmniSharp#actions#codestructure#Get(bufnr, Callback) abort
4+
function! OmniSharp#actions#codestructure#Get(bufnr, sendbuffer, Callback) abort
55
let opts = {
66
\ 'ResponseHandler': function('s:CodeStructureRH', [a:bufnr, a:Callback]),
77
\ 'BufNum': a:bufnr,
8-
\ 'SendBuffer': 0
8+
\ 'SendBuffer': a:sendbuffer
99
\}
1010
call OmniSharp#stdio#Request('/v2/codestructure', opts)
1111
endfunction

autoload/OmniSharp/actions/fold.vim

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
let s:save_cpo = &cpoptions
2+
set cpoptions&vim
3+
4+
function! OmniSharp#actions#fold#Create() abort
5+
if !g:OmniSharp_server_stdio
6+
echomsg 'This functionality is only available with the stdio server'
7+
endif
8+
call OmniSharp#actions#codestructure#Get(bufnr('%'), 1,
9+
\ function('s:CreateFolds'))
10+
endfunction
11+
12+
function! s:CreateFolds(bufnr, codeElements) abort
13+
if a:bufnr != bufnr('%') | return | endif
14+
setlocal foldmethod=manual
15+
normal! zE
16+
for range in reverse(s:FindBlocks(a:codeElements))
17+
execute printf('%d,%dfold', range[0], range[1])
18+
endfor
19+
endfunction
20+
21+
function! s:FindBlocks(codeElements) abort
22+
if type(a:codeElements) != type([]) | return [] | endif
23+
let ranges = []
24+
for element in a:codeElements
25+
if get(element, 'Kind', '') !=# 'namespace'
26+
if has_key(element, 'Ranges') && has_key(element.Ranges, 'full')
27+
let full = element.Ranges.full
28+
let start = get(get(full, 'Start', {}), 'Line', 0)
29+
let end = get(get(full, 'End', {}), 'Line', 0)
30+
if end - start >= &foldminlines
31+
call add(ranges, [start, end])
32+
endif
33+
endif
34+
endif
35+
call extend(ranges, s:FindBlocks(get(element, 'Children', [])))
36+
endfor
37+
return ranges
38+
endfunction
39+
40+
let &cpoptions = s:save_cpo
41+
unlet s:save_cpo
42+
43+
" vim:et:sw=2:sts=2

autoload/OmniSharp/actions/test.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function! OmniSharp#actions#test#Run(...) abort
1414
return
1515
endif
1616
let s:runningTest = 1
17-
call OmniSharp#actions#codestructure#Get(bufnr,
17+
call OmniSharp#actions#codestructure#Get(bufnr, 0,
1818
\ function('s:RunTest', [function('s:CBRunTest')]))
1919
endfunction
2020

@@ -176,7 +176,7 @@ endfunction
176176

177177
function! s:FindTestsInFiles(Callback, buffers, ...) abort
178178
call OmniSharp#util#AwaitParallel(
179-
\ map(copy(a:buffers), {i,b -> function('OmniSharp#actions#codestructure#Get', [b])}),
179+
\ map(copy(a:buffers), {i,b -> function('OmniSharp#actions#codestructure#Get', [b, 0])}),
180180
\ function('s:RunTestsInFiles', [a:Callback]))
181181
endfunction
182182

ftplugin/cs/OmniSharp.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ command! -buffer -bar -nargs=? OmniSharpFindSymbol call OmniSharp#actions#symbol
3434
command! -buffer -bar -nargs=? OmniSharpFindType call OmniSharp#actions#symbols#Find(<q-args>, 'Type')
3535
command! -buffer -bar OmniSharpFindUsages call OmniSharp#actions#usages#Find()
3636
command! -buffer -bar OmniSharpFixUsings call OmniSharp#actions#usings#Fix()
37+
command! -buffer -bar OmniSharpFold call OmniSharp#actions#fold#Create()
3738
command! -buffer -bar OmniSharpGetCodeActions call OmniSharp#actions#codeactions#Get('normal')
3839
command! -buffer -bar OmniSharpGlobalCodeCheck call OmniSharp#actions#diagnostics#CheckGlobal()
3940
command! -buffer -bar OmniSharpGotoDefinition call OmniSharp#actions#definition#Find()
@@ -59,6 +60,7 @@ nnoremap <buffer> <Plug>(omnisharp_find_symbol) :OmniSharpFindSymbol<CR>
5960
nnoremap <buffer> <Plug>(omnisharp_find_type) :OmniSharpFindType<CR>
6061
nnoremap <buffer> <Plug>(omnisharp_find_usages) :OmniSharpFindUsages<CR>
6162
nnoremap <buffer> <Plug>(omnisharp_fix_usings) :OmniSharpFixUsings<CR>
63+
nnoremap <buffer> <Plug>(omnisharp_fold) :OmniSharpFold<CR>
6264
nnoremap <buffer> <Plug>(omnisharp_code_actions) :OmniSharpGetCodeActions<CR>
6365
xnoremap <buffer> <Plug>(omnisharp_code_actions) :call OmniSharp#actions#codeactions#Get('visual')<CR>
6466
nnoremap <buffer> <Plug>(omnisharp_code_action_repeat) :OmniSharpRepeatCodeAction<CR>
@@ -105,6 +107,7 @@ let b:undo_ftplugin .= '
105107
\| delcommand OmniSharpFindType
106108
\| delcommand OmniSharpFindUsages
107109
\| delcommand OmniSharpFixUsings
110+
\| delcommand OmniSharpFold
108111
\| delcommand OmniSharpGetCodeActions
109112
\| delcommand OmniSharpGlobalCodeCheck
110113
\| delcommand OmniSharpGotoDefinition

0 commit comments

Comments
 (0)