1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-02 20:30:05 +08:00

feat(format): add vim-codefmt support

Co-authored-by: Wang Shidong <wsdjeg@outlook.com>
This commit is contained in:
Aron Griffis 2021-10-23 02:50:09 -04:00 committed by GitHub
parent 7f41707436
commit 04e4b3d121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 18 deletions

View File

@ -25,6 +25,7 @@
if exists('s:format_on_save')
finish
else
let s:format_method = 'neoformat'
let s:format_on_save = 0
let s:format_ft = []
endif
@ -36,13 +37,26 @@ function! SpaceVim#layers#format#health() abort
endfunction
function! SpaceVim#layers#format#plugins() abort
return [
\ [g:_spacevim_root_dir . 'bundle/neoformat', {'merged' : 0, 'loadconf' : 1 , 'loadconf_before' : 1}],
\ ]
if s:format_method ==# 'neoformat'
return [
\ [g:_spacevim_root_dir . 'bundle/neoformat', {'merged' : 0, 'loadconf' : 1, 'loadconf_before' : 1}],
\ ]
elseif s:format_method ==# 'codefmt'
return [
\ ['google/vim-maktaba', {'merged' : 0}],
\ ['google/vim-glaive', {'merged' : 0, 'loadconf' : 1}],
\ ['google/vim-codefmt', {'merged' : 0}],
\ ]
endif
endfunction
function! SpaceVim#layers#format#config() abort
call SpaceVim#mapping#space#def('nnoremap', ['b', 'f'], ":Neoformat\<Enter>", 'format-code', 0, 1)
if s:format_method ==# 'neoformat'
call SpaceVim#mapping#space#def('nnoremap', ['b', 'f'], 'Neoformat', 'format-code', 1)
elseif s:format_method ==# 'codefmt'
call SpaceVim#mapping#space#def('nnoremap', ['b', 'f'], 'FormatCode', 'format-code', 1)
endif
augroup spacevim_layer_format
autocmd!
autocmd BufWritePre * call s:format()
@ -50,13 +64,12 @@ function! SpaceVim#layers#format#config() abort
endfunction
function! SpaceVim#layers#format#set_variable(var) abort
let s:format_method = get(a:var, 'format_method', s:format_method)
let s:format_on_save = get(a:var, 'format_on_save', s:format_on_save)
endfunction
function! SpaceVim#layers#format#get_options() abort
return ['format_on_save']
return ['format_method', 'format_on_save']
endfunction
function! SpaceVim#layers#format#add_filetype(ft) abort
@ -74,12 +87,11 @@ endfunction
function! s:format() abort
if !empty(&ft) &&
\ ( index(s:format_ft, &ft) !=# -1 || s:format_on_save ==# 1)
try
undojoin
Neoformat
catch /^Vim\%((\a\+)\)\=:E790/
finally
silent Neoformat
endtry
if s:format_method ==# 'neoformat'
undojoin | Neoformat
elseif s:format_method ==# 'codefmt'
undojoin | FormatCode
endif
endif
endfunction

View File

@ -0,0 +1 @@
call glaive#Install()

View File

@ -18,8 +18,11 @@ description: "Code formatting layer for SpaceVim, includes a variety of formatte
## Description
`format` layer provides code formatting feature for SpaceVim, this layer includes `neoformat`
as the default code formatting plugin.
The `format` layer provides code formatting for SpaceVim, with support for
[`neoformat`](https://github.com/sbdchd/neoformat) (default) and
[`codefmt`](https://github.com/google/vim-codefmt) underlying code
formatting plugins.
## Install
@ -35,7 +38,16 @@ This layer is enabled by default. If you want to disable it, add the following t
### Layer options
- **`format_on_save`**: This option is to enable/disable code formatting when save current buffer,
- **`format_method`**: The default plugin is `neoformat` but can be changed to `codefmt`:
```toml
[[layers]]
name = "format"
format_method = "codefmt"
```
- **`format_on_save`**: This layer option is to enable/disable code formatting when save current buffer,
and it is disabled by default. To enable it:
```toml
@ -63,7 +75,9 @@ This layer is enabled by default. If you want to disable it, add the following t
neoformat is a formatting framework, all of it's options can be used in bootstrap function. You can read
`:help neoformat` for more info.
here is an example to add a formater for java file, and it has been included into `lang#java` layer:
Here is an example for add formatter for java file, and it has been included into `lang#java` layer:
```viml
let g:neoformat_enabled_java = ['googlefmt']