File tree 5 files changed +27
-11
lines changed 5 files changed +27
-11
lines changed Original file line number Diff line number Diff line change @@ -39,8 +39,10 @@ ft('go'):fmt('lsp')
39
39
40
40
-- call setup LAST
41
41
require (' guard' ).setup ({
42
- -- the only option for the setup function
42
+ -- the only options for the setup function
43
43
fmt_on_save = true ,
44
+ -- Use lsp if no formatter was defined for this filetype
45
+ lsp_as_default_formatter = false ,
44
46
})
45
47
```
46
48
@@ -85,7 +87,7 @@ Table format for custom tool:
85
87
- ` clang-tidy `
86
88
- ` Pylint `
87
89
88
- ## Trobuleshooting
90
+ ## Troubleshooting
89
91
90
92
if guard does not auto format on save, run ` checkhealth ` first.
91
93
Original file line number Diff line number Diff line change @@ -40,23 +40,25 @@ examples, more info below.
40
40
41
41
>lua
42
42
local ft = require('guard.filetype')
43
-
43
+
44
44
-- use clang-format and clang-tidy for c files
45
45
ft('c'):fmt('clang-format')
46
46
:lint('clang-tidy')
47
-
47
+
48
48
-- use stylua to format lua files and no linter
49
49
ft('lua' ):fmt('stylua' )
50
-
50
+
51
51
-- use lsp to format first then use golines to format
52
52
ft('go' ):fmt('lsp' )
53
53
:append('golines' )
54
54
:lint('golangci' )
55
-
55
+
56
56
-- call setup LAST
57
57
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,
60
62
})
61
63
<
62
64
@@ -92,7 +94,7 @@ Table format for custom tool:
92
94
timeout --integer
93
95
ignore_pattern --table ignore run format when pattern match
94
96
ignore_error --when has lsp error ignore format
95
-
97
+
96
98
--special
97
99
fn --function if fn is set other field will not take effect
98
100
}
Original file line number Diff line number Diff line change
1
+ local M = {
2
+ lsp_as_default_formatter = false
3
+ }
4
+
5
+ return M
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ local uv = vim.version().minor >= 10 and vim.uv or vim.loop
4
4
local spawn = require (' guard.spawn' ).try_spawn
5
5
local get_prev_lines = require (' guard.util' ).get_prev_lines
6
6
local filetype = require (' guard.filetype' )
7
+ local config = require (' guard.config' )
7
8
local util = require (' guard.util' )
8
9
9
10
local function ignored (buf , patterns )
62
63
63
64
local function do_fmt (buf )
64
65
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
66
67
vim .notify (' [Guard] missing config for filetype ' .. vim .bo [buf ].filetype , vim .log .levels .ERROR )
67
68
return
68
69
end
@@ -75,7 +76,7 @@ local function do_fmt(buf)
75
76
end
76
77
local prev_lines = util .get_prev_lines (buf , srow , erow )
77
78
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 ' }
79
80
local formatter = require (' guard.tools.formatter' )
80
81
local fname = vim .fn .fnameescape (api .nvim_buf_get_name (buf ))
81
82
Original file line number Diff line number Diff line change 1
1
local api = vim .api
2
2
local group = api .nvim_create_augroup (' Guard' , { clear = true })
3
+ local config = require (' guard.config' )
3
4
4
5
local function register_event (fts )
5
6
api .nvim_create_autocmd (' FileType' , {
25
26
local function setup (opt )
26
27
opt = opt or {
27
28
fmt_on_save = true ,
29
+ lsp_as_default_formatter = false ,
28
30
}
29
31
local fts_config = require (' guard.filetype' )
30
32
if opt .fmt_on_save then
31
33
register_event (vim .tbl_keys (fts_config ))
32
34
end
33
35
36
+ if opt .lsp_as_default_formatter then
37
+ config .lsp_as_default_formatter = true
38
+ end
39
+
34
40
local lint = require (' guard.lint' )
35
41
for ft , conf in pairs (fts_config ) do
36
42
if conf .linter then
You can’t perform that action at this time.
0 commit comments