Skip to content

Code Actions Available flag

Philip Becker edited this page Jul 4, 2020 · 5 revisions

Update: This functionality is now part of the vim-sharpenup plugin, which includes more options for configuring the code-actions-available flag.


To display a flag in the signs column when there are code actions available at the current cursor position, the OmniSharp#CountCodeActions function can be used:

actionsavailable

set updatetime=500

sign define OmniSharpCodeActions text=💡

augroup OSCountCodeActions
  autocmd!
  autocmd FileType cs set signcolumn=yes
  autocmd CursorHold *.cs call OSCountCodeActions()
augroup END

function! OSCountCodeActions() abort
  if bufname('%') ==# '' || OmniSharp#FugitiveCheck() | return | endif
  if !OmniSharp#IsServerRunning() | return | endif
  let opts = {
  \ 'CallbackCount': function('s:CBReturnCount'),
  \ 'CallbackCleanup': {-> execute('sign unplace 99')}
  \}
  call OmniSharp#actions#codeactions#Count(opts)
endfunction

function! s:CBReturnCount(count) abort
  if a:count
    let l = getpos('.')[1]
    let f = expand('%:p')
    execute ':sign place 99 line='.l.' name=OmniSharpCodeActions file='.f
  endif
endfunction

This code includes an autocmd to call user function OSCountCodeActions on CursorHold - so whenever the cursor is still for 'updatetime' milliseconds (the vim default is 4000 or 4 seconds, so this has been set to 500 here). The function calls OmniSharp#CountCodeActions and passes 2 callback functions: one to display the sign when the count is ready, and the other to clear it when the current code actions are no longer valid (i.e. whenever the cursor moves or leaves the buffer).

If there are any code actions, then the lightbulb glyph is displayed in the signs column.

Note that if you are using the HTTP OmniSharp-roslyn server, OmniSharp#CountCodeActions is performed synchronously, which means this will result in a lag while the code actions are being fetched. Try switching to the stdio server - running code actions asynchronously is a completely different experience - you can even trigger the function call with the CursorMoved autocmd instead of CursorHold if that's your preference.