Skip to content

Commit

Permalink
Merge pull request #16 from erikzaadi/add-default-lsp-option
Browse files Browse the repository at this point in the history
Optionally use lsp as default formatter
  • Loading branch information
glepnir authored Jul 26, 2023
2 parents 1a6aa20 + 01de8b0 commit 33afb5a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ ft('typescript,javascript,typescriptreact'):fmt('prettier')

-- call setup LAST
require('guard').setup({
-- the only option for the setup function
-- the only options for the setup function
fmt_on_save = true,
-- Use lsp if no formatter was defined for this filetype
lsp_as_default_formatter = false,
})
```

Expand Down Expand Up @@ -90,7 +92,7 @@ Table format for custom tool:
- `Pylint`
- `rubocop`

## Trobuleshooting
## Troubleshooting

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

Expand Down
19 changes: 11 additions & 8 deletions doc/guard.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,29 @@ examples, more info below.

>lua
local ft = require('guard.filetype')

-- use clang-format and clang-tidy for c files
ft('c'):fmt('clang-format')
:lint('clang-tidy')

-- use stylua to format lua files and no linter
ft('lua'):fmt('stylua')

-- use lsp to format first then use golines to format
ft('go'):fmt('lsp')
:append('golines')
:lint('golangci')

-- multiple files register
ft('typescript,javascript,typescriptreact'):fmt('prettier')



-- call setup LAST
require('guard').setup({
-- the only option for the setup function
fmt_on_save = true,
-- the only options for the setup function
fmt_on_save = true,
-- Use lsp if no formatter was defined for this filetype
lsp_as_default_formatter = false,
})
<

Expand Down Expand Up @@ -96,7 +99,7 @@ Table format for custom tool:
timeout --integer
ignore_pattern --table ignore run format when pattern match
ignore_error --when has lsp error ignore format
--special
fn --function if fn is set other field will not take effect
}
Expand Down
19 changes: 19 additions & 0 deletions lua/guard/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ end
local function setup(opt)
opt = opt or {
fmt_on_save = true,
lsp_as_default_formatter = false,
}

parse_setup_cfg(opt.ft)
Expand All @@ -65,6 +66,24 @@ local function setup(opt)
register_event(fts)
end

if opt.lsp_as_default_formatter then
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if not client:supports_method('textDocument/formatting') then
return
end
local fthandler = require('guard.filetype')
local lsp = require('guard.tools.formatter').lsp
if fthandler[vim.bo[args.buf].filetype] and fthandler[vim.bo[args.buf].filetype].fmt then
table.insert(fthandler[vim.bo[args.buf]], 1, lsp)
else
fthandler(vim.bo[args.buf].filetype):fmt(lsp)
end
end
})
end

local lint = require('guard.lint')
for ft, conf in pairs(fts_config) do
if conf.linter then
Expand Down

0 comments on commit 33afb5a

Please sign in to comment.