Skip to content

Commit

Permalink
feat: support find tool format config file and set extra arguments close
Browse files Browse the repository at this point in the history
  • Loading branch information
glepnir committed Jul 28, 2023
1 parent 9b402d2 commit a2d1b41
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
10 changes: 10 additions & 0 deletions lua/guard/filetype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ local function box()
return self
end

function tbl:extra(...)
local tool = self[current][#self[current]]
if type(tool) == 'string' then
tool = current == 'format' and require('guard.tools.formatter')[tool]
or require('guard.tools.linter.' .. tool)
end
tool.args = vim.list_extend({ ... }, tool.args)
return self
end

function tbl:key_alias(key)
local _t = {
['lint'] = function()
Expand Down
25 changes: 20 additions & 5 deletions lua/guard/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local uv = vim.version().minor >= 10 and vim.uv or vim.loop
local spawn = require('guard.spawn').try_spawn
local get_prev_lines = require('guard.util').get_prev_lines
local filetype = require('guard.filetype')
local formatter = require('guard.tools.formatter')
local util = require('guard.util')

local function ignored(buf, patterns)
Expand Down Expand Up @@ -65,6 +66,18 @@ local function update_buffer(bufnr, new_lines, srow, erow)
vim.fn.winrestview(view)
end

local function find(startpath, patterns, root_dir)
patterns = util.as_table(patterns)
for _, pattern in ipairs(patterns) do
if
#vim.fs.find(pattern, { upward = true, stop = root_dir or uv.os_homedir(), path = startpath })
> 0
then
return true
end
end
end

local function do_fmt(buf)
buf = buf or api.nvim_get_current_buf()
if not filetype[vim.bo[buf].filetype] then
Expand All @@ -81,8 +94,10 @@ local function do_fmt(buf)
local prev_lines = util.get_prev_lines(buf, srow, erow)

local fmt_configs = filetype[vim.bo[buf].filetype].format
local formatter = require('guard.tools.formatter')
local fname = vim.fn.fnameescape(api.nvim_buf_get_name(buf))
local startpath = vim.fn.expand(fname, ':p:h')
local root_dir = util.get_lsp_root()
local cwd = root_dir or uv.cwd()

coroutine.resume(coroutine.create(function()
local new_lines
Expand All @@ -97,17 +112,17 @@ local function do_fmt(buf)
local can_run = true
if config.ignore_patterns and ignored(buf, configs.ignore_patterns) then
can_run = false
end

if config.ignore_error and can_run and #vim.diagnostic.get(buf, { severity = 1 }) ~= 0 then
elseif config.ignore_error and #vim.diagnostic.get(buf, { severity = 1 }) ~= 0 then
can_run = false
elseif config.find and not find(startpath, config.find, root_dir) then
can_run = false
end

if can_run then
if config.cmd then
config.lines = new_lines and new_lines or prev_lines
config.args[#config.args + 1] = config.fname and fname or nil
config.cwd = util.get_lsp_root() or uv.cwd()
config.cwd = cwd
reload = (not reload and config.stdout == false) and true or false
new_lines = spawn(config)
--restore
Expand Down
6 changes: 3 additions & 3 deletions lua/guard/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function util.get_prev_lines(bufnr, srow, erow)
return res
end

function util.get_lsp_root()
local curbuf = api.nvim_get_current_buf()
local clients = get_clients({ bufnr = curbuf })
function util.get_lsp_root(buf)
buf = buf or api.nvim_get_current_buf()
local clients = get_clients({ bufnr = buf })
if #clients == 0 then
return
end
Expand Down
14 changes: 14 additions & 0 deletions test/filetype_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,18 @@ describe('filetype module', function()
format = { { cmd = 'prettier', args = { 'some', 'args' } } },
}, ft.javascriptreact)
end)

it('can add extra command arguments', function()
ft('c'):fmt('clang-format'):extra('--verbose'):lint('clang-tidy'):extra('--fix')
same({
cmd = 'clang-format',
args = { '--verbose', '-style=file' },
stdin = true,
}, require('guard.tools.formatter')['clang-format'])

same({
'--fix',
'--quiet',
}, require('guard.tools.linter.clang-tidy').args)
end)
end)

0 comments on commit a2d1b41

Please sign in to comment.