Skip to content

Commit ad92c42

Browse files
Add C# language layer
1 parent 97d5557 commit ad92c42

File tree

6 files changed

+306
-2
lines changed

6 files changed

+306
-2
lines changed

core/ftplugin/cs.vim

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
if exists('b:did_spacevim_cs_ftplugin') || !spacevim#load('csharp')
2+
finish
3+
endif
4+
let b:did_spacevim_cs_ftplugin = 1
5+
6+
let b:ale_linters = ['OmniSharp']
7+
8+
compiler dotnet
9+
10+
if spacevim#load('lightline')
11+
"b:lightline didn't work, g:lightline does
12+
"Maybe direct to a function based on filetype?
13+
let g:lightline['component'] = {
14+
\ 'sharpenup': sharpenup#statusline#Build()
15+
\}
16+
17+
augroup setupOmniSharpLightlineIntegration
18+
autocmd!
19+
autocmd User OmniSharpProjectUpdated,OmniSharpReady call lightline#update()
20+
augroup END
21+
endif
22+
23+
" [re]defines :Make command, with autocomplete to a csproj in the sln
24+
command! -bang -nargs=* -bar -complete=custom,s:DotNetFileComplete Make AsyncRun -program=make @ <args>
25+
26+
" Find relevant .csproj files to populate autocomplete list
27+
" See `:help command-completion-custom`
28+
function! s:DotNetFileComplete(A,L,P)
29+
let searchdir = expand('%:.:h')
30+
let matches = ''
31+
" If we're not relative to the cwd (e.g. in :help), don't try to search
32+
if fnamemodify(searchdir,':p:h') !=? searchdir
33+
let host = OmniSharp#GetHost(bufnr('%'))
34+
let csprojs = deepcopy(host.job.projects)
35+
let csprojs_relative = map(csprojs, {index, value -> fnamemodify(value['path'], ':.')})
36+
if has_key(host, 'project')
37+
" Make the first project this file is in first in the sln list
38+
let project = fnamemodify(host['project']['MsBuildProject']['Path'], ':.')
39+
let i = index(csprojs_relative, project)
40+
call remove(csprojs_relative, i)
41+
let matches = join(insert(csprojs_relative, project), "\n")
42+
else
43+
let matches = join(csprojs_relative, "\n")
44+
endif
45+
endif
46+
return matches
47+
endfunction
48+
49+
command! -buffer -bar OmniSharpBuildProject call s:OmniSharpBuildProject()
50+
command! -buffer -bar OmniSharpBuildSolution call s:OmniSharpBuildSolution()
51+
nnoremap <buffer> <Plug>(omnisharp_build_project) :OmniSharpBuildProject<CR>
52+
nnoremap <buffer> <Plug>(omnisharp_build_solution) :OmniSharpBuildSolution<CR>
53+
54+
function! s:OmniSharpBuildProject() abort
55+
let host = OmniSharp#GetHost(bufnr('%'))
56+
let CsprojF = {->fnamemodify(host['project']['MsBuildProject']['Path'], ':.')}
57+
if has_key(host, 'project')
58+
call execute('Make '.CsprojF())
59+
else
60+
call OmniSharp#actions#project#Get(bufnr('%'), { -> execute('Make '.CsprojF()) })
61+
endif
62+
endfunction
63+
64+
function! s:OmniSharpBuildSolution() abort
65+
let sln = OmniSharp#GetHost(bufnr('%')).sln_or_dir
66+
call execute('Make '.sln)
67+
endfunction

layers/+lang/csharp/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# C# Layer
2+
3+
## Table of Contents
4+
5+
<!-- vim-markdown-toc GFM -->
6+
* [Description](#description)
7+
* [Install](#install)
8+
* [Key Bindings](#keybindings)
9+
10+
<!-- vim-markdown-toc -->
11+
12+
## Description
13+
14+
This layer adds support for the C# language.
15+
16+
## Install
17+
18+
To use this configuration layer, add it to your `.spacevim`.
19+
20+
## Key Bindings
21+
22+
Here are most/all:
23+
24+
```vim
25+
let g:OmniSharp_popup_mappings = {
26+
\ 'close': ['<Esc>', 'gq']
27+
\ 'lineDown': '<C-e>',
28+
\ 'lineUp': '<C-y>',
29+
\ 'halfPageDown': '<C-d>',
30+
\ 'halfPageUp': '<C-u>',
31+
\ 'pageDown': ['<C-f>', '<PageDown>'],
32+
\ 'pageUp': ['<C-b>', '<PageUp>']
33+
\ 'sigNext': '<C-j>',
34+
\ 'sigPrev': '<C-k>',
35+
\ 'sigParamNext': '<C-l>',
36+
\ 'sigParamPrev': '<C-h>'
37+
\}
38+
39+
nmap <C-\> <Plug>(omnisharp_signature_help)
40+
imap <C-\> <Plug>(omnisharp_signature_help)
41+
nmap <silent> <buffer> [[ <Plug>(omnisharp_navigate_up)
42+
nmap <silent> <buffer> ]] <Plug>(omnisharp_navigate_down)
43+
"nmap <prefix>ca <Plug>(omnisharp_code_actions)
44+
"xmap <prefix>ca <Plug>(omnisharp_code_actions)
45+
"nmap <silent> <buffer> <prefix>. <Plug>(omnisharp_code_action_repeat)
46+
"xmap <silent> <buffer> <prefix>. <Plug>(omnisharp_code_action_repeat)
47+
```
48+
49+
The remainder are integrated with *vim-which-key* in the `<leader>/+lsp` category.
50+
51+
See [OmniSharp/omnisharp-vim](https://github.com/OmniSharp/omnisharp-vim) for some bindings documentation. See `:help omnisharp`.
52+
[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*.

layers/+lang/csharp/config.vim

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
" OmniSharp/omnisharp-vim: {
2+
let g:OmniSharp_popup_position = winwidth(0) >= 80 ? 'peek' : 'center'
3+
if has('nvim')
4+
let g:OmniSharp_popup_options = {
5+
\ 'winhl': 'Normal:NormalFloat'
6+
\}
7+
else
8+
let g:OmniSharp_popup_options = {
9+
\ 'highlight': 'Normal',
10+
\ 'padding': [0, 0, 0, 0],
11+
\ 'border': [1]
12+
\}
13+
endif
14+
let g:OmniSharp_popup_mappings = {
15+
\ 'pageDown': ['<C-f>', '<PageDown>'],
16+
\ 'pageUp': ['<C-b>', '<PageUp>']
17+
\}
18+
19+
"if s:using_snippets
20+
" let g:OmniSharp_want_snippet = 1
21+
"endif
22+
23+
let g:OmniSharp_highlight_groups = {
24+
\ 'ExcludedCode': 'NonText'
25+
\}
26+
"\ 'Comment': 'NonText',
27+
"\ 'XmlDocCommentName': 'Identifier',
28+
"\ 'XmlDocCommentText': 'NonText'
29+
"\}
30+
31+
if spacevim#load('clap')
32+
let g:OmniSharp_selector_findusages = 'clap'
33+
endif
34+
let g:OmniSharp_autoselect_existing_sln = 1
35+
let g:OmniSharp_highlighting = 3
36+
let g:OmniSharp_diagnostic_exclude_paths = [ 'Temp[/\\]', 'obj[/\\]', '\.nuget[/\\]' ]
37+
"let g:OmniSharp_fzf_options = { 'down': '10' }']]]' ]
38+
39+
" The following autoformats the code on write per OmniSharp
40+
" configuration when g:OmniSharp_code_format is set.
41+
" Each codebase might have different configuration.
42+
" Some codebases might be messy, where autoformat is a burden in making patches, thus this is false by default.
43+
" https://github.com/OmniSharp/omnisharp-roslyn/wiki/Configuration-Options
44+
let g:OmniSharp_code_format = v:false
45+
augroup codeformatOmniSharp
46+
autocmd!
47+
autocmd BufWritePre *.cs :if g:OmniSharp_code_format | :OmniSharpCodeFormat | :endif
48+
augroup END
49+
50+
" Cache the 'project' data from OmniSharp-Roslyn to each cs buffer
51+
augroup cacheOmniSharpProject
52+
autocmd!
53+
autocmd FileType cs call s:CacheOmniSharpProjectInBuffer({->0})
54+
augroup END
55+
56+
function! s:CacheOmniSharpProjectInBuffer(callback)
57+
if &filetype ==# 'cs'
58+
let bufnr = bufnr('%')
59+
let host = OmniSharp#GetHost(bufnr)
60+
if !has_key(host, 'project')
61+
" 'project' not in cache, must query from OmniSharp-Roslyn, is async
62+
call OmniSharp#actions#project#Get(bufnr, a:callback)
63+
endif
64+
endif
65+
endfunction
66+
67+
if spacevim#load('programming')
68+
if exists('g:vista_executive_for') && v:t_dict == type(g:vista_executive_for)
69+
call extend(g:vista_executive_for, {'cs': 'ctags'})
70+
else
71+
let g:vista_executive_for = {'cs': 'ctags'}
72+
endif
73+
endif
74+
" }
75+
76+
" nickspoons/vim-sharpenup: {
77+
" We want everything from vim-sharpenup but the keybindings due to integration
78+
let g:sharpenup_create_mappings = 0
79+
let g:sharpenup_map_legacy_csproj_actions = 0
80+
81+
" TODO: integrate mappings better:
82+
nmap <C-\> <Plug>(omnisharp_signature_help)
83+
imap <C-\> <Plug>(omnisharp_signature_help)
84+
nmap <silent> <buffer> [[ <Plug>(omnisharp_navigate_up)
85+
nmap <silent> <buffer> ]] <Plug>(omnisharp_navigate_down)
86+
"nmap <prefix>ca <Plug>(omnisharp_code_actions)
87+
"xmap <prefix>ca <Plug>(omnisharp_code_actions)
88+
"nmap <silent> <buffer> <prefix>. <Plug>(omnisharp_code_action_repeat)
89+
"xmap <silent> <buffer> <prefix>. <Plug>(omnisharp_code_action_repeat)
90+
91+
" A `Dictionary-function` containing vim-which-key mappings
92+
function! s:whichkey_omnisharp_integration()
93+
if &filetype ==# 'cs'
94+
let s:new_keymap = get(s:, 'new_keymap', {
95+
\ 'name': '+omnisharp',
96+
\ '.': ['<Plug>(omnisharp_code_action_repeat)', 'repeat'],
97+
\ 'a': ['<Plug>(omnisharp_code_actions)' , 'code-action'],
98+
\ 'c': ['<Plug>(omnisharp_global_code_check)' , 'global-code-check'],
99+
\ 'f': ['<Plug>(omnisharp_code_format)' , 'formatting'],
100+
\ 'h': ['<Plug>(omnisharp_signature_help)' , 'hover'],
101+
\ 'H': ['<Plug>(omnisharp_highlight)' , 'highlight'],
102+
\ 'r': ['<Plug>(omnisharp_find_usages)' , 'references'],
103+
\ 'R': ['<Plug>(omnisharp_rename)' , 'rename'],
104+
\ 's': ['<Plug>(omnisharp_documentation)' , 'document-symbol'],
105+
\ 'S': ['<Plug>(omnisharp_find_symbol)' , 'workspace-symbol'],
106+
\ 'g': {
107+
\ 'name': '+goto',
108+
\ 'd': ['<Plug>(omnisharp_go_to_definition)', 'definition'],
109+
\ 't': ['<Plug>(omnisharp_find_type)' , 'type-definition'],
110+
\ 'i': ['<Plug>(omnisharp_find_implementations)', 'implementation'],
111+
\ 's': ['<Plug>(omnisharp_find_symbol)' , 'symbol'],
112+
\ },
113+
\ 'p': {
114+
\ 'name': '+preview',
115+
\ 'd': ['<Plug>(omnisharp_preview_definition)' , 'definition'],
116+
\ 'i': ['<Plug>(omnisharp_preview_implementations)', 'implementation'],
117+
\ },
118+
\ 'b': {
119+
\ 'name': '+build',
120+
\ 'p': ['<Plug>(omnisharp_build_project)' , 'build-project'],
121+
\ 's': ['<Plug>(omnisharp_build_solution)' , 'build-solution'],
122+
\ 'd': ['<Plug>(omnisharp_debug_project)' , 'debug-project'],
123+
\ 'v': ['<Plug>(omnisharp_create_debug_config)', 'create-vimspector-config'],
124+
\ },
125+
\ 't': {
126+
\ 'name': '+test',
127+
\ 't': ['<Plug>(omnisharp_run_test)' , 'run-test'],
128+
\ 'n': ['<Plug>(omnisharp_run_test_no_build)', 'run-test-no-build'],
129+
\ 'T': ['<Plug>(omnisharp_run_tests_in_file)', 'run-tests-in-file'],
130+
\ 'N': ['<Plug>(omnisharp_run_tests_in_file_no_build)', 'run-tests-in-file-no-build'],
131+
\ 'd': ['<Plug>(omnisharp_debug_test)' , 'debug-test'],
132+
\ 'D': ['<Plug>(omnisharp_debug_test_no_build)', 'debug-test-no-build'],
133+
\ },
134+
\ 'x': {
135+
\ 'name': '+csproj-modify',
136+
\ 'a': ['<Plug>(omnisharp_add_to_csproj)' , 'add-to-csproj'],
137+
\ 'r': ['<Plug>(omnisharp_rename_in_csproj)', 'rename-in-csproj'],
138+
\ },
139+
\ 'X': {
140+
\ 'name': '+server',
141+
\ 's': [':OmniSharpStatus!' , 'status'],
142+
\ 'S': ['<Plug>(omnisharp_start_server)' , 'start'],
143+
\ 't': ['<Plug>(omnisharp_stop_server)' , 'stop'],
144+
\ 'T': ['<Plug>(omnisharp_stop_all_servers)' , 'stop-all'],
145+
\ 'r': ['<Plug>(omnisharp_restart_server)' , 'restart'],
146+
\ 'R': ['<Plug>(omnisharp_restart_all_servers)', 'restart-all'],
147+
\ },
148+
\ })
149+
return s:new_keymap
150+
else
151+
return s:keep_keymap
152+
endif
153+
endfunction
154+
155+
" Integrate OmniSharp hotkeys with vim-which-key
156+
if spacevim#load('which-key')
157+
let s:keep_keymap = get(s:, 'keep_keymap', deepcopy(g:spacevim#map#leader#desc['l']))
158+
let g:spacevim#map#leader#desc['l'] = function('s:whichkey_omnisharp_integration')
159+
endif
160+
" }
161+
162+
" mattn/vim-lsp-settings: {
163+
if exists('g:spacevim_lsp_engine') && g:spacevim_lsp_engine ==# 'vim_lsp'
164+
let g:lsp_settings = get(g:, 'lsp_settings', {})
165+
if !has_key(g:lsp_settings, 'omnisharp-lsp')
166+
let g:lsp_settings['omnisharp-lsp'] = {}
167+
endif
168+
let g:lsp_settings['omnisharp-lsp'].disabled = 1
169+
endif
170+
" }
171+
172+
" itchyny/lightline.vim: {
173+
let g:sharpenup_statusline_opts = { 'Text': '%s (%p/%P)' }
174+
let g:sharpenup_statusline_opts.Highlight = 0
175+
" }
176+
177+
" As alternative to OmniSharpCodeFormat, can use different formatter
178+
" vim-autoformat/vim-autoformat: {
179+
"let g:formatdef_my_custom_cs = '"astyle --mode=cs --style=ansi -pcHs".&shiftwidth'
180+
" }

layers/+lang/csharp/packages.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
MP 'OmniSharp/omnisharp-vim', { 'on_ft' : 'cs' }
2+
MP 'nickspoons/vim-sharpenup', { 'on_ft' : 'cs' }
3+
MP 'nickspoons/vim-cs', { 'on_ft' : 'cs' }

layers/+programming/programming/config.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ augroup END
8383

8484
" vim-polyglot {
8585
" Reset errorformat to its default value for cooperating with asyncrun.vim
86-
autocmd BufEnter * set errorformat&
86+
" BUG: Prevents other plugins from including CompilerSets
87+
"autocmd BufEnter * set errorformat&
8788
" }
8889

8990
" vim-rooter {

layers/LAYERS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Layer Manifest
22
==============
33

4-
Last updated: 2022-09-29 12:26:10
4+
Last updated: 2022-09-29 12:39:45
55

66
Default layers: `fzf`, `better-defaults` and `which-key`.
77

@@ -19,6 +19,7 @@ Topic | Layer | Plugins
1919
+fun | [goyo](https://github.com/liuchengxu/space-vim/tree/master/layers/+fun/goyo) | <ul><li>[junegunn/goyo.vim](https://github.com/junegunn/goyo.vim)</li><li>[junegunn/limelight.vim](https://github.com/junegunn/limelight.vim)</li></ul>
2020
+lang | [c-c++](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/c-c++) | <ul><li>[octol/vim-cpp-enhanced-highlight](https://github.com/octol/vim-cpp-enhanced-highlight)</li><li>[rhysd/vim-clang-format](https://github.com/rhysd/vim-clang-format)</li></ul>
2121
+lang | [clojure](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/clojure) | <ul><li>[guns/vim-clojure-highlight](https://github.com/guns/vim-clojure-highlight)</li><li>[guns/vim-clojure-static](https://github.com/guns/vim-clojure-static)</li><li>[guns/vim-slamhound](https://github.com/guns/vim-slamhound)</li><li>[kovisoft/paredit](https://github.com/kovisoft/paredit)</li><li>[tpope/vim-fireplace](https://github.com/tpope/vim-fireplace)</li><li>[venantius/vim-cljfmt](https://github.com/venantius/vim-cljfmt)</li></ul>
22+
+lang | [csharp](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/csharp) | <ul><li>[OmniSharp/omnisharp-vim](https://github.com/OmniSharp/omnisharp-vim)</li><li>[nickspoons/vim-cs](https://github.com/nickspoons/vim-cs)</li><li>[nickspoons/vim-sharpenup](https://github.com/nickspoons/vim-sharpenup)</li></ul>
2223
+lang | [elixir](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/elixir) | <ul><li>[elixir-lang/vim-elixir](https://github.com/elixir-lang/vim-elixir)</li><li>[slashmili/alchemist.vim](https://github.com/slashmili/alchemist.vim)</li></ul>
2324
+lang | [elm](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/elm) | <ul><li>[ElmCast/elm-vim](https://github.com/ElmCast/elm-vim)</li></ul>
2425
+lang | [erlang](https://github.com/liuchengxu/space-vim/tree/master/layers/+lang/erlang) | <ul><li>[vim-erlang/erlang-motions.vim](https://github.com/vim-erlang/erlang-motions.vim)</li><li>[vim-erlang/vim-erlang-compiler](https://github.com/vim-erlang/vim-erlang-compiler)</li><li>[vim-erlang/vim-erlang-omnicomplete](https://github.com/vim-erlang/vim-erlang-omnicomplete)</li><li>[vim-erlang/vim-erlang-skeletons](https://github.com/vim-erlang/vim-erlang-skeletons)</li><li>[vim-erlang/vim-erlang-tags](https://github.com/vim-erlang/vim-erlang-tags)</li></ul>

0 commit comments

Comments
 (0)