Skip to content

Commit

Permalink
feat: update to refactorex 0.1.29
Browse files Browse the repository at this point in the history
The [stdio PR](gp-pereira/refactorex#19) has
been accepted and merged for 0.1.29, so the now unnecessary patch code
has been removed.
  • Loading branch information
synic committed Dec 29, 2024
1 parent ea6ceef commit 4df8785
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 162 deletions.
131 changes: 23 additions & 108 deletions lua/refactorex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ local refactorex_version =
vim.fn.readfile(vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h:h") .. "/refactorex-version.txt")[1]
local github_url = "https://github.com/gp-pereira/refactorex"

local install_dir = Path:new(vim.fn.stdpath("data")):joinpath("refactorex")
local install_path = Path:new(install_dir):joinpath("refactorex-" .. refactorex_version)
local start_script = install_path:joinpath("bin/start")

local M = {
config = {
filetypes = { "elixir" },
Expand All @@ -23,64 +27,8 @@ local M = {
pending_server_starts = {},
}

local function get_install_dir()
return Path:new(vim.fn.stdpath("data")):joinpath("refactorex")
end

local function apply_stdio_patch(install_path, callback)
local patch_source = Path:new(vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h"))
:joinpath("../patches/stdio-0.1.28.patch")

if not patch_source:exists() then
if callback then
callback(false)
end
return
end

---@diagnostic disable-next-line: missing-fields
Job:new({
command = "patch",
args = { "-p1", "-d", install_path:absolute() },
writer = patch_source:read(),
on_exit = function(j, return_val)
if return_val == 0 and j:result()[1]:match("patching file") then
local start_stdio_script = install_path:joinpath("bin/start-stdio")
if start_stdio_script:exists() then
---@diagnostic disable-next-line: missing-fields
Job:new({
command = "chmod",
args = { "+x", start_stdio_script:absolute() },
on_exit = function(_, retval)
if retval ~= 0 then
vim.notify(
"RefactorEx: Failed to make start-stdio script executable",
vim.log.levels.ERROR
)
end
if callback then
callback(retval == 0)
end
end,
}):start()
else
vim.notify("RefactorEx: start-stdio script not found", vim.log.levels.ERROR)
if callback then
callback(false)
end
end
else
vim.notify("RefactorEx: Failed to apply stdio patch", vim.log.levels.ERROR)
if callback then
callback(false)
end
end
end,
}):start()
end

local function check_dependencies()
local required_commands = { "nc", "curl", "tar", "mix", "patch", "chmod" }
local required_commands = { "nc", "curl", "tar", "mix" }
for _, cmd in ipairs(required_commands) do
if vim.fn.executable(cmd) ~= 1 then
error(string.format("Required command '%s' not found in PATH", cmd))
Expand Down Expand Up @@ -124,7 +72,7 @@ local function setup_server_config(opts)
end

local function download_refactorex(tar_file, callback)
vim.notify("RefactorEx: downloading and installing release source...", vim.log.levels.INFO)
vim.notify("RefactorEx: downloading and installing updated release source...", vim.log.levels.INFO)

---@diagnostic disable-next-line: missing-fields
Job:new({
Expand All @@ -148,7 +96,7 @@ local function download_refactorex(tar_file, callback)
}):start()
end

local function extract_tarball(tar_file, install_dir, callback)
local function extract_tarball(tar_file, callback)
---@diagnostic disable-next-line: missing-fields
Job:new({
command = "tar",
Expand All @@ -165,7 +113,7 @@ local function extract_tarball(tar_file, install_dir, callback)
}):start()
end

local function install_mix_dependencies(install_path, callback)
local function install_mix_dependencies(callback)
---@diagnostic disable-next-line: missing-fields
Job:new({
command = "mix",
Expand All @@ -182,7 +130,7 @@ local function install_mix_dependencies(install_path, callback)
}):start()
end

local function compile_refactorex(install_path, callback)
local function compile_refactorex(callback)
---@diagnostic disable-next-line: missing-fields
Job:new({
command = "mix",
Expand All @@ -202,7 +150,6 @@ local function compile_refactorex(install_path, callback)
end

function M.ensure_refactorex(callback, force_reinstall)
local install_dir = get_install_dir()
local tar_file = install_dir:joinpath(string.format("refactorex-%s.tar.gz", refactorex_version))
local refactorex_path = install_dir:joinpath(string.format("refactorex-%s", refactorex_version))

Expand Down Expand Up @@ -232,23 +179,16 @@ function M.ensure_refactorex(callback, force_reinstall)
M.installing = true

download_refactorex(tar_file, function()
extract_tarball(tar_file, install_dir, function()
local install_path = install_dir:joinpath(string.format("refactorex-%s", refactorex_version))
install_mix_dependencies(install_path, function()
apply_stdio_patch(install_path, function(success)
if not success then
M.installing = false
return
extract_tarball(tar_file, function()
install_mix_dependencies(function()
compile_refactorex(function()
if callback then
callback()
end
compile_refactorex(install_path, function()
if callback then
callback()
end
for _, pending_callback in ipairs(M.pending_server_starts) do
pending_callback()
end
M.pending_server_starts = {}
end)
for _, pending_callback in ipairs(M.pending_server_starts) do
pending_callback()
end
M.pending_server_starts = {}
end)
end)
end)
Expand All @@ -263,22 +203,17 @@ local function create_commands()
})

vim.api.nvim_create_user_command("RefactorExPatch", function()
local install_dir = get_install_dir()
local install_path = install_dir:joinpath(string.format("refactorex-%s", refactorex_version))

if not install_path:exists() then
vim.notify("RefactorEx installation not found", vim.log.levels.ERROR)
return
end

apply_stdio_patch(install_path)
end, {
desc = "Apply stdio patch to existing RefactorEx installation",
})
end

local function start_lsp_server(opts, start_stdio_script)
opts.cmd = { start_stdio_script:absolute() }
local function start_lsp_server(opts)
opts.cmd = { start_script:absolute(), "--stdio" }
setup_server_config(opts)

vim.schedule(function()
Expand All @@ -303,32 +238,12 @@ function M.setup(opts)
pattern = "elixir",
callback = function()
if not server_started then
local install_path = Path:new(get_install_dir()):joinpath("refactorex-" .. refactorex_version)
local start_stdio_script = install_path:joinpath("bin/start-stdio")
local start_script = install_path:joinpath("bin/start")

if start_stdio_script:exists() then
start_lsp_server(opts, start_stdio_script)
elseif start_script:exists() then
-- this is only required for the 0.1.28 version that may have already been installed for some neovim users -
-- I switched from tcp transport to patching refactorex to use the stdio transport, however, some people may
-- already have 0.1.28 downloaded and installed. If `bin/start-stdio` doesn't exist, then it means that the
-- source hasn't been patched and it needs to be done.
--
-- When there's a new version of refactorex, this can be removed, because patching will be done as a part of
-- the new version installation process (or, possibly might not be needed at all, see:
-- https://github.com/gp-pereira/refactorex/pull/19)
apply_stdio_patch(install_path, function(success)
if success then
start_lsp_server(opts, start_stdio_script)
else
vim.notify("RefactorEx: Failed to apply stdio patch", vim.log.levels.ERROR)
end
end)
if start_script:exists() then
start_lsp_server(opts)
else
M.ensure_refactorex(function()
vim.notify("RefactorEx: installed successfully, starting server...", vim.log.levels.INFO)
start_lsp_server(opts, start_stdio_script)
start_lsp_server(opts)
end)
end

Expand Down
53 changes: 0 additions & 53 deletions patches/stdio-0.1.28.patch

This file was deleted.

2 changes: 1 addition & 1 deletion refactorex-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.28
0.1.29

0 comments on commit 4df8785

Please sign in to comment.