From 04e4b3d1211f18c42ca4951b3ca3a5ca84b2d57a Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Sat, 23 Oct 2021 02:50:09 -0400 Subject: [PATCH] feat(format): add vim-codefmt support Co-authored-by: Wang Shidong --- autoload/SpaceVim/layers/format.vim | 40 +++++++++++++++++++---------- config/plugins/vim-glaive.vim | 1 + docs/layers/format.md | 22 +++++++++++++--- 3 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 config/plugins/vim-glaive.vim diff --git a/autoload/SpaceVim/layers/format.vim b/autoload/SpaceVim/layers/format.vim index 5333a53cd..f420c2633 100644 --- a/autoload/SpaceVim/layers/format.vim +++ b/autoload/SpaceVim/layers/format.vim @@ -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\", '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 diff --git a/config/plugins/vim-glaive.vim b/config/plugins/vim-glaive.vim new file mode 100644 index 000000000..9acd437de --- /dev/null +++ b/config/plugins/vim-glaive.vim @@ -0,0 +1 @@ +call glaive#Install() diff --git a/docs/layers/format.md b/docs/layers/format.md index 6bbdd6c49..aab993f1f 100644 --- a/docs/layers/format.md +++ b/docs/layers/format.md @@ -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']