Skip to content

Commit fa77011

Browse files
committed
Optionally use lsp as default formatter
1 parent cee98d1 commit fa77011

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

@@ -85,7 +87,7 @@ Table format for custom tool:
8587
- `clang-tidy`
8688
- `Pylint`
8789

88-
## Trobuleshooting
90+
## Troubleshooting
8991

9092
if guard does not auto format on save, run `checkhealth` first.
9193

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

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

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)
@@ -62,7 +63,7 @@ end
6263

6364
local function do_fmt(buf)
6465
buf = buf or api.nvim_get_current_buf()
65-
if not filetype[vim.bo[buf].filetype] then
66+
if not filetype[vim.bo[buf].filetype] and not config.lsp_as_default_formatter then
6667
vim.notify('[Guard] missing config for filetype ' .. vim.bo[buf].filetype, vim.log.levels.ERROR)
6768
return
6869
end
@@ -75,7 +76,7 @@ local function do_fmt(buf)
7576
end
7677
local prev_lines = util.get_prev_lines(buf, srow, erow)
7778

78-
local fmt_configs = filetype[vim.bo[buf].filetype].format
79+
local fmt_configs = filetype[vim.bo[buf].filetype] and filetype[vim.bo[buf].filetype].format or { 'lsp' }
7980
local formatter = require('guard.tools.formatter')
8081
local fname = vim.fn.fnameescape(api.nvim_buf_get_name(buf))
8182

lua/guard/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local api = vim.api
22
local group = api.nvim_create_augroup('Guard', { clear = true })
3+
local config = require('guard.config')
34

45
local function register_event(fts)
56
api.nvim_create_autocmd('FileType', {
@@ -25,12 +26,17 @@ end
2526
local function setup(opt)
2627
opt = opt or {
2728
fmt_on_save = true,
29+
lsp_as_default_formatter = false,
2830
}
2931
local fts_config = require('guard.filetype')
3032
if opt.fmt_on_save then
3133
register_event(vim.tbl_keys(fts_config))
3234
end
3335

36+
if opt.lsp_as_default_formatter then
37+
config.lsp_as_default_formatter = true
38+
end
39+
3440
local lint = require('guard.lint')
3541
for ft, conf in pairs(fts_config) do
3642
if conf.linter then

0 commit comments

Comments
 (0)