Closed pharaok closed 1 year ago
You are modifying the buffer on BufWritePre
and Firenvim synchronizes the buffer on BufWrite
, so in theory your code should work. But in reality, it seems that BufWrite
and BufWritePre
are the same event (wtf???). I confirmed this by running au BufWrite echom 'BufWrite'
and au BufWritePre echom 'BufWritePre'
and then writing a buffer, BufWrite
was printed before BufWritePre
. It seems that the autocommands will be ran in the order they were created though, so tweaking your code like this:
local imports =
{ "from typing import List, Dict", "import math", "import heapq", "import collections", "", "" }
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "leetcode.com_problems*.txt",
callback = function(event)
vim.api.nvim_buf_set_lines(event.buf, 0, 0, false, imports)
end,
})
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "leetcode.com_problems*.txt",
callback = function(event)
vim.api.nvim_buf_set_lines(event.buf, 0, #imports, false, {})
end,
})
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = "leetcode.com_problems*.txt",
callback = function(event)
vim.api.nvim_buf_set_lines(event.buf, 0, 0, false, imports)
end,
})
Should produce the behavior you desire, as the BufWrite synchronization autocommand is only created when the Buffer is created, i.e. much later than the code that is ran in your init.lua. Let me know if this works for you :)
Yes, that fixed it!
And yeah it is weird that BufWrite
is an alias for BufWritePre
, if anything I would imagine it's like BufWritePost
.
Would be great if you could give autocmds a priority, but that's an issue for neovim :).
Thank you for this amazing project!
What I tried to do
I'm trying to do add some lines to the buffer on
BufWritePre
and remove those lines onBufWritePost
on leetcode.What happened
The added lines are still there after quitting firenvim, it looks like the content is sent to the browser before the lines are removed by
BufWritePre
.I tried looking at the tmp file in
/run
from another editor and it had the correct content.I also tried the same autocmds on a local file and it worked totally fine.
here's my code