From ad92c42f97c2cb495c34b83f43d91b3d6cfdfa4e Mon Sep 17 00:00:00 2001 From: Rene Descartes Date: Wed, 8 Sep 2021 08:08:38 -0600 Subject: [PATCH] Add C# language layer --- core/ftplugin/cs.vim | 67 ++++++++ layers/+lang/csharp/README.md | 52 ++++++ layers/+lang/csharp/config.vim | 180 +++++++++++++++++++++ layers/+lang/csharp/packages.vim | 3 + layers/+programming/programming/config.vim | 3 +- layers/LAYERS.md | 3 +- 6 files changed, 306 insertions(+), 2 deletions(-) create mode 100644 core/ftplugin/cs.vim create mode 100644 layers/+lang/csharp/README.md create mode 100644 layers/+lang/csharp/config.vim create mode 100644 layers/+lang/csharp/packages.vim diff --git a/core/ftplugin/cs.vim b/core/ftplugin/cs.vim new file mode 100644 index 00000000..e79b5ffe --- /dev/null +++ b/core/ftplugin/cs.vim @@ -0,0 +1,67 @@ +if exists('b:did_spacevim_cs_ftplugin') || !spacevim#load('csharp') + finish +endif +let b:did_spacevim_cs_ftplugin = 1 + +let b:ale_linters = ['OmniSharp'] + +compiler dotnet + +if spacevim#load('lightline') + "b:lightline didn't work, g:lightline does + "Maybe direct to a function based on filetype? + let g:lightline['component'] = { + \ 'sharpenup': sharpenup#statusline#Build() + \} + + augroup setupOmniSharpLightlineIntegration + autocmd! + autocmd User OmniSharpProjectUpdated,OmniSharpReady call lightline#update() + augroup END +endif + +" [re]defines :Make command, with autocomplete to a csproj in the sln +command! -bang -nargs=* -bar -complete=custom,s:DotNetFileComplete Make AsyncRun -program=make @ + +" Find relevant .csproj files to populate autocomplete list +" See `:help command-completion-custom` +function! s:DotNetFileComplete(A,L,P) + let searchdir = expand('%:.:h') + let matches = '' + " If we're not relative to the cwd (e.g. in :help), don't try to search + if fnamemodify(searchdir,':p:h') !=? searchdir + let host = OmniSharp#GetHost(bufnr('%')) + let csprojs = deepcopy(host.job.projects) + let csprojs_relative = map(csprojs, {index, value -> fnamemodify(value['path'], ':.')}) + if has_key(host, 'project') + " Make the first project this file is in first in the sln list + let project = fnamemodify(host['project']['MsBuildProject']['Path'], ':.') + let i = index(csprojs_relative, project) + call remove(csprojs_relative, i) + let matches = join(insert(csprojs_relative, project), "\n") + else + let matches = join(csprojs_relative, "\n") + endif + endif + return matches +endfunction + +command! -buffer -bar OmniSharpBuildProject call s:OmniSharpBuildProject() +command! -buffer -bar OmniSharpBuildSolution call s:OmniSharpBuildSolution() +nnoremap (omnisharp_build_project) :OmniSharpBuildProject +nnoremap (omnisharp_build_solution) :OmniSharpBuildSolution + +function! s:OmniSharpBuildProject() abort + let host = OmniSharp#GetHost(bufnr('%')) + let CsprojF = {->fnamemodify(host['project']['MsBuildProject']['Path'], ':.')} + if has_key(host, 'project') + call execute('Make '.CsprojF()) + else + call OmniSharp#actions#project#Get(bufnr('%'), { -> execute('Make '.CsprojF()) }) + endif +endfunction + +function! s:OmniSharpBuildSolution() abort + let sln = OmniSharp#GetHost(bufnr('%')).sln_or_dir + call execute('Make '.sln) +endfunction diff --git a/layers/+lang/csharp/README.md b/layers/+lang/csharp/README.md new file mode 100644 index 00000000..49710aad --- /dev/null +++ b/layers/+lang/csharp/README.md @@ -0,0 +1,52 @@ +# C# Layer + +## Table of Contents + + +* [Description](#description) +* [Install](#install) +* [Key Bindings](#keybindings) + + + +## Description + +This layer adds support for the C# language. + +## Install + +To use this configuration layer, add it to your `.spacevim`. + +## Key Bindings + +Here are most/all: + +```vim +let g:OmniSharp_popup_mappings = { +\ 'close': ['', 'gq'] +\ 'lineDown': '', +\ 'lineUp': '', +\ 'halfPageDown': '', +\ 'halfPageUp': '', +\ 'pageDown': ['', ''], +\ 'pageUp': ['', ''] +\ 'sigNext': '', +\ 'sigPrev': '', +\ 'sigParamNext': '', +\ 'sigParamPrev': '' +\} + +nmap (omnisharp_signature_help) +imap (omnisharp_signature_help) +nmap [[ (omnisharp_navigate_up) +nmap ]] (omnisharp_navigate_down) +"nmap ca (omnisharp_code_actions) +"xmap ca (omnisharp_code_actions) +"nmap . (omnisharp_code_action_repeat) +"xmap . (omnisharp_code_action_repeat) +``` + +The remainder are integrated with *vim-which-key* in the `/+lsp` category. + +See [OmniSharp/omnisharp-vim](https://github.com/OmniSharp/omnisharp-vim) for some bindings documentation. See `:help omnisharp`. +[nickspoons/vim-sharpenup](https://github.com/nickspoons/vim-sharpenup) includes some keybindings but they are not enabled and were instead integrated with *space-vim* and *vim-which-key*. diff --git a/layers/+lang/csharp/config.vim b/layers/+lang/csharp/config.vim new file mode 100644 index 00000000..bd2ce27d --- /dev/null +++ b/layers/+lang/csharp/config.vim @@ -0,0 +1,180 @@ +" OmniSharp/omnisharp-vim: { + let g:OmniSharp_popup_position = winwidth(0) >= 80 ? 'peek' : 'center' + if has('nvim') + let g:OmniSharp_popup_options = { + \ 'winhl': 'Normal:NormalFloat' + \} + else + let g:OmniSharp_popup_options = { + \ 'highlight': 'Normal', + \ 'padding': [0, 0, 0, 0], + \ 'border': [1] + \} + endif + let g:OmniSharp_popup_mappings = { + \ 'pageDown': ['', ''], + \ 'pageUp': ['', ''] + \} + + "if s:using_snippets + " let g:OmniSharp_want_snippet = 1 + "endif + + let g:OmniSharp_highlight_groups = { + \ 'ExcludedCode': 'NonText' + \} + "\ 'Comment': 'NonText', + "\ 'XmlDocCommentName': 'Identifier', + "\ 'XmlDocCommentText': 'NonText' + "\} + + if spacevim#load('clap') + let g:OmniSharp_selector_findusages = 'clap' + endif + let g:OmniSharp_autoselect_existing_sln = 1 + let g:OmniSharp_highlighting = 3 + let g:OmniSharp_diagnostic_exclude_paths = [ 'Temp[/\\]', 'obj[/\\]', '\.nuget[/\\]' ] + "let g:OmniSharp_fzf_options = { 'down': '10' }']]]' ] + + " The following autoformats the code on write per OmniSharp + " configuration when g:OmniSharp_code_format is set. + " Each codebase might have different configuration. + " Some codebases might be messy, where autoformat is a burden in making patches, thus this is false by default. + " https://github.com/OmniSharp/omnisharp-roslyn/wiki/Configuration-Options + let g:OmniSharp_code_format = v:false + augroup codeformatOmniSharp + autocmd! + autocmd BufWritePre *.cs :if g:OmniSharp_code_format | :OmniSharpCodeFormat | :endif + augroup END + + " Cache the 'project' data from OmniSharp-Roslyn to each cs buffer + augroup cacheOmniSharpProject + autocmd! + autocmd FileType cs call s:CacheOmniSharpProjectInBuffer({->0}) + augroup END + + function! s:CacheOmniSharpProjectInBuffer(callback) + if &filetype ==# 'cs' + let bufnr = bufnr('%') + let host = OmniSharp#GetHost(bufnr) + if !has_key(host, 'project') + " 'project' not in cache, must query from OmniSharp-Roslyn, is async + call OmniSharp#actions#project#Get(bufnr, a:callback) + endif + endif + endfunction + + if spacevim#load('programming') + if exists('g:vista_executive_for') && v:t_dict == type(g:vista_executive_for) + call extend(g:vista_executive_for, {'cs': 'ctags'}) + else + let g:vista_executive_for = {'cs': 'ctags'} + endif + endif +" } + +" nickspoons/vim-sharpenup: { + " We want everything from vim-sharpenup but the keybindings due to integration + let g:sharpenup_create_mappings = 0 + let g:sharpenup_map_legacy_csproj_actions = 0 + + " TODO: integrate mappings better: + nmap (omnisharp_signature_help) + imap (omnisharp_signature_help) + nmap [[ (omnisharp_navigate_up) + nmap ]] (omnisharp_navigate_down) + "nmap ca (omnisharp_code_actions) + "xmap ca (omnisharp_code_actions) + "nmap . (omnisharp_code_action_repeat) + "xmap . (omnisharp_code_action_repeat) + + " A `Dictionary-function` containing vim-which-key mappings + function! s:whichkey_omnisharp_integration() + if &filetype ==# 'cs' + let s:new_keymap = get(s:, 'new_keymap', { + \ 'name': '+omnisharp', + \ '.': ['(omnisharp_code_action_repeat)', 'repeat'], + \ 'a': ['(omnisharp_code_actions)' , 'code-action'], + \ 'c': ['(omnisharp_global_code_check)' , 'global-code-check'], + \ 'f': ['(omnisharp_code_format)' , 'formatting'], + \ 'h': ['(omnisharp_signature_help)' , 'hover'], + \ 'H': ['(omnisharp_highlight)' , 'highlight'], + \ 'r': ['(omnisharp_find_usages)' , 'references'], + \ 'R': ['(omnisharp_rename)' , 'rename'], + \ 's': ['(omnisharp_documentation)' , 'document-symbol'], + \ 'S': ['(omnisharp_find_symbol)' , 'workspace-symbol'], + \ 'g': { + \ 'name': '+goto', + \ 'd': ['(omnisharp_go_to_definition)', 'definition'], + \ 't': ['(omnisharp_find_type)' , 'type-definition'], + \ 'i': ['(omnisharp_find_implementations)', 'implementation'], + \ 's': ['(omnisharp_find_symbol)' , 'symbol'], + \ }, + \ 'p': { + \ 'name': '+preview', + \ 'd': ['(omnisharp_preview_definition)' , 'definition'], + \ 'i': ['(omnisharp_preview_implementations)', 'implementation'], + \ }, + \ 'b': { + \ 'name': '+build', + \ 'p': ['(omnisharp_build_project)' , 'build-project'], + \ 's': ['(omnisharp_build_solution)' , 'build-solution'], + \ 'd': ['(omnisharp_debug_project)' , 'debug-project'], + \ 'v': ['(omnisharp_create_debug_config)', 'create-vimspector-config'], + \ }, + \ 't': { + \ 'name': '+test', + \ 't': ['(omnisharp_run_test)' , 'run-test'], + \ 'n': ['(omnisharp_run_test_no_build)', 'run-test-no-build'], + \ 'T': ['(omnisharp_run_tests_in_file)', 'run-tests-in-file'], + \ 'N': ['(omnisharp_run_tests_in_file_no_build)', 'run-tests-in-file-no-build'], + \ 'd': ['(omnisharp_debug_test)' , 'debug-test'], + \ 'D': ['(omnisharp_debug_test_no_build)', 'debug-test-no-build'], + \ }, + \ 'x': { + \ 'name': '+csproj-modify', + \ 'a': ['(omnisharp_add_to_csproj)' , 'add-to-csproj'], + \ 'r': ['(omnisharp_rename_in_csproj)', 'rename-in-csproj'], + \ }, + \ 'X': { + \ 'name': '+server', + \ 's': [':OmniSharpStatus!' , 'status'], + \ 'S': ['(omnisharp_start_server)' , 'start'], + \ 't': ['(omnisharp_stop_server)' , 'stop'], + \ 'T': ['(omnisharp_stop_all_servers)' , 'stop-all'], + \ 'r': ['(omnisharp_restart_server)' , 'restart'], + \ 'R': ['(omnisharp_restart_all_servers)', 'restart-all'], + \ }, + \ }) + return s:new_keymap + else + return s:keep_keymap + endif + endfunction + + " Integrate OmniSharp hotkeys with vim-which-key + if spacevim#load('which-key') + let s:keep_keymap = get(s:, 'keep_keymap', deepcopy(g:spacevim#map#leader#desc['l'])) + let g:spacevim#map#leader#desc['l'] = function('s:whichkey_omnisharp_integration') + endif +" } + +" mattn/vim-lsp-settings: { + if exists('g:spacevim_lsp_engine') && g:spacevim_lsp_engine ==# 'vim_lsp' + let g:lsp_settings = get(g:, 'lsp_settings', {}) + if !has_key(g:lsp_settings, 'omnisharp-lsp') + let g:lsp_settings['omnisharp-lsp'] = {} + endif + let g:lsp_settings['omnisharp-lsp'].disabled = 1 + endif +" } + +" itchyny/lightline.vim: { + let g:sharpenup_statusline_opts = { 'Text': '%s (%p/%P)' } + let g:sharpenup_statusline_opts.Highlight = 0 +" } + +" As alternative to OmniSharpCodeFormat, can use different formatter +" vim-autoformat/vim-autoformat: { + "let g:formatdef_my_custom_cs = '"astyle --mode=cs --style=ansi -pcHs".&shiftwidth' +" } diff --git a/layers/+lang/csharp/packages.vim b/layers/+lang/csharp/packages.vim new file mode 100644 index 00000000..f478c1fa --- /dev/null +++ b/layers/+lang/csharp/packages.vim @@ -0,0 +1,3 @@ +MP 'OmniSharp/omnisharp-vim', { 'on_ft' : 'cs' } +MP 'nickspoons/vim-sharpenup', { 'on_ft' : 'cs' } +MP 'nickspoons/vim-cs', { 'on_ft' : 'cs' } diff --git a/layers/+programming/programming/config.vim b/layers/+programming/programming/config.vim index f0637326..80ca0721 100644 --- a/layers/+programming/programming/config.vim +++ b/layers/+programming/programming/config.vim @@ -83,7 +83,8 @@ augroup END " vim-polyglot { " Reset errorformat to its default value for cooperating with asyncrun.vim - autocmd BufEnter * set errorformat& + " BUG: Prevents other plugins from including CompilerSets + "autocmd BufEnter * set errorformat& " } " vim-rooter { diff --git a/layers/LAYERS.md b/layers/LAYERS.md index 10d5bdf3..934a76e5 100644 --- a/layers/LAYERS.md +++ b/layers/LAYERS.md @@ -1,7 +1,7 @@ Layer Manifest ============== -Last updated: 2022-09-29 12:26:10 +Last updated: 2022-09-29 12:39:45 Default layers: `fzf`, `better-defaults` and `which-key`. @@ -19,6 +19,7 @@ Topic | Layer | Plugins +fun | [goyo](https://github.com/liuchengxu/space-vim/tree/master/layers/+fun/goyo) |
  • [junegunn/goyo.vim](https://github.com/junegunn/goyo.vim)
  • [junegunn/limelight.vim](https://github.com/junegunn/limelight.vim)
+lang | [c-c++](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/c-c++) |
  • [octol/vim-cpp-enhanced-highlight](https://github.com/octol/vim-cpp-enhanced-highlight)
  • [rhysd/vim-clang-format](https://github.com/rhysd/vim-clang-format)
+lang | [clojure](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/clojure) |
  • [guns/vim-clojure-highlight](https://github.com/guns/vim-clojure-highlight)
  • [guns/vim-clojure-static](https://github.com/guns/vim-clojure-static)
  • [guns/vim-slamhound](https://github.com/guns/vim-slamhound)
  • [kovisoft/paredit](https://github.com/kovisoft/paredit)
  • [tpope/vim-fireplace](https://github.com/tpope/vim-fireplace)
  • [venantius/vim-cljfmt](https://github.com/venantius/vim-cljfmt)
++lang | [csharp](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/csharp) |
  • [OmniSharp/omnisharp-vim](https://github.com/OmniSharp/omnisharp-vim)
  • [nickspoons/vim-cs](https://github.com/nickspoons/vim-cs)
  • [nickspoons/vim-sharpenup](https://github.com/nickspoons/vim-sharpenup)
+lang | [elixir](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/elixir) |
  • [elixir-lang/vim-elixir](https://github.com/elixir-lang/vim-elixir)
  • [slashmili/alchemist.vim](https://github.com/slashmili/alchemist.vim)
+lang | [elm](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/elm) |
  • [ElmCast/elm-vim](https://github.com/ElmCast/elm-vim)
+lang | [erlang](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/erlang) |
  • [vim-erlang/erlang-motions.vim](https://github.com/vim-erlang/erlang-motions.vim)
  • [vim-erlang/vim-erlang-compiler](https://github.com/vim-erlang/vim-erlang-compiler)
  • [vim-erlang/vim-erlang-omnicomplete](https://github.com/vim-erlang/vim-erlang-omnicomplete)
  • [vim-erlang/vim-erlang-skeletons](https://github.com/vim-erlang/vim-erlang-skeletons)
  • [vim-erlang/vim-erlang-tags](https://github.com/vim-erlang/vim-erlang-tags)