-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
fix: defer closing guest to make rpcrequest work #102
base: main
Are you sure you want to change the base?
Conversation
6e3a099
to
a1cfd1b
Compare
a1cfd1b
to
62a1976
Compare
Hi, @willothy! It would be amazing if you could take a look at the PR. It's been almost half a year with no feedback. |
I'm sorry! I've been quite busy this year and haven't been able to prioritize plugin development. I'll try this out right now :) |
response_sock, | ||
"nvim_exec_lua", | ||
---@diagnostic disable-next-line: param-type-mismatch | ||
"vim.cmd.qa({ bang = true })", | ||
"vim.defer_fn(function() vim.cmd.qa({ bang = true }) end, 25)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tried this with just vim.schedule
? I'd like to avoid timers here if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember why I've used defer_fn
instead of schedule
but schedule
seems to work as well.
@@ -22,7 +22,7 @@ function M.maybe_block(block) | |||
end | |||
vim.fn.chanclose(host) | |||
while true do | |||
vim.cmd.sleep(1) | |||
vim.cmd.sleep(10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe this should just be changed to vim.wait
- I will look into this now. I wrote the original code for this quite a while ago when I was a lot less familiar with Neovim's internals and APIs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a big problem with vim.wait
- it is async, meaning guest stays fully responsive which is what we do not want because a user can accidentally mess up guest. sleep
on the other hand fully blocks guest neovim instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I'm not sure this is the case - vim.wait is blocking (for Lua), it just may not fully block the event loop - I don't think the guest is responsive while blocked. It definitely isn't for me, main is using vim.wait now and it works for me :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is blocking the function where it is called but it is not blocking the UI.
Try this:
- Open terminal:
:term
- Add something to index
git commit
- Return to the terminal buffer in host
i
once, you will enter terminal mode in host <- this is expectedi
second time, you will enter insert mode in guest <- this should not happen but it does becausevim.wait
only blocksmaybe_block
That's why I believe it is better to use sleep
.
Also, have you tried the |
I've just merged that PR - mind checking if the issue is still present in main? |
I'm a bit confused. Guest is not getting blocked. For this test I'm using the default config: require('flatten').setup({}) Lazygit config: os:
editPreset: nvim
Am I missing something? |
Nope, I accidentally introduced a bug earlier today. Should be fixed now! I didn't catch it because it was only breaking with |
I don't think we need Based on this comment and the comments before above I've adjusted Diffdiff --git a/lua/flatten/core.lua b/lua/flatten/core.lua
index 151df18..e03f3a7 100644
--- a/lua/flatten/core.lua
+++ b/lua/flatten/core.lua
@@ -9,10 +9,10 @@ function M.unblock_guest(guest_pipe)
})
return
end
- vim.rpcnotify(
+ vim.rpcrequest(
response_sock,
"nvim_exec_lua",
- "require('flatten.guest').unblock()",
+ "vim.schedule(function() vim.cmd.qa({ bang = true }) end)",
{}
)
if vim.api.nvim_get_chan_info(response_sock).id ~= nil then
diff --git a/lua/flatten/guest.lua b/lua/flatten/guest.lua
index 9281a76..536f29a 100644
--- a/lua/flatten/guest.lua
+++ b/lua/flatten/guest.lua
@@ -2,7 +2,6 @@ local M = {}
local config = require("flatten").config
-local waiting = false
local host
local function is_windows()
@@ -17,20 +16,13 @@ function M.exec_on_host(call, opts)
return vim.rpcrequest(host, "nvim_exec_lua", call, opts or {})
end
-function M.unblock()
- waiting = false
-end
-
function M.maybe_block(block)
if not block then
vim.cmd.qa({ bang = true })
end
- waiting = true
- vim.wait(math.huge, function()
- return waiting == false
- or vim.api.nvim_get_chan_info(host) == vim.empty_dict()
- end, 1000)
- vim.cmd.qa({ bang = true })
+ while true do
+ vim.cmd("sleep 10")
+ end
end
function M.send_files(files, stdin) But sometimes terminal colors get messed up and I get flashbanged by a light color: I can't figure out yet why that happens. |
Hmm, I'm not experiencing the delay or the flashbang. What terminal emulator are you using? Edit: Try 4258a0a, maybe that change will fix the delay :) |
That only decreases the delay to 200ms, which is better but not instant. And it puts pressure on the guest to process the function call every 200ms. This call is not a particularly expensive operation but it still costs some CPU time. Where |
It's alacritty 0.13 + tmux 3.5a. I'll try to figure out what is causing it and report later. |
Is the 200ms noticeable? If it is we could just decrease it to like 100ms or 50ms. I think the cost of running that function every 100ms even would be negligible. |
Good point, this one definitely will have weird behavior depending on the platform - changed it to just be |
Closes #101
Hey, @willothy!
So, I've solved it. The guest was quitting too fast thus rendering the channel invalid. Deferring the quit command allowed the
vim.rpcrequest
function to be used.I've also increased sleep time to 10s as I proposed in #101 but I can remove this commit if you don't like it.