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
chore: more rigorous error handling
  • Loading branch information
xiaoshihou514 committed Jun 11, 2024
commit 651d53b525ee268ca1b8005d5a28b07029b74fce
50 changes: 17 additions & 33 deletions ADVANCED.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,15 @@ end

However, `prettierd` prints error messages to stdout, so guard will fail to detect an error and proceed to replace your code with its error message :cry:

But fear not! You can create your custom logic by passing a function in the config table, in fact, lsp formatting is simply defined as:
But fear not! You can create your custom logic by passing a function in the config table, let's do this step by step:

```lua
{
fn = function(bufnr, range)
vim.lsp.buf.format({ bufnr = bufnr, range = range, async = true })
end,
}
```

Let's do this step by step:

```lua
local function prettierd_fmt(buf, range)
local srow = 0
local erow = -1
if range then
srow = range["start"][1]
erow = range["end"][1]
end
local function prettierd_fmt(buf, range, acc)
local co = assert(coroutine.running())
end
```

The function signature takes a buffer number (`:h bufnr`) and (optionally) a range table, like `vim.lsp.buf.format`. The range table contains 2 keys `start` and `end`, which themselves is a { row, col } tuple, respectively. Since it's not very reasonable to format from the middle of a line, we just take the row numbers.
Guard runs the format function in a coroutine so as not to block the UI, to achieve what we want we have to interact with the current coroutine.

We can now go on to mimic how we would call `prettierd` on the cmdline:

Expand All @@ -48,20 +33,18 @@ cat test.js | prettierd test.js
```

```lua
local function prettierd_fmt(buf, range)
-- previous code omitted

local prev_lines = table.concat(vim.api.nvim_buf_get_lines(buf, srow, erow, false), "\n")
local handle = vim.system({ "prettierd", vim.api.nvim_buf_get_name(buf) }, {
stdin = true,
}, vim.schedule_wrap(function(result)
if result.code ~= 0 then
return
end
local out = result.stdout
vim.api.nvim_buf_set_lines(buf, srow, erow, false, vim.split(out, "\r?\n"))
vim.cmd("silent! noautocmd write!")
end))
local function prettierd_fmt(buf, range, acc)
local handle = vim.system({ "prettierd", vim.api.nvim_buf_get_name(buf) }, {
stdin = true,
}, function(result)
if result.code ~= 0 then
-- "returns" the error
coroutine.resume(co, result)
else
-- "returns" the result
coroutine.resume(co, result.stdout)
end
end)
end
```

Expand All @@ -81,6 +64,7 @@ local function prettierd_fmt(buf, range)

handle:write(prev_lines)
handle:write(nil) -- closes stdin
return coroutine.yield() -- this returns either the error or the formatted code we returned earlier
end
```

Expand Down
12 changes: 9 additions & 3 deletions lua/guard/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,20 @@ local function do_fmt(buf)

coroutine.resume(coroutine.create(function()
local changedtick = -1
-- defer initialization, since BufWritePre would trigger a tick change
vim.schedule(function()
changedtick = api.nvim_buf_get_changedtick(buf)
end)
new_lines = pure:fold(new_lines, function(acc, config, _)
if errno then
return ''
end
-- check if we are in a valid state
vim.schedule(function()
if api.nvim_buf_get_changedtick(buf) ~= changedtick then
errno = { reason = 'buffer changed' }
end
end)
if errno then
return ''
end

if config.fn then
return config.fn(buf, range, acc)
Expand Down Expand Up @@ -217,6 +219,10 @@ local function do_fmt(buf)
end
return
end
-- check buffer one last time
if api.nvim_buf_get_changedtick(buf) ~= changedtick then
fail('buffer changed during formatting')
end
if not api.nvim_buf_is_valid(buf) then
fail('buffer no longer valid')
return
Expand Down