Skip to content

Commit 465fd2f

Browse files
author
Hrle
authored
Default formatter, styling and fixes (#148)
* add default formatter configs for any filetype, update readme and help, divorce CONTRIBUTING from README, add splash.gif * fix splash typo, complete when no formatter defined * put formatter per filetype logic into config * fix naming and center splash * format * fix complete * remove sd and just use sed * edit splash * Center div * small fixes * add config validation and fix completion * split up util * small fix in config.lua * remove table.lua * fix log message * make tempfile stuff more vim-like * add get_current_buffer_file_extensions * fix complete
1 parent 028cba8 commit 465fd2f

File tree

17 files changed

+623
-354
lines changed

17 files changed

+623
-354
lines changed

.luacheckrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
globals = { "vim", "_", "_FormatterConfigurationValues" }
1+
globals = { "vim", "_", }
22
exclude_files = { "test/*.lua" }
3+
exclude_files = { "asset/*.lua" }

.styluaignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/
2+
asset/

CONTRIBUTING.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Contribute
2+
3+
<!-- TODO: general contribution guide -->
4+
5+
## Default configurations
6+
7+
All default configurations are placed in the
8+
[default configurations per formatter](lua/formatter/defaults),
9+
[default configurations per `filetype`](lua/formatter/filetypes), and
10+
[default configurations for any `filetype`](lua/formatter/filetypes/any.lua).
11+
You should use the [`util` module](lua/formatter/util)
12+
which has various functions that help with creating default configurations.
13+
14+
The [default configurations per formatter](lua/formatter/defaults) return
15+
functions which create a formatter configuration. For example, the `prettier`
16+
default configuration function takes in a parser argument:
17+
18+
```lua
19+
local util = require "formatter.util"
20+
21+
return function(parser)
22+
if not parser then
23+
return {
24+
exe = "prettier",
25+
args = {
26+
"--stdin-filepath",
27+
util.escape_path(util.get_current_buffer_file_path()),
28+
},
29+
stdin = true,
30+
try_node_modules = true,
31+
}
32+
end
33+
34+
return {
35+
exe = "prettier",
36+
args = {
37+
"--stdin-filepath",
38+
util.escape_path(util.get_current_buffer_file_path()),
39+
"--parser",
40+
parser,
41+
},
42+
stdin = true,
43+
try_node_modules = true,
44+
}
45+
end
46+
```
47+
48+
[Default configurations per formatter](lua/formatter/defaults)
49+
are used to create
50+
[default configurations per `filetype`](lua/formatter/filetypes), and
51+
[default configurations for any `filetype`](lua/formatter/filetypes/any.lua).
52+
53+
For example, the
54+
[default formatter configuration for `prettier` for the `typescript` `filetype`](lua/formatter/defaults/typescript.lua) uses the
55+
[default formatter configuration function for `prettier`](lua/formatter/defaults/prettier.lua):
56+
57+
```lua
58+
59+
local M = {}
60+
61+
local defaults = require "formatter.defaults"
62+
local util = require "formatter.util"
63+
64+
-- other formatters...
65+
66+
-- "util.withl" here returns a function that executes the defaults.prettier
67+
-- function with "typescript" as the first argument for the parser
68+
M.prettier = util.withl(defaults.prettier, "typescript")
69+
70+
-- other formatters...
71+
72+
return M
73+
```
74+
75+
The
76+
[default configurations for any `filetype`](lua/formatter/filetypes/any.lua)
77+
are in exactly the same format as
78+
[default configurations per `filetype`](lua/formatter/filetypes).
79+
It is just important to note that this file is special because all formatter
80+
default configurations that can be applied to any `filetype` go here.

README.md

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# Formatter.nvim
22

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>
47

58
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
710
a reference to create our own opt-in default formatter configurations.
811

912
## Features
1013

1114
- Written in `Lua`
1215
- Asynchronous execution
1316
- Opt-in default formatter configurations
17+
- Conditional formatting
1418

1519
## Install
1620

@@ -54,15 +58,25 @@ Setup:
5458
local util = require "formatter.util"
5559

5660
-- Provides the Format and FormatWrite commands
57-
require('formatter').setup {
61+
require("formatter").setup {
5862
-- All formatter configurations are opt-in
5963
filetype = {
64+
-- Formatter configurations for filetype "lua" go here
65+
-- and will be executed in order
6066
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,
6370

64-
-- ,or define your own:
71+
-- You can also define your own configuration
6572
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
6680
return {
6781
exe = "stylua",
6882
args = {
@@ -75,22 +89,35 @@ require('formatter').setup {
7589
stdin = true,
7690
}
7791
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
78100
}
79101
}
80102
}
81103
```
82104

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
87112
at the same time we realize that most formatter configurations are the same.
88113
See the discussion in
89114
[#97](https://github.com/mhartington/formatter.nvim/issues/97) for more
90115
information.
91116

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)
94121
as a starting point for creating your configurations.
95122
Feel free to contribute to this repository by creating or improving default
96123
configurations that everyone can use! The guide for contributing to default
@@ -99,15 +126,15 @@ configurations is below.
99126
You can use the [`util` module](lua/formatter/util) which has various
100127
functions that help with creating default configurations as shown above.
101128

102-
<!-- TODO: with lua callbacks -->
129+
### Map keys
103130

104-
Map keys:
105131
```vim
106132
nnoremap <silent> <leader>f :Format<CR>
107133
nnoremap <silent> <leader>F :FormatWrite<CR>
108134
```
109135

110-
Format and write after save asynchronously:
136+
### Format after save
137+
111138
```vim
112139
augroup FormatAutogroup
113140
autocmd!
@@ -165,39 +192,4 @@ the path to the formatted file as a named argument. For an example, check the
165192

166193
## Contribute
167194

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.

asset/splash.gif

425 KB
Loading

asset/splash.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local M = {}
2+
3+
function M.hello()
4+
5+
print "Hello formatter.nvim!"
6+
7+
8+
end
9+
10+
return M

0 commit comments

Comments
 (0)