mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-03 00:50:05 +08:00
feat(format): add vim-codefmt support
Co-authored-by: Wang Shidong <wsdjeg@outlook.com>
This commit is contained in:
parent
7f41707436
commit
04e4b3d121
@ -25,6 +25,7 @@
|
|||||||
if exists('s:format_on_save')
|
if exists('s:format_on_save')
|
||||||
finish
|
finish
|
||||||
else
|
else
|
||||||
|
let s:format_method = 'neoformat'
|
||||||
let s:format_on_save = 0
|
let s:format_on_save = 0
|
||||||
let s:format_ft = []
|
let s:format_ft = []
|
||||||
endif
|
endif
|
||||||
@ -36,13 +37,26 @@ function! SpaceVim#layers#format#health() abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! SpaceVim#layers#format#plugins() abort
|
function! SpaceVim#layers#format#plugins() abort
|
||||||
|
if s:format_method ==# 'neoformat'
|
||||||
return [
|
return [
|
||||||
\ [g:_spacevim_root_dir . 'bundle/neoformat', {'merged' : 0, 'loadconf' : 1 , 'loadconf_before' : 1}],
|
\ [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
|
endfunction
|
||||||
|
|
||||||
function! SpaceVim#layers#format#config() abort
|
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
|
augroup spacevim_layer_format
|
||||||
autocmd!
|
autocmd!
|
||||||
autocmd BufWritePre * call s:format()
|
autocmd BufWritePre * call s:format()
|
||||||
@ -50,13 +64,12 @@ function! SpaceVim#layers#format#config() abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! SpaceVim#layers#format#set_variable(var) abort
|
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)
|
let s:format_on_save = get(a:var, 'format_on_save', s:format_on_save)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! SpaceVim#layers#format#get_options() abort
|
function! SpaceVim#layers#format#get_options() abort
|
||||||
|
return ['format_method', 'format_on_save']
|
||||||
return ['format_on_save']
|
|
||||||
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! SpaceVim#layers#format#add_filetype(ft) abort
|
function! SpaceVim#layers#format#add_filetype(ft) abort
|
||||||
@ -74,12 +87,11 @@ endfunction
|
|||||||
function! s:format() abort
|
function! s:format() abort
|
||||||
if !empty(&ft) &&
|
if !empty(&ft) &&
|
||||||
\ ( index(s:format_ft, &ft) !=# -1 || s:format_on_save ==# 1)
|
\ ( index(s:format_ft, &ft) !=# -1 || s:format_on_save ==# 1)
|
||||||
try
|
|
||||||
undojoin
|
if s:format_method ==# 'neoformat'
|
||||||
Neoformat
|
undojoin | Neoformat
|
||||||
catch /^Vim\%((\a\+)\)\=:E790/
|
elseif s:format_method ==# 'codefmt'
|
||||||
finally
|
undojoin | FormatCode
|
||||||
silent Neoformat
|
endif
|
||||||
endtry
|
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
1
config/plugins/vim-glaive.vim
Normal file
1
config/plugins/vim-glaive.vim
Normal file
@ -0,0 +1 @@
|
|||||||
|
call glaive#Install()
|
@ -18,8 +18,11 @@ description: "Code formatting layer for SpaceVim, includes a variety of formatte
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
`format` layer provides code formatting feature for SpaceVim, this layer includes `neoformat`
|
The `format` layer provides code formatting for SpaceVim, with support for
|
||||||
as the default code formatting plugin.
|
[`neoformat`](https://github.com/sbdchd/neoformat) (default) and
|
||||||
|
[`codefmt`](https://github.com/google/vim-codefmt) underlying code
|
||||||
|
formatting plugins.
|
||||||
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
@ -35,7 +38,16 @@ This layer is enabled by default. If you want to disable it, add the following t
|
|||||||
|
|
||||||
### Layer options
|
### 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:
|
and it is disabled by default. To enable it:
|
||||||
|
|
||||||
```toml
|
```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
|
neoformat is a formatting framework, all of it's options can be used in bootstrap function. You can read
|
||||||
`:help neoformat` for more info.
|
`: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
|
```viml
|
||||||
let g:neoformat_enabled_java = ['googlefmt']
|
let g:neoformat_enabled_java = ['googlefmt']
|
||||||
|
Loading…
Reference in New Issue
Block a user