From e733d69c02744d979b7a31de86e3e36a0bb5fde6 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Mon, 6 Jan 2025 14:28:37 +0200 Subject: [PATCH] refactor: adapt to nvim 0.11.x deprecating `vim.fn.termopen()` https://github.com/neovim/neovim/pull/31343 --- lua/yazi/process/yazi_process.lua | 47 +++++++++++++++++++++++++++---- spec/yazi/ya_process_spec.lua | 16 +++++++---- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/lua/yazi/process/yazi_process.lua b/lua/yazi/process/yazi_process.lua index 092ff36e..9c55748e 100644 --- a/lua/yazi/process/yazi_process.lua +++ b/lua/yazi/process/yazi_process.lua @@ -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 @@ -61,10 +102,6 @@ function YaziProcess:start(config, paths, on_exit) ) end, }) - - self.ya_process:start() - - return self end return YaziProcess diff --git a/spec/yazi/ya_process_spec.lua b/spec/yazi/ya_process_spec.lua index f84896d0..6f3b511a 100644 --- a/spec/yazi/ya_process_spec.lua +++ b/spec/yazi/ya_process_spec.lua @@ -270,13 +270,17 @@ 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, @@ -284,10 +288,10 @@ describe("opening the yazi in a terminal", function() 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