Skip to content

Commit a14d6fe

Browse files
committed
Optionally use lsp as default formatter
1 parent be414bf commit a14d6fe

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ ft('go'):fmt('lsp')
3939

4040
-- call setup LAST
4141
require('guard').setup({
42-
-- the only option for the setup function
42+
-- the only options for the setup function
4343
fmt_on_save = true,
44+
-- Use lsp if no formatter was defined for this filetype
45+
lsp_as_default_formatter = false,
4446
})
4547
```
4648

@@ -87,7 +89,7 @@ Table format for custom tool:
8789
- `Pylint`
8890
- `rubocop`
8991

90-
## Trobuleshooting
92+
## Troubleshooting
9193

9294
if guard does not auto format on save, run `checkhealth` first.
9395

doc/guard.nvim.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,25 @@ examples, more info below.
4040

4141
>lua
4242
local ft = require('guard.filetype')
43-
43+
4444
-- use clang-format and clang-tidy for c files
4545
ft('c'):fmt('clang-format')
4646
:lint('clang-tidy')
47-
47+
4848
-- use stylua to format lua files and no linter
4949
ft('lua'):fmt('stylua')
50-
50+
5151
-- use lsp to format first then use golines to format
5252
ft('go'):fmt('lsp')
5353
:append('golines')
5454
:lint('golangci')
55-
55+
5656
-- call setup LAST
5757
require('guard').setup({
58-
-- the only option for the setup function
59-
fmt_on_save = true,
58+
-- the only options for the setup function
59+
fmt_on_save = true,
60+
-- Use lsp if no formatter was defined for this filetype
61+
lsp_as_default_formatter = false,
6062
})
6163
<
6264

@@ -93,7 +95,7 @@ Table format for custom tool:
9395
timeout --integer
9496
ignore_pattern --table ignore run format when pattern match
9597
ignore_error --when has lsp error ignore format
96-
98+
9799
--special
98100
fn --function if fn is set other field will not take effect
99101
}

lua/guard/config.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local M = {
2+
lsp_as_default_formatter = false
3+
}
4+
5+
return M

lua/guard/format.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local uv = vim.version().minor >= 10 and vim.uv or vim.loop
44
local spawn = require('guard.spawn').try_spawn
55
local get_prev_lines = require('guard.util').get_prev_lines
66
local filetype = require('guard.filetype')
7+
local config = require('guard.config')
78
local util = require('guard.util')
89

910
local function ignored(buf, patterns)
@@ -65,7 +66,7 @@ end
6566

6667
local function do_fmt(buf)
6768
buf = buf or api.nvim_get_current_buf()
68-
if not filetype[vim.bo[buf].filetype] then
69+
if not filetype[vim.bo[buf].filetype] and not config.lsp_as_default_formatter then
6970
vim.notify('[Guard] missing config for filetype ' .. vim.bo[buf].filetype, vim.log.levels.ERROR)
7071
return
7172
end
@@ -78,7 +79,7 @@ local function do_fmt(buf)
7879
end
7980
local prev_lines = util.get_prev_lines(buf, srow, erow)
8081

81-
local fmt_configs = filetype[vim.bo[buf].filetype].format
82+
local fmt_configs = filetype[vim.bo[buf].filetype] and filetype[vim.bo[buf].filetype].format or { 'lsp' }
8283
local formatter = require('guard.tools.formatter')
8384
local fname = vim.fn.fnameescape(api.nvim_buf_get_name(buf))
8485

lua/guard/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local api = vim.api
22
local group = api.nvim_create_augroup('Guard', { clear = true })
33
local fts_config = require('guard.filetype')
44
local util = require('guard.util')
5+
local config = require('guard.config')
56

67
local function register_event(fts)
78
api.nvim_create_autocmd('FileType', {
@@ -56,6 +57,7 @@ end
5657
local function setup(opt)
5758
opt = opt or {
5859
fmt_on_save = true,
60+
lsp_as_default_formatter = false,
5961
}
6062

6163
parse_setup_cfg(opt.ft)
@@ -65,6 +67,10 @@ local function setup(opt)
6567
register_event(fts)
6668
end
6769

70+
if opt.lsp_as_default_formatter then
71+
config.lsp_as_default_formatter = true
72+
end
73+
6874
local lint = require('guard.lint')
6975
for ft, conf in pairs(fts_config) do
7076
if conf.linter then

0 commit comments

Comments
 (0)