Skip to content

Commit

Permalink
refactor: adapt to nvim 0.11.x deprecating vim.fn.termopen()
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Jan 6, 2025
1 parent 41ede7f commit 04ce8a1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
47 changes: 42 additions & 5 deletions lua/yazi/process/yazi_process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,48 @@ function YaziProcess:start(config, paths, on_exit)
local yazi_cmd = self.ya_process:get_yazi_command(paths)
Log:debug(string.format("Opening yazi with the command: (%s).", yazi_cmd))

self.yazi_job_id = vim.fn.termopen(yazi_cmd, {
if vim.fn.has("nvim-0.11") == 1 then
self.yazi_job_id = vim.fn.jobstart(yazi_cmd, {
term = true,
env = {
-- expose NVIM_CWD so that yazi keybindings can use it to offer basic
-- neovim specific functionality
NVIM_CWD = vim.uv.cwd(),
},
on_exit = function(_, code)
self.ya_process:kill()
local events = self.ya_process:wait(1000)

local chosen_files = {}
if utils.file_exists(config.chosen_file_path) == true then
chosen_files = vim.fn.readfile(config.chosen_file_path)
end

local last_directory = nil
if self.ya_process.cwd ~= nil then
last_directory = plenary_path:new(self.ya_process.cwd) --[[@as Path]]
end

on_exit(
code,
chosen_files,
events,
self.ya_process.hovered_url,
last_directory
)
end,
})
else
self.yazi_job_id = self:nvim_0_10_termopen(config, on_exit, yazi_cmd)
end

self.ya_process:start()

return self
end

function YaziProcess:nvim_0_10_termopen(config, on_exit, yazi_cmd)
return vim.fn.termopen(yazi_cmd, {
env = {
-- expose NVIM_CWD so that yazi keybindings can use it to offer basic
-- neovim specific functionality
Expand Down Expand Up @@ -61,10 +102,6 @@ function YaziProcess:start(config, paths, on_exit)
)
end,
})

self.ya_process:start()

return self
end

return YaziProcess
16 changes: 10 additions & 6 deletions spec/yazi/ya_process_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -270,24 +270,28 @@ describe("opening the yazi in a terminal", function()
it(
"sets the NVIM_CWD environment variable to the current working directory",
function()
if vim.fn.has("nvim-0.11") == 0 then
return
end

-- selene: allow(incorrect_standard_library_use)
os.remove = spy.new(function() end)
vim.uv.cwd = spy.new(function()
return "/tmp/fakedir"
end)
local termopen_spy = spy.new(function() end)
vim.fn.termopen = termopen_spy
local jobstart_spy = spy.new(function() end)
vim.fn.jobstart = jobstart_spy

require("yazi.process.yazi_process"):start(
config,
{ path },
function() end
)

assert(termopen_spy.calls[1])
assert(termopen_spy.calls[1].vals[2])
assert(termopen_spy.calls[1].vals[2].env)
local env = termopen_spy.calls[1].vals[2].env
assert(jobstart_spy.calls[1])
assert(jobstart_spy.calls[1].vals[2])
assert(jobstart_spy.calls[1].vals[2].env)
local env = jobstart_spy.calls[1].vals[2].env

assert.equal(env.NVIM_CWD, "/tmp/fakedir")
end
Expand Down

0 comments on commit 04ce8a1

Please sign in to comment.