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

Add format_on_save options (#3746)

This commit is contained in:
Wang Shidong 2020-09-01 17:34:49 +08:00 committed by GitHub
parent 3422243c8a
commit b0fd3f8c1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 211 additions and 53 deletions

View File

@ -9,23 +9,59 @@
""
" @section format, layer-format
" @parentsection layers
" SpaceVim uses neoformat as the default code format tools. Neoformat uses a
" variety of formatters for many filetypes. for more info see |neoformat|
" if you want to run a formatter on save, just put this config into bootstrap
" function.
" >
" augroup fmt
" autocmd!
" autocmd BufWritePre * undojoin | Neoformat
" augroup END
" <
" format layer provides code formation for SpaceVim, the default formatting
" plugin is |neoformat|.
" @subsection options
" format_on_save: disabled by default.
"
"
if exists('s:format_on_save')
finish
else
let s:format_on_save = 0
let s:format_ft = []
endif
function! SpaceVim#layers#format#plugins() abort
return [
\ [g:_spacevim_root_dir . 'bundle/neoformat', {'merged' : 0, 'loadconf' : 1 , 'loadconf_before' : 1}],
\ ]
return [
\ [g:_spacevim_root_dir . 'bundle/neoformat', {'merged' : 0, 'loadconf' : 1 , 'loadconf_before' : 1}],
\ ]
endfunction
function! SpaceVim#layers#format#config() abort
call SpaceVim#mapping#space#def('nnoremap', ['b', 'f'], 'Neoformat', 'format-code', 1)
augroup spacevim_layer_format
autocmd!
autocmd BufWritePre * call s:format()
augroup END
endfunction
function! SpaceVim#layers#format#set_variable(var) abort
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']
endfunction
function! SpaceVim#layers#format#add_filetype(ft) abort
if get(a:ft, 'enable', 0)
if index(s:format_ft, a:ft.filetype) ==# -1
call add(s:format_ft, a:ft.filetype)
endif
else
if index(s:format_ft, a:ft.filetype) !=# -1
call remove(s:format_ft, a:ft.filetype)
endif
endif
endfunction
function! s:format() abort
if !empty(&ft) &&
\ ( index(s:format_ft, &ft) !=# -1 || s:format_on_save ==# 1)
undojoin | Neoformat
endif
endfunction

View File

@ -12,7 +12,7 @@
" This layer provides syntax highlighting for actionscript. To enable this
" layer:
" >
" [layers]
" [[layers]]
" name = "lang#actionscript"
" <

View File

@ -138,10 +138,10 @@ function! s:language_specified_mappings() abort
" Format on save
if s:format_on_save
augroup SpaceVim_layer_lang_python
autocmd!
autocmd BufWritePre *.py undojoin | Neoformat
augroup end
call SpaceVim#layers#format#add_filetype({
\ 'filetype' : 'python',
\ 'enable' : 1,
\ })
endif
endfunction

View File

@ -28,6 +28,20 @@
" ''
" ]
" <
" 2. ruby_repl_command: the REPL command for ruby
" >
" [[layers]]
" name = 'lang#ruby'
" ruby_repl_command = '~/download/bin/ruby_repl'
" <
" 3. format_on_save: enable/disable code formation when save ruby file. This
" options is disabled by default, to enable it:
" >
" [[layers]]
" name = 'lang#ruby'
" ruby_repl_command = '~/download/bin/ruby_repl'
" format_on_save = true
" <
" @subsection Key bindings
"
" >
@ -47,13 +61,26 @@
" <
"
if exists('s:ruby_file_head')
finish
else
let s:ruby_repl_command = ''
let s:ruby_file_head = [
\ '#!/usr/bin/ruby -w',
\ '# -*- coding : utf-8 -*-',
\ ''
\ ]
let s:format_on_save = 0
endif
function! SpaceVim#layers#lang#ruby#plugins() abort
return [
\ ['vim-ruby/vim-ruby', { 'on_ft' : 'ruby' }]
\ ]
endfunction
let s:ruby_repl_command = ''
function! SpaceVim#layers#lang#ruby#config() abort
call SpaceVim#plugins#runner#reg_runner('ruby', {
@ -70,15 +97,10 @@ function! SpaceVim#layers#lang#ruby#config() abort
endif
endfunction
let s:ruby_file_head = [
\ '#!/usr/bin/ruby -w',
\ '# -*- coding : utf-8 -*-',
\ ''
\ ]
function! SpaceVim#layers#lang#ruby#set_variable(var) abort
let s:ruby_repl_command = get(a:var, 'repl_command', '')
let s:ruby_file_head = get(a:var, 'ruby-file-head', s:ruby_file_head)
let s:format_on_save = get(a:var, 'format_on_save', s:format_on_save)
endfunction
function! s:language_specified_mappings() abort

View File

@ -100,12 +100,13 @@ function! SpaceVim#layers#lang#rust#config() abort
" Disable racer format, use Neoformat instead!
let g:rustfmt_autosave = 0
if s:format_on_save
augroup SpaceVim_layer_lang_rust
autocmd!
autocmd BufWritePre *.rs undojoin | Neoformat
augroup end
call SpaceVim#layers#format#add_filetype({
\ 'filetype' : 'rust',
\ 'enable' : 1,
\ })
endif
call SpaceVim#mapping#space#regesit_lang_mappings('rust', function('s:language_specified_mappings'))
call add(g:spacevim_project_rooter_patterns, 'Cargo.toml')
@ -207,11 +208,11 @@ function! s:execCMD(cmd) abort
endfunction
"
"#用于更新 toolchain
" toolchain
"
" set RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
"
" #用于更新 rustup
" rustup
"
" set RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
"

View File

@ -1388,15 +1388,12 @@ Mappings:
==============================================================================
FORMAT *SpaceVim-layer-format*
SpaceVim uses neoformat as the default code format tools. Neoformat uses a
variety of formatters for many filetypes. for more info see |neoformat| if you
want to run a formatter on save, just put this config into bootstrap function.
>
augroup fmt
autocmd!
autocmd BufWritePre * undojoin | Neoformat
augroup END
<
format layer provides code formation for SpaceVim, the default formatting
plugin is |neoformat|.
OPTIONS
format_on_save: disabled by default.
==============================================================================
GITHUB *SpaceVim-layer-github*
@ -1468,7 +1465,7 @@ LANG#ACTIONSCRIPT *SpaceVim-layer-lang-actionscript*
This layer provides syntax highlighting for actionscript. To enable this
layer:
>
[layers]
[[layers]]
name = "lang#actionscript"
<
@ -2855,6 +2852,21 @@ OPTIONS
'# -*- coding : utf-8 -*-'
''
]
<
2. ruby_repl_command: the REPL command for ruby
>
[[layers]]
name = 'lang#ruby'
ruby_repl_command = '~/download/bin/ruby_repl'
<
3. format_on_save: enable/disable code formation when save ruby file. This
options is disabled by default, to enable it:
>
[[layers]]
name = 'lang#ruby'
ruby_repl_command = '~/download/bin/ruby_repl'
format_on_save = true
<
KEY BINDINGS

View File

@ -18,11 +18,12 @@ commentsID: "使用 Vim 搭建基本开发环境"
- [安装](#安装)
- [基本配置](#基本配置)
- [基本使用](#基本使用)
- [文件及窗口操作](#文件及窗口操作)
- [文件及窗口操作](#文件及窗口操作)
- [代码格式化](#代码格式化)
<!-- vim-markdown-toc -->
### 安装
## 安装
在入门指南里,介绍了不同系统安装 SpaceVim 的步骤。在安装过程中还是存在一些问题,比如颜色主题看上去和官网不一致,出现各种字体乱码。
安装 SpaceVim 最理想的环境是 neovim + nerdfont + 一个支持真色的终端模拟器。
@ -38,7 +39,7 @@ commentsID: "使用 Vim 搭建基本开发环境"
{% include bilibilivedio.html id="aid=51967466&cid=90976280&page=1" %}
### 基本配置
## 基本配置
SpaceVim 的配置文件有两种,一种是全局配置文件(`~/.SpaceVim.d/init.toml`)
另外一种是项目专属配置文件,即为项目根目录的配置(`.SpaceVim.d/init.toml`)。
@ -51,7 +52,7 @@ SpaceVim 的配置文件有两种,一种是全局配置文件(`~/.SpaceVim.d/i
{% include bilibilivedio.html id="aid=71915741&cid=124611019&page=1" %}
### 基本使用
## 基本使用
首先,需要了解下 SpaceVim 启动后几个界面元素:顶部标签栏、底部状态栏。
可以看到,顶部标签栏通常只有一个,主要用来列出已经打开的文件或者是标签页,并以序号标记。
@ -64,4 +65,6 @@ SpaceVim 的配置文件有两种,一种是全局配置文件(`~/.SpaceVim.d/i
SpaceVim 会在状态栏展示各个窗口的编号,可以使用快捷键 `SPC + 数字` 快速跳到对应的窗口,在顶部标签栏,会列出当前已经打开的文件或者标签裂变,
可以使用快捷键 `Leader + 数字` 快速跳到对应的文件。在这里默认的 Leader 是 `\` 键。
### 代码格式化
代码格式化这一功能由 `format` 模块提供,默认该模块已启用,手动格式化快捷键为 `SPC b f`

View File

@ -12,23 +12,66 @@ lang: zh
- [模块简介](#模块简介)
- [启用模块](#启用模块)
- [模块设置](#模块设置)
- [模块选项](#模块选项)
- [全局选项](#全局选项)
<!-- vim-markdown-toc -->
## 模块简介
模块为 SpaceVim 提供代码格式化的功能,使用了 Vim8/neovim 的异步特性。引入了插件 [neoformat](https://github.com/sbdchd/neoformat)
`format` 模块为 SpaceVim 提供代码格式化的功能,使用了 Vim8/neovim 的异步特性。引入了插件 [neoformat](https://github.com/sbdchd/neoformat)
## 启用模块
可通过在配置文件内加入如下配置来启用该模块
`format` 模块默认已经启用,如果需要禁用该模块,可以在配置文件中添加如下配置
```toml
[[layers]]
name = "format"
enable = false
```
## 模块设置
neoformat 这一插件为不同语言做了很好的默认配置只需要安装对应的格式化命令即可。当然neoformat 也支持自定义配置。
可以在 SpaceVim 启动函数里设置相关插件选项。
### 模块选项
- **`format_on_save`**: 这一模块选项是用于设置是否在保存文件时自动进行格式化,默认是禁用的,
如果需要启用该功能,可以在设置文件中加入以下内容:
```toml
[[layers]]
name = "format"
format_on_save = true
```
这一选项可以被语言模块中的 `format_on_save` 选项所覆盖。比如,为所有文件类型启用自动格式化,但是 Python
除外:
```toml
# 启用 format 模块
[[layers]]
name = 'format'
format_on_save = true
# 启用 lang#java 模块
[[layers]]
name = 'lang#python'
format_on_save = false
```
### 全局选项
neoformat 是一个格式化框架插件,该插件的所有自身选项可以在启动函数中进行设置,可以预读 `:help neoformat`
获取完整帮助。
以下是一个为 Java 文件设置格式化命令的配置,以下配置已经包含在 `lang#java` 模块内了:
```viml
let g:neoformat_enabled_java = ['googlefmt']
let g:neoformat_java_googlefmt = {
\ 'exe': 'java',
\ 'args': ['-jar', '~/Downloads/google-java-format-1.5-all-deps.jar', '-'],
\ 'stdin': 1,
\ }
```

View File

@ -5,18 +5,20 @@ description: "Code formatting support for SpaceVim"
# [Available Layers](../) >> format
<!-- vim-markdown-toc GFM -->
- [Description](#description)
- [Install](#install)
- [Configuration](#configuration)
- [Layer options](#layer-options)
- [Global options](#global-options)
<!-- vim-markdown-toc -->
## Description
This layer provides code format feature SpaceVim, and neoformat is included in this layer.
`format` layer provides code formation feature for SpaceVim, this layer includes `neoformat`
as default code formation plugin.
## Install
@ -30,4 +32,43 @@ This layer is enabled by default. If you want to disable this layer, add followi
## Configuration
neoformat provide better default for different languages, but you can also config it in bootstrap function.
### Layer options
- **`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
[[layers]]
name = "format"
format_on_save = true
```
This option can be overrided by `format_on_save` in language layer. For example, enable `format_on_save`
for all filetypes expect python.
```toml
# enable format layer
[[layers]]
name = 'format'
format_on_save = true
# enable lang#java layer
[[layers]]
name = 'lang#python'
format_on_save = false
```
### Global options
neoformat is a format framework, all of it's options can be used in bootstrap function. You can read
`:help neoformat` for more info.
here is an example for add formater for java file, and it has been included into `lang#java` layer:
```viml
let g:neoformat_enabled_java = ['googlefmt']
let g:neoformat_java_googlefmt = {
\ 'exe': 'java',
\ 'args': ['-jar', '~/Downloads/google-java-format-1.5-all-deps.jar', '-'],
\ 'stdin': 1,
\ }
```