Skip to content

ref!: major refactor #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 56 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
4d39989
chore(doc): auto generate docs
github-actions[bot] Jun 6, 2024
40a5d00
chore: shuffle all autocmd functions into the event module
xiaoshihou514 Jun 6, 2024
f19c4fd
chore: remove deprecated methods
xiaoshihou514 Jun 6, 2024
7399998
doc: add example of how to write custom formatting logic
xiaoshihou514 Jun 6, 2024
c9b9488
doc: promote advanced.md in README
xiaoshihou514 Jun 6, 2024
1b0d339
fix: typo
xiaoshihou514 Jun 6, 2024
8ad2cd4
chore: less verbose alias
xiaoshihou514 Jun 7, 2024
e0b7039
wip: basic outlinen of do_fmt
xiaoshihou514 Jun 7, 2024
d373d6d
wip: formatting module
xiaoshihou514 Jun 8, 2024
3cf35c1
fix: "async" execution using coroutines
xiaoshihou514 Jun 10, 2024
547149c
chore: remove version checks
xiaoshihou514 Jun 10, 2024
c35b0e1
feat: formatting module working correctly
xiaoshihou514 Jun 11, 2024
5be58dd
fix: autocmd test
xiaoshihou514 Jun 11, 2024
df79ed6
wip: mess with changedtick
xiaoshihou514 Jun 11, 2024
c96d016
fix: incorrect changedtick
xiaoshihou514 Jun 11, 2024
651d53b
chore: more rigorous error handling
xiaoshihou514 Jun 11, 2024
0a8e6b2
wip: lsp formatting
xiaoshihou514 Jun 12, 2024
ab5b673
fix: potential race condition
xiaoshihou514 Jun 12, 2024
4ec630e
fix: lint module
xiaoshihou514 Jun 12, 2024
248e533
feat: enhancements for linter module
xiaoshihou514 Jun 13, 2024
7950c42
doc: add nice looking demos :)
xiaoshihou514 Jun 13, 2024
434dc80
doc: formatting
xiaoshihou514 Jun 13, 2024
341b579
fix: save view
xiaoshihou514 Jun 13, 2024
b149d11
fix: remove deepcopies
xiaoshihou514 Jun 13, 2024
639ef8e
fix: tbl_filter and exepath
xiaoshihou514 Jun 13, 2024
527696e
chore: use custom table copy
xiaoshihou514 Jun 16, 2024
19a96d7
fix: existing tests
xiaoshihou514 Jun 17, 2024
3c1caff
test: toolcopy
xiaoshihou514 Jun 17, 2024
59c4527
chore: apply changes
xiaoshihou514 Jun 17, 2024
b3e42b0
doc: advanced linter customization
xiaoshihou514 Jun 17, 2024
6f6fe0f
Merge branch 'ref-api' of github.com:xiaoshihou514/guard.nvim into re…
xiaoshihou514 Jun 17, 2024
79bd3fa
fix: link
xiaoshihou514 Jun 17, 2024
8b496bf
fix: emoji
xiaoshihou514 Jun 17, 2024
e41c0e2
doc: fix missing line
xiaoshihou514 Jun 18, 2024
59c3977
chore: add types
xiaoshihou514 Jun 19, 2024
d425c0a
fix: types
xiaoshihou514 Jun 19, 2024
4e8f7d6
fix: use fnamemodify
xiaoshihou514 Jun 19, 2024
b3fa4c7
chore: refine code
xiaoshihou514 Jun 26, 2024
fb98efd
fix: lsp warning
xiaoshihou514 Jul 1, 2024
2fde1a6
chore: add executable check for linters
xiaoshihou514 Jul 2, 2024
7e2b579
chore: check executable in setup instead
xiaoshihou514 Jul 2, 2024
3bb6227
chore: remove redundant bufid check
xiaoshihou514 Jul 2, 2024
5f01630
fix: typo
xiaoshihou514 Jul 3, 2024
b322cfc
feat: more elegant checking and corresponding tests
xiaoshihou514 Jul 3, 2024
b2b900d
chore: rename due to namespace conflict
xiaoshihou514 Jul 3, 2024
eec417e
chore: ensure non nil
xiaoshihou514 Jul 3, 2024
475d970
chore: consistent executable checks
xiaoshihou514 Jul 3, 2024
3b79f3a
fix: incorrect copy and paste
xiaoshihou514 Jul 3, 2024
da9d6e2
chore: use vim.iter more
xiaoshihou514 Jul 3, 2024
9ae2f34
test: test for multiple formatters
xiaoshihou514 Jul 3, 2024
1835101
chore: ignore patterns should support single string
xiaoshihou514 Jul 3, 2024
939f6f4
feat: exepath patch for windows
xiaoshihou514 Jul 7, 2024
b051387
fix: exe check warning msg
xiaoshihou514 Jul 7, 2024
e377b9a
fix: update plugin/guard.lua
xiaoshihou514 Jul 10, 2024
787dac2
feat: tests for commands
xiaoshihou514 Jul 12, 2024
d31e457
fix: lsp attach autocmd
xiaoshihou514 Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip: formatting module
  • Loading branch information
xiaoshihou514 committed Jun 8, 2024
commit d373d6d2ae0dd6b0047adc847fe74d7ba576e0c5
49 changes: 34 additions & 15 deletions lua/guard/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ local function find(startpath, patterns, root_dir)
end

local function get_cmd(config, fname)
local cmd = config.args or {}
local cmd = config.args and vim.deepcopy(config.args) or {}
table.insert(cmd, 1, config.cmd)
if config.fname then
table.insert(cmd, fname)
Expand Down Expand Up @@ -114,10 +114,10 @@ local function do_fmt(buf)
local new_lines = prev_lines
local changedtick = api.nvim_buf_get_changedtick(buf)
local error = false
local error_cmd = ''
local error_result = {}

-- handle execution condition
fmt_configs = fmt_configs.filter(function(config)
fmt_configs = vim.iter(fmt_configs):filter(function(config)
if config.ignore_patterns and ignored(buf, config.ignore_patterns) then
return false
elseif config.ignore_error and #vim.diagnostic.get(buf, { severity = 1 }) ~= 0 then
Expand All @@ -129,10 +129,10 @@ local function do_fmt(buf)
end)

-- filter out pure and impure formatters
local pure = vim.iter(fmt_configs):filter(function(config)
local pure = vim.deepcopy(fmt_configs):filter(function(config)
return config.fn or (config.cmd and config.stdin)
end)
local impure = vim.iter(fmt_configs):filter(function(config)
local impure = vim.deepcopy(fmt_configs):filter(function(config)
return config.cmd and not config.stdin
end)

Expand All @@ -151,7 +151,7 @@ local function do_fmt(buf)
return
end

new_lines = pure:fold(new_lines, function(acc, _, config)
new_lines = pure:fold(new_lines, function(acc, config, _)
-- we don't need to reformat an empty string
if new_lines == '' then
return ''
Expand All @@ -160,12 +160,15 @@ local function do_fmt(buf)
return config.fn(buf, range, acc)
else
local result = spawn.transform(get_cmd(config, fname), cwd, config.env or {}, acc)
if type(result) == 'number' then
if type(result) == 'table' then
-- indicates error
error = true
error_cmd = config.cmd
error_result = result
---@diagnostic disable-next-line: inject-field
error_result.cmd = config.cmd
return ''
else
---@diagnostic disable-next-line: return-type-mismatch
return result
end
end
Expand All @@ -174,9 +177,16 @@ local function do_fmt(buf)
if error then
util.doau('GuardFmt', {
status = 'failed',
msg = error_cmd .. ' exited with non-zero exit code',
msg = error_result.cmd .. ' exited with non-zero exit code',
})
vim.notify('[Guard]: ' .. error_cmd .. ' exited with errors', 4)
vim.notify(
('[Guard]: %s exited with code %d\n%s'):format(
error_result.cmd,
error_result.code,
error_result.stderr
),
4
)
return
end

Expand All @@ -196,10 +206,12 @@ local function do_fmt(buf)
text = true,
cwd = cwd,
env = config.env or {},
}, function(handle)
if handle.code ~= 0 and #handle.stderr > 0 then
}, function(result)
if result.code ~= 0 and #result.stderr > 0 then
error = true
error_cmd = config.cmd
error_result = result
---@diagnostic disable-next-line: inject-field
error_result.cmd = config.cmd
end
end)
:wait()
Expand All @@ -208,9 +220,16 @@ local function do_fmt(buf)
if error then
util.doau('GuardFmt', {
status = 'failed',
msg = error_cmd .. ' exited with non-zero exit code',
msg = error_result .. ' exited with non-zero exit code',
})
vim.notify('[Guard]: ' .. error_cmd .. ' exited with errors', 4)
vim.notify(
('[Guard]: %s exited with code %d\n%s'):format(
error_result.cmd,
error_result.code,
error_result.stderr
),
4
)
return
end

Expand Down
18 changes: 15 additions & 3 deletions lua/guard/spawn.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
local M = {}

-- @return number | string
-- @return table | string
function M.transform(cmd, cwd, env, lines)
-- TODO
return 'foo'
local handle = vim.system(cmd, {
stdin = true,
cwd = cwd,
env = env,
})
-- write to stdin and close it
handle:write(lines)
handle:write(nil)
local result = handle:wait()
if result.code ~= 0 and #result.stderr > 0 then
-- error
return result
end
return result.stdout
end

return M
14 changes: 0 additions & 14 deletions test/spawn_spec.lua

This file was deleted.