File tree Expand file tree Collapse file tree 5 files changed +27
-11
lines changed Expand file tree Collapse file tree 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
@@ -87,7 +89,7 @@ Table format for custom tool:
87
89
- ` Pylint `
88
90
- ` rubocop `
89
91
90
- ## Trobuleshooting
92
+ ## Troubleshooting
91
93
92
94
if guard does not auto format on save, run ` checkhealth ` first.
93
95
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
@@ -93,7 +95,7 @@ Table format for custom tool:
93
95
timeout --integer
94
96
ignore_pattern --table ignore run format when pattern match
95
97
ignore_error --when has lsp error ignore format
96
-
98
+
97
99
--special
98
100
fn --function if fn is set other field will not take effect
99
101
}
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 )
65
66
66
67
local function do_fmt (buf )
67
68
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
69
70
vim .notify (' [Guard] missing config for filetype ' .. vim .bo [buf ].filetype , vim .log .levels .ERROR )
70
71
return
71
72
end
@@ -78,7 +79,7 @@ local function do_fmt(buf)
78
79
end
79
80
local prev_lines = util .get_prev_lines (buf , srow , erow )
80
81
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 ' }
82
83
local formatter = require (' guard.tools.formatter' )
83
84
local fname = vim .fn .fnameescape (api .nvim_buf_get_name (buf ))
84
85
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ local api = vim.api
2
2
local group = api .nvim_create_augroup (' Guard' , { clear = true })
3
3
local fts_config = require (' guard.filetype' )
4
4
local util = require (' guard.util' )
5
+ local config = require (' guard.config' )
5
6
6
7
local function register_event (fts )
7
8
api .nvim_create_autocmd (' FileType' , {
56
57
local function setup (opt )
57
58
opt = opt or {
58
59
fmt_on_save = true ,
60
+ lsp_as_default_formatter = false ,
59
61
}
60
62
61
63
parse_setup_cfg (opt .ft )
@@ -65,6 +67,10 @@ local function setup(opt)
65
67
register_event (fts )
66
68
end
67
69
70
+ if opt .lsp_as_default_formatter then
71
+ config .lsp_as_default_formatter = true
72
+ end
73
+
68
74
local lint = require (' guard.lint' )
69
75
for ft , conf in pairs (fts_config ) do
70
76
if conf .linter then
You can’t perform that action at this time.
0 commit comments