Skip to content

Commit 8468ef6

Browse files
committed
Add OmniSharpDebugProject and OmniSharpCreateDebugConfig features
1 parent 9335b8b commit 8468ef6

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,20 @@ It is possible to run unit tests via OmniSharp-roslyn, with success/failures lis
426426

427427
**Note:** this is only available using the stdio server, and unfortunately does _not_ work in translated WSL, due to the way OmniSharp-roslyn runs the tests.
428428

429+
## Debugging
430+
431+
Using Vimspector, you can debug C# projects and tests.
432+
For debugging tests see the [Run unit tests](##Run unit tests) section.
433+
434+
```vim
435+
" Starts vimspector with an ad-hoc config that will debug the current project
436+
:OmniSharpDebugProject
437+
438+
" Create a .vimspector config that you can use to debug the current project and
439+
" change as needed.
440+
:OmniSharpCreateDebugConfig
441+
```
442+
429443

430444
## Configuration
431445

autoload/OmniSharp/actions/project.vim

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,81 @@ function! s:ProjectRH(Callback, bufnr, response) abort
2121
call a:Callback()
2222
endfunction
2323

24+
function! OmniSharp#actions#project#DebugProject(stopAtEntry, ...) abort
25+
if !OmniSharp#util#HasVimspector()
26+
echohl WarningMsg
27+
echomsg 'Vimspector required to debug project'
28+
echohl None
29+
return
30+
endif
31+
let bufnr = bufnr('%')
32+
function! DebugProjectCb(bufnr, stopAtEntry, args) abort
33+
let project = getbufvar(a:bufnr, 'OmniSharp_host').project
34+
" Make sure we're not running on a csx script
35+
if project.ScriptProject is v:null
36+
call vimspector#LaunchWithConfigurations({
37+
\ 'launch': {
38+
\ 'adapter': 'netcoredbg',
39+
\ 'configuration': {
40+
\ 'request': 'launch',
41+
\ 'program': project.MsBuildProject.TargetPath,
42+
\ 'args': a:args,
43+
\ 'stopAtEntry': a:stopAtEntry ? v:true : v:false
44+
\ }
45+
\ }
46+
\})
47+
else
48+
echohl WarningMsg
49+
echomsg 'DebugProject is not currently implemented for csx files'
50+
echohl None
51+
endif
52+
endfunction
53+
call OmniSharp#actions#project#Get(bufnr, function('DebugProjectCb', [bufnr, a:stopAtEntry, a:000]))
54+
endfunction
55+
56+
function! OmniSharp#actions#project#CreateDebugConfig(stopAtEntry, ...) abort
57+
let bufnr = bufnr('%')
58+
function! CreateDebugConfigCb(bufnr, stopAtEntry, args) abort
59+
let host = getbufvar(a:bufnr, 'OmniSharp_host')
60+
let contents = [
61+
\' {',
62+
\' "configurations": {',
63+
\' "attach": {',
64+
\' "adapter": "netcoredbg",',
65+
\' "configuration": {',
66+
\' "request": "attach",',
67+
\' "processId": "${pid}"',
68+
\' }',
69+
\' },',
70+
\' "launch": {',
71+
\' "adapter": "netcoredbg",',
72+
\' "configuration": {',
73+
\' "request": "launch",',
74+
\' "program": "'.host.project.MsBuildProject.TargetPath.'",',
75+
\' "args": ' . json_encode(a:args) . ',',
76+
\' "stopAtEntry": ' . (a:stopAtEntry ? 'true' : 'false'),
77+
\' }',
78+
\' }',
79+
\' }',
80+
\' }',
81+
\ ]
82+
if isdirectory(host.sln_or_dir)
83+
let hostdir = host.sln_or_dir
84+
else
85+
let hostdir = fnamemodify(host.sln_or_dir, ':h:p')
86+
endif
87+
let filename = hostdir . '/.vimspector.json'
88+
call writefile(contents, filename)
89+
execute 'edit ' . filename
90+
if !OmniSharp#util#HasVimspector()
91+
echohl WarningMsg
92+
echomsg 'Vimspector does not seem to be installed. You will need it to run the created configuration.'
93+
echohl None
94+
endif
95+
endfunction
96+
call OmniSharp#actions#project#Get(bufnr, function('CreateDebugConfigCb', [bufnr, a:stopAtEntry, a:000]))
97+
endfunction
98+
2499
let &cpoptions = s:save_cpo
25100
unlet s:save_cpo
26101

autoload/OmniSharp/actions/test.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function! OmniSharp#actions#test#Run(...) abort
2323
endfunction
2424

2525
function! OmniSharp#actions#test#Debug(...) abort
26-
if !exists('g:vimspector_home')
26+
if !OmniSharp#util#HasVimspector()
2727
echohl WarningMsg
2828
echomsg 'Vimspector required to debug tests'
2929
echohl None

autoload/OmniSharp/util.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ function! s:is_wsl() abort
3333
return s:is_wsl_val
3434
endfunction
3535

36+
function! OmniSharp#util#HasVimspector() abort
37+
return exists('g:vimspector_home')
38+
endfunction
39+
3640
" Call a list of async functions in parallel, and wait for them all to complete
3741
" before calling the OnAllComplete function.
3842
function! OmniSharp#util#AwaitParallel(Funcs, OnAllComplete) abort

doc/omnisharp-vim.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,25 @@ convenient user re-mapping. These can be used like so: >
623623
:OmniSharpCodeFormat
624624
Formats code
625625

626+
*:OmniSharpDebugProject*
627+
*<Plug>(omnisharp_debug_project)*
628+
:OmniSharpDebugProject
629+
Run vimspector with an ad-hoc config on the current project.
630+
Running this with a bang (OmniSharpDebugProject!) will break on entry in the
631+
debugger.
632+
The command takes optional arguments that will be passed to the debugged
633+
process as args.
634+
635+
*:OmniSharpCreateDebugConfig*
636+
*<Plug>(omnisharp_create_debug_config)*
637+
:OmniSharpCreateDebugConfig
638+
Creates a .vimspector config next to the open sln file. Or at the root of the
639+
open directory if a directory is being used.
640+
For example, use this command to dump a configuration to disk when it gets to
641+
complicated to run using OmniSharpDebugProject.
642+
The command takes optional arguments that will be inserted into the generated
643+
config as args.
644+
626645
*:OmniSharpRestartAllServers*
627646
*<Plug>(omnisharp_restart_all_servers)*
628647
:OmniSharpRestartAllServers

ftplugin/cs/OmniSharp.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ command! -buffer -bar OmniSharpDebugTest call OmniSharp#actions#test#Debug()
8282
command! -buffer -bar -nargs=* -complete=file OmniSharpRunTestsInFile call OmniSharp#actions#test#RunInFile(<f-args>)
8383
command! -buffer -bar OmniSharpSignatureHelp call OmniSharp#actions#signature#SignatureHelp()
8484
command! -buffer -bar OmniSharpTypeLookup call OmniSharp#actions#documentation#TypeLookup()
85+
command! -buffer -bar -nargs=* -bang OmniSharpDebugProject call OmniSharp#actions#project#DebugProject(<bang>0, <f-args>)
86+
command! -buffer -bar -nargs=* -bang OmniSharpCreateDebugConfig call OmniSharp#actions#project#CreateDebugConfig(<bang>0, <f-args>)
8587

8688
nnoremap <buffer> <Plug>(omnisharp_code_format) :OmniSharpCodeFormat<CR>
8789
nnoremap <buffer> <Plug>(omnisharp_documentation) :OmniSharpDocumentation<CR>
@@ -114,6 +116,8 @@ nnoremap <buffer> <Plug>(omnisharp_start_server) :OmniSharpStartServer<CR>
114116
nnoremap <buffer> <Plug>(omnisharp_stop_all_servers) :OmniSharpStopAllServers<CR>
115117
nnoremap <buffer> <Plug>(omnisharp_stop_server) :OmniSharpStopServer<CR>
116118
nnoremap <buffer> <Plug>(omnisharp_type_lookup) :OmniSharpTypeLookup<CR>
119+
nnoremap <buffer> <Plug>(omnisharp_debug_project) :OmniSharpDebugProject<CR>
120+
nnoremap <buffer> <Plug>(omnisharp_create_debug_config) :OmniSharpCreateDebugConfig<CR>
117121
118122
" The following commands and mappings have been renamed, but the old versions
119123
" are kept here for backwards compatibility
@@ -162,6 +166,8 @@ let b:undo_ftplugin .= '
162166
\| delcommand OmniSharpStopAllServers
163167
\| delcommand OmniSharpStopServer
164168
\| delcommand OmniSharpTypeLookup
169+
\| delcommand OmniSharpDebugProject
170+
\| delcommand OmniSharpCreateDebugConfig
165171
\
166172
\| setlocal omnifunc<'
167173

0 commit comments

Comments
 (0)