1
1
# Formatter.nvim
2
2
3
- A format runner for ` Neovim ` .
3
+ <div align =" center " >
4
+ <h3 >A format runner for <code >Neovim</code >.</h3 >
5
+ <img src =" asset/splash.gif " alt =" splash " />
6
+ </div >
4
7
5
8
We want to thank the [ ` neoformat ` ] ( https://github.com/sbdchd/neoformat )
6
- contributors for developing a lot of formatter configurations that we used as
9
+ contributors. They developed a lot of formatter configurations that we used as
7
10
a reference to create our own opt-in default formatter configurations.
8
11
9
12
## Features
10
13
11
14
- Written in ` Lua `
12
15
- Asynchronous execution
13
16
- Opt-in default formatter configurations
17
+ - Conditional formatting
14
18
15
19
## Install
16
20
@@ -54,15 +58,25 @@ Setup:
54
58
local util = require " formatter.util"
55
59
56
60
-- Provides the Format and FormatWrite commands
57
- require (' formatter' ).setup {
61
+ require (" formatter" ).setup {
58
62
-- All formatter configurations are opt-in
59
63
filetype = {
64
+ -- Formatter configurations for filetype "lua" go here
65
+ -- and will be executed in order
60
66
lua = {
61
- -- Pick from defaults:
62
- require (' formatter.filetypes.lua' ).stylua ,
67
+ -- "formatter.filetypes.lua" defines default configurations for the
68
+ -- "lua" filetype
69
+ require (" formatter.filetypes.lua" ).stylua ,
63
70
64
- -- ,or define your own:
71
+ -- You can also define your own configuration
65
72
function ()
73
+ -- Supports conditional formatting
74
+ if util .get_current_buffer_file_name () == " special.lua" then
75
+ return nil
76
+ end
77
+
78
+ -- Full specification of configurations is down below and in Vim help
79
+ -- files
66
80
return {
67
81
exe = " stylua" ,
68
82
args = {
@@ -75,22 +89,35 @@ require('formatter').setup {
75
89
stdin = true ,
76
90
}
77
91
end
92
+ },
93
+
94
+ -- Use the special "*" filetype for defining formatter configurations on
95
+ -- any filetype
96
+ [" *" ] = {
97
+ -- "formatter.filetypes.any" defines default configurations for any
98
+ -- filetype
99
+ require (" formatter.filetypes.any" ).remove_trailing_whitespace
78
100
}
79
101
}
80
102
}
81
103
```
82
104
83
- By default, there are no preconfigured formatters, however there are opt-in
84
- [ default configurations per ` filetype ` ] ( lua/formatter/filetypes )
85
- and [ default configurations per formatter] ( lua/formatter/defaults )
86
- as shown in the snippet above. It is hard to predict what everyone wants, but
105
+ ### Opt-in formatters
106
+
107
+ By default, there are no preconfigured formatters. You can opt-into
108
+ [ default configurations per formatter] ( lua/formatter/defaults ) ,
109
+ [ default configurations per ` filetype ` ] ( lua/formatter/filetypes ) , and
110
+ [ default configurations for any ` filetype ` ] ( lua/formatter/filetypes/any.lua )
111
+ or write your own. It is hard to predict what everyone wants, but
87
112
at the same time we realize that most formatter configurations are the same.
88
113
See the discussion in
89
114
[ #97 ] ( https://github.com/mhartington/formatter.nvim/issues/97 ) for more
90
115
information.
91
116
92
- You can use the [ default configurations per ` filetype ` ] ( lua/formatter/filetypes )
93
- and [ default configurations per formatter] ( lua/formatter/defaults )
117
+ You can use the
118
+ [ default configurations per formatter] ( lua/formatter/defaults ) ,
119
+ [ default configurations per ` filetype ` ] ( lua/formatter/filetypes ) , and
120
+ [ default configurations for any ` filetype ` ] ( lua/formatter/filetypes/any.lua )
94
121
as a starting point for creating your configurations.
95
122
Feel free to contribute to this repository by creating or improving default
96
123
configurations that everyone can use! The guide for contributing to default
@@ -99,15 +126,15 @@ configurations is below.
99
126
You can use the [ ` util ` module] ( lua/formatter/util ) which has various
100
127
functions that help with creating default configurations as shown above.
101
128
102
- <!-- TODO: with lua callbacks -->
129
+ ### Map keys
103
130
104
- Map keys:
105
131
``` vim
106
132
nnoremap <silent> <leader>f :Format<CR>
107
133
nnoremap <silent> <leader>F :FormatWrite<CR>
108
134
```
109
135
110
- Format and write after save asynchronously:
136
+ ### Format after save
137
+
111
138
``` vim
112
139
augroup FormatAutogroup
113
140
autocmd!
@@ -165,39 +192,4 @@ the path to the formatted file as a named argument. For an example, check the
165
192
166
193
## Contribute
167
194
168
- <!-- TODO: general contribution guide? -->
169
-
170
- ### Default configurations
171
-
172
- All default configurations are placed in the
173
- [ default configurations directory] ( lua/formatter/filetypes ) and are grouped by
174
- ` filetype ` .
175
- You should use the [ ` util ` module] ( lua/formatter/util )
176
- which has various functions that help with creating default configurations.
177
-
178
- For example, the default configuration of the ` prettier ` formatter for the
179
- ` javascript ` ` filetype ` would be placed in
180
- ` lua/formatter/filetypes/javascript.lua ` as such:
181
-
182
- ``` lua
183
- local M = {}
184
-
185
- local util = require (" formatter.util" )
186
-
187
- -- other formatters...
188
-
189
- function M .prettier ()
190
- return {
191
- exe = " prettier" ,
192
- args = {
193
- " --stdin-filepath" ,
194
- util .escape_path (util .get_current_buffer_file_path ()),
195
- },
196
- stdin = true ,
197
- }
198
- end
199
-
200
- -- other formatters...
201
-
202
- return M
203
- ```
195
+ Refer to the [ CONTRIBUTING.md] ( CONTRIBUTING.md ) file for more information.
0 commit comments