-
Notifications
You must be signed in to change notification settings - Fork 166
Build command with autocomplete for .csproj files
rene-descartes2021 edited this page Sep 18, 2021
·
21 revisions
I needed a way to make a specific .csproj instead of every project in the .sln.
So I made it so the <tab>
key should autocomplete to a .csproj.
E.g. typing :Make <tab>
will result in a menu of options, or only one option if only one .csproj was found in the search space:
:Make Content.Shared/Content.Shared.csproj
:Make
BuildChecker/BuildChecker.csproj Content.Shared/Content.Shared.csproj
Content.Server.Database/Content.Server.Database.csproj
See :help wildmenu
.
The only plugin this approach depends on is something which defines asyncdo#run(), either hauleth/asyncdo.vim or an alternative like skywind3000/asyncrun.vim.
~/.vim/plugin/asyncdo.vim
:
"First a generalized way to have the makeprg be called async, not just C#.
"I think I found this on ThePrimeagen YouTube channel or other YouTube video.
"Display status of :AsyncDo (asyncdo#run) in status line:
let &statusline .= "%{exists('g:asyncdo')?'runing':''}"
"Make makeprg (:make) run async
command! -bang -nargs=* -complete=file Make call asyncdo#run(<bang>0, &makeprg, <f-args>)
"Make :grep run async
command! -bang -nargs=* -complete=dir Grep call asyncdo#run(
\ <bang>0,
\ { 'job': &grepprg, 'errorformat': &grepformat },
\ <f-args>)
~/.vim/ftplugin/cs.vim
:
"Reference: https://github.com/OmniSharp/omnisharp-vim/issues/386
compiler dotnet
" automatically open quickfix window after build is completed
augroup QUICKFIX_CS
autocmd!
autocmd QuickFixCmdPost [^l]* nested cwindow
autocmd QuickFixCmdPost l* nested lwindow
augroup END
~/.vim/compiler/dotnet.vim
:
if exists("current_compiler")
finish
endif
let current_compiler = "dotnet"
setlocal makeprg=dotnet\ build\ /v:q\ /property:GenerateFullPaths=true\ /clp:ErrorsOnly
setlocal errorformat=\ %#%f(%l\\\,%c):\ %m
set wildmenu
"Redefines earlier :Make command for .cs files.
command! -bang -nargs=* -complete=custom,DotNetFileComplete Make asyncdo#run(<bang>0, &makeprg, <f-args>)
":help command-completion-custom
fun DotNetFileComplete(A,L,P)
"return split(globpath(&path, a:A), "\n")
"First try to find match from current file's folder
let matches=globpath(expand('%:.:h'),'*.csproj')
"If no matches then try solution directory subdirectories:
if empty(matches)
let matches=globpath('*','*.csproj')
endif
return matches
endfun
If you have many .csproj files in your .sln, then maybe setting the vertical option in :help wildmode
may help navigation.
The DotNetFileComplete function above could use generalized refinement:
- Maybe iteratively searching up from the current file's folder to the solution folder, then recursively through all subdirectories if no match found on first iteration.
- Maybe some caching of the result/selection and any other args for a given directory in a tempfile(). So
:Make
no args would prefer the cached choice.