glacambre / firenvim

Embed Neovim in Chrome, Firefox & others.
GNU General Public License v3.0
4.6k stars 143 forks source link

Browser textarea content out of sync with buffer #1533

Closed pharaok closed 1 year ago

pharaok commented 1 year ago

What I tried to do

I'm trying to do add some lines to the buffer on BufWritePre and remove those lines on BufWritePost 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

vim.api.nvim_create_autocmd("BufEnter", {
  pattern = "leetcode.com_problems*.txt",
  callback = function(event)
    local imports =
      { "from typing import List, Dict", "import math", "import heapq", "import collections", "", "" }
    vim.api.nvim_buf_set_lines(event.buf, 0, 0, false, imports)

    vim.api.nvim_create_autocmd("BufWritePre", {
      buffer = event.buf,
      callback = function()
        vim.api.nvim_buf_set_lines(event.buf, 0, #imports, false, {})
      end,
    })
    vim.api.nvim_create_autocmd("BufWritePost", {
      buffer = event.buf,
      callback = function()
        vim.api.nvim_buf_set_lines(event.buf, 0, 0, false, imports)
      end,
    })
  end,
})
glacambre commented 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 :)

pharaok commented 1 year ago

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!