-
Notifications
You must be signed in to change notification settings - Fork 167
Working with older .csproj Projects
OmniSharp-vim does not have any notion of .csproj files, and OmniSharp-roslyn has no options for editing them. This means that adding a new file or renaming an existing one can break your project if you are using an older-style .csproj project, which lists all .cs files.
There is an open issue with OmniSharp-roslyn to alter the .csproj file, but it seems unlikely that this will be addressed, as the newer .csproj files don't need this functionality.
However, for OmniSharp-vim users stuck for the time being on older .csproj files, here are some functions which can be used to add the current file to a .csproj. Please note that these are naiive and hacky functions, use at your own risk!
These functions can be added directly to a .vimrc and used from the commandline or with a mapping. Alternatively, create a ~/.vim/autoload
folder, put these functions in a ~/.vim/autoload/myomnisharp.vim
script, rename AddToProject
to myomnisharp#AddToProject
and map it like this: nnoremap <space>osa :call myomnisharp#AddToProject()<CR>
.
" Search from the current directory up to the solution directory for a .csproj
" file, and return the first one found. Note that this may return the wrong
" .csproj file if two .csproj files are found in the same directory.
function! s:FindProject() abort
let l:base = fnamemodify(OmniSharp#FindSolutionOrDir(), ':h')
let l:dir = expand('%:p:h')
while 1
let l:projects = split(globpath(l:dir, '*.csproj'), '\n')
if len(l:projects)
return l:projects[0]
endif
let l:parent = fnamemodify(l:dir, ':h')
if l:dir ==# l:parent || l:dir ==# l:base
return ''
endif
let l:dir = l:parent
endwhile
endfunction
" Add the current file to the .csproj. The .csproj must be found in an ancestor
" directory and must already contain at least one .cs file.
function! AddToProject() abort
let l:filename = expand('%:p')
let l:project = s:FindProject()
if !len(l:project)
echohl WarningMsg | echomsg 'Project not found' | echohl None
endif
execute 'silent tabedit' l:project
call cursor(1, 1)
if !search('^\s*<compile include=".*\.cs"', '')
tabclose
echohl WarningMsg | echomsg 'Could not find a .cs entry' | echohl None
endif
normal! yypk
if fnamemodify(l:project, ':h') !=# getcwd()
execute 'lcd' fnamemodify(l:project, ':h')
endif
execute 's/".*\.cs"/"' . fnamemodify(l:filename, ':.:gs?/?\\/?') . '"/'
if g:OmniSharp_translate_cygwin_wsl || has('win32')
s?/?\\?g
s?\\>?/>?g
endif
write
tabclose
endfunction