2018-09-12 20:14:20 +08:00
|
|
|
---
|
|
|
|
title: "SpaceVim format layer"
|
2020-09-09 18:27:37 +08:00
|
|
|
description: "Code formatting layer for SpaceVim, includes a variety of formatters for many filetypes"
|
2018-09-12 20:14:20 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
# [Available Layers](../) >> format
|
|
|
|
|
|
|
|
<!-- vim-markdown-toc GFM -->
|
|
|
|
|
|
|
|
- [Description](#description)
|
|
|
|
- [Install](#install)
|
|
|
|
- [Configuration](#configuration)
|
2020-09-01 17:34:49 +08:00
|
|
|
- [Layer options](#layer-options)
|
|
|
|
- [Global options](#global-options)
|
2020-10-04 23:02:30 +08:00
|
|
|
- [Key bindings](#key-bindings)
|
2018-09-12 20:14:20 +08:00
|
|
|
|
|
|
|
<!-- vim-markdown-toc -->
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
2021-10-23 14:50:09 +08:00
|
|
|
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.
|
|
|
|
|
2018-09-12 20:14:20 +08:00
|
|
|
|
|
|
|
## Install
|
|
|
|
|
2021-06-21 20:39:29 +08:00
|
|
|
This layer is enabled by default. If you want to disable it, add the following to your configuration file:
|
2018-09-12 20:14:20 +08:00
|
|
|
|
|
|
|
```toml
|
|
|
|
[[layers]]
|
2020-04-28 23:25:13 +08:00
|
|
|
name = "format"
|
2018-09-12 20:14:20 +08:00
|
|
|
enable = false
|
|
|
|
```
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
2020-09-01 17:34:49 +08:00
|
|
|
### Layer options
|
|
|
|
|
2021-10-23 14:50:09 +08:00
|
|
|
- **`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,
|
|
|
|
|
2020-09-01 17:34:49 +08:00
|
|
|
and it is disabled by default. To enable it:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[[layers]]
|
|
|
|
name = "format"
|
|
|
|
format_on_save = true
|
|
|
|
```
|
|
|
|
|
2021-06-21 20:39:29 +08:00
|
|
|
This option can be overrided by `format_on_save` in the language layer. For example, enable `format_on_save`
|
|
|
|
for all filetypes except python.
|
2020-09-01 17:34:49 +08:00
|
|
|
|
|
|
|
```toml
|
|
|
|
# enable format layer
|
|
|
|
[[layers]]
|
|
|
|
name = 'format'
|
|
|
|
format_on_save = true
|
|
|
|
# enable lang#java layer
|
|
|
|
[[layers]]
|
|
|
|
name = 'lang#python'
|
|
|
|
format_on_save = false
|
|
|
|
```
|
|
|
|
|
2022-02-08 23:11:57 +08:00
|
|
|
- **`silent_format`**: Setting this to true will run the formatter silently without any messages. Default is
|
|
|
|
disabled.
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[[layers]]
|
|
|
|
name = "format"
|
|
|
|
silent_format = true
|
|
|
|
```
|
|
|
|
|
2020-09-01 17:34:49 +08:00
|
|
|
### Global options
|
|
|
|
|
2021-06-21 20:39:29 +08:00
|
|
|
neoformat is a formatting framework, all of it's options can be used in bootstrap function. You can read
|
2020-09-01 17:34:49 +08:00
|
|
|
`:help neoformat` for more info.
|
|
|
|
|
2021-10-23 14:50:09 +08:00
|
|
|
|
|
|
|
Here is an example for add formatter for java file, and it has been included into `lang#java` layer:
|
|
|
|
|
2020-09-01 17:34:49 +08:00
|
|
|
|
|
|
|
```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,
|
|
|
|
\ }
|
|
|
|
```
|
2020-10-04 23:02:30 +08:00
|
|
|
|
|
|
|
## Key bindings
|
|
|
|
|
|
|
|
| Key binding | Description |
|
|
|
|
| ----------- | ------------------------------------- |
|
|
|
|
| `SPC b f` | format whole buffer or selected lines |
|