Skip to content

Commit 1f7c9e5

Browse files
committed
feat: replace plenary Job with vim.system (#556)
Plenary is not actively maintained and will be archived soon. Replace plenary.Job with builtin vim.system calls.
1 parent 8bb1557 commit 1f7c9e5

8 files changed

Lines changed: 88 additions & 121 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ With <a href="https://github.com/folke/lazy.nvim">Lazy</a>:
4444
"harrisoncramer/gitlab.nvim",
4545
dependencies = {
4646
"MunifTanjim/nui.nvim",
47-
"nvim-lua/plenary.nvim",
4847
"dlyongemallo/diffview-plus.nvim", -- Maintained fork of "sindrets/diffview.nvim".
4948
"stevearc/dressing.nvim", -- Recommended but not required. Better UI for pickers.
5049
"nvim-tree/nvim-web-devicons", -- Recommended but not required. Icons in discussion tree.
@@ -63,7 +62,6 @@ And with <a href="https://github.com/lewis6991/pckr.nvim">pckr.nvim</a>:
6362
"harrisoncramer/gitlab.nvim",
6463
requires = {
6564
"MunifTanjim/nui.nvim",
66-
"nvim-lua/plenary.nvim",
6765
"dlyongemallo/diffview-plus.nvim", -- Maintained fork of "sindrets/diffview.nvim".
6866
"stevearc/dressing.nvim", -- Recommended but not required. Better UI for pickers.
6967
"nvim-tree/nvim-web-devicons", -- Recommended but not required. Icons in discussion tree.

doc/gitlab.nvim.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ With Lazy:
6969
"harrisoncramer/gitlab.nvim",
7070
dependencies = {
7171
"MunifTanjim/nui.nvim",
72-
"nvim-lua/plenary.nvim",
7372
"dlyongemallo/diffview-plus.nvim", -- Maintained fork of "sindrets/diffview.nvim".
7473
"stevearc/dressing.nvim", -- Recommended but not required. Better UI for pickers.
7574
"nvim-tree/nvim-web-devicons", -- Recommended but not required. Icons in discussion tree.
@@ -86,7 +85,6 @@ And with pckr.nvim:
8685
"harrisoncramer/gitlab.nvim",
8786
requires = {
8887
"MunifTanjim/nui.nvim",
89-
"nvim-lua/plenary.nvim",
9088
"dlyongemallo/diffview-plus.nvim", -- Maintained fork of "sindrets/diffview.nvim".
9189
"stevearc/dressing.nvim", -- Recommended but not required. Better UI for pickers.
9290
"nvim-tree/nvim-web-devicons", -- Recommended but not required. Icons in discussion tree.

lua-test.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ set -euo pipefail
99
PLUGINS_FOLDER="tests/plugins"
1010
PLUGINS=(
1111
"https://github.com/MunifTanjim/nui.nvim"
12-
"https://github.com/nvim-lua/plenary.nvim"
1312
"https://github.com/dlyongemallo/diffview-plus.nvim"
1413
)
1514

lua/gitlab/git.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,25 @@ M.check_mr_in_good_condition = function()
234234
end
235235
end
236236

237+
---Returns the full diff between the local working tree relative to the named `base_sha`, for the
238+
---given file(s).
239+
---@param base_sha string Base sha to diff against
240+
---@param old_path string? Old file name
241+
---@param new_path string? New file name - relevant for renamed files, ignored if same as old_path
242+
---@return string|nil diff, string|nil err
243+
M.diff_files = function(base_sha, old_path, new_path)
244+
return run_system({
245+
"git",
246+
"diff",
247+
"--minimal",
248+
"--unified=0",
249+
"--no-color",
250+
"--no-ext-diff",
251+
base_sha,
252+
"--",
253+
old_path,
254+
new_path,
255+
})
256+
end
257+
237258
return M

lua/gitlab/health.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ M.check = function(return_results)
2626
name = "MunifTanjim/nui.nvim",
2727
package = "nui.popup",
2828
},
29-
{
30-
name = "nvim-lua/plenary.nvim",
31-
package = "plenary",
32-
},
3329
{
3430
name = "dlyongemallo/diffview-plus.nvim",
3531
package = "diffview",

lua/gitlab/hunks.lua

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local M = {}
1414
---@field all_diff_output table The data from the git diff command
1515

1616
---Turn hunk line into Lua table
17-
---@param line table
17+
---@param line string
1818
---@return Hunk|nil
1919
M.parse_possible_hunk_headers = function(line)
2020
if line:sub(1, 2) == "@@" then
@@ -34,7 +34,7 @@ M.parse_possible_hunk_headers = function(line)
3434
end
3535
---@param linnr number
3636
---@param hunk Hunk
37-
---@param all_diff_output table
37+
---@param all_diff_output string[]
3838
---@return boolean
3939
local line_was_removed = function(linnr, hunk, all_diff_output)
4040
for matching_line_index, line in ipairs(all_diff_output) do
@@ -58,7 +58,7 @@ end
5858

5959
---@param linnr number
6060
---@param hunk Hunk
61-
---@param all_diff_output table
61+
---@param all_diff_output string[]
6262
---@return boolean
6363
local line_was_added = function(linnr, hunk, all_diff_output)
6464
for matching_line_index, line in ipairs(all_diff_output) do
@@ -96,46 +96,26 @@ local parse_hunks_and_diff = function(base_sha)
9696
local hunks = {}
9797
local all_diff_output = {}
9898

99+
local git = require("gitlab.git")
99100
local reviewer = require("gitlab.reviewer")
100-
local cmd = {
101-
"diff",
102-
"--minimal",
103-
"--unified=0",
104-
"--no-color",
105-
"--no-ext-diff",
106-
base_sha,
107-
"--",
108-
reviewer.get_current_file_oldpath(),
109-
reviewer.get_current_file_path(),
110-
}
111101

112-
local Job = require("plenary.job")
113-
local diff_job = Job:new({
114-
command = "git",
115-
args = cmd,
116-
on_exit = function(j, return_code)
117-
if return_code == 0 then
118-
all_diff_output = j:result()
119-
for _, line in ipairs(all_diff_output) do
120-
local hunk = M.parse_possible_hunk_headers(line)
121-
if hunk ~= nil then
122-
table.insert(hunks, hunk)
123-
end
124-
end
125-
else
126-
M.notify("Failed to get git diff: " .. j:stderr(), vim.log.levels.WARN)
102+
local diff, _ = git.diff_files(base_sha, reviewer.get_current_file_oldpath(), reviewer.get_current_file_path())
103+
if diff ~= nil then
104+
for line in diff:gmatch("[^\r\n]+") do
105+
table.insert(all_diff_output, line)
106+
local hunk = M.parse_possible_hunk_headers(line)
107+
if hunk ~= nil then
108+
table.insert(hunks, hunk)
127109
end
128-
end,
129-
})
130-
131-
diff_job:sync()
110+
end
111+
end
132112

133113
return { hunks = hunks, all_diff_output = all_diff_output }
134114
end
135115

136116
-- Parses the lines from a diff and returns the
137117
-- index of the next hunk, when provided an initial index
138-
---@param lines table
118+
---@param lines string[]
139119
---@param i integer
140120
---@return integer|nil
141121
local next_hunk_index = function(lines, i)

lua/gitlab/job.lua

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
11
-- This module is responsible for making API calls to the Go server and
22
-- running the callbacks associated with those jobs when the JSON is returned
3-
local Job = require("plenary.job")
43
local u = require("gitlab.utils")
54
local M = {}
65

6+
---Send a request to the Go server
7+
---@param endpoint string The endpoint path on the server.
8+
---@param method string The HTTP rquest method.
9+
---@param callback fun(data: table) The function to run on the decoded JSON response data if the response contains no error details.
10+
---@param on_error_callback? fun(data: table) The function to run on the decoded JSON response data in case the response contains error details.
711
M.run_job = function(endpoint, method, body, callback, on_error_callback)
812
local state = require("gitlab.state")
913
local port = state.settings.server and state.settings.server.port
10-
local args =
11-
{ "--noproxy", "localhost", "-s", "-X", (method or "POST"), string.format("localhost:%s%s", port, endpoint) }
14+
local cmd = {
15+
"curl",
16+
"--noproxy",
17+
"localhost",
18+
"-s",
19+
"-X",
20+
(method or "POST"),
21+
string.format("localhost:%s%s", port, endpoint),
22+
}
1223

1324
if body ~= nil then
1425
local encoded_body = vim.json.encode(body)
15-
table.insert(args, 1, "-d")
16-
table.insert(args, 2, encoded_body)
26+
table.insert(cmd, 2, "-d")
27+
table.insert(cmd, 3, encoded_body)
1728
end
1829

1930
-- This handler will handle all responses from the Go server. Anything with a successful
2031
-- status will call the callback (if it is supplied for the job). Otherwise, it will print out the
2132
-- success message or error message and details from the Go server and run the on_error_callback
2233
-- (if supplied for the job).
23-
local stderr = {}
24-
Job:new({
25-
command = "curl",
26-
args = args,
27-
on_stdout = function(_, output)
28-
vim.defer_fn(function()
29-
if output == nil then
30-
return
31-
end
32-
local data_ok, data = pcall(vim.json.decode, output)
34+
vim.system(cmd, { text = true }, function(out)
35+
vim.schedule(function()
36+
if out.code ~= 0 then
37+
u.notify(string.format("Go server exited with non-zero code: %d", out.code), vim.log.levels.ERROR)
38+
end
3339

40+
if out.stderr ~= "" then
41+
u.notify(string.format("Could not run command `%s`! Stderr was:", table.concat(cmd, " ")), vim.log.levels.ERROR)
42+
u.notify(vim.trim(out.stderr), vim.log.levels.ERROR)
43+
end
44+
45+
if out.stdout ~= "" then
46+
local data_ok, data = pcall(vim.json.decode, out.stdout)
3447
-- Failing to unmarshal JSON
3548
if not data_ok then
3649
local msg = string.format("Failed to parse JSON from %s endpoint", endpoint)
37-
if type(output) == "string" then
38-
msg = string.format(msg .. ", got: '%s'", output)
50+
if type(out.stdout) == "string" then
51+
msg = string.format(msg .. ", got: '%s'", out.stdout)
3952
end
40-
u.notify(string.format(msg, endpoint, output), vim.log.levels.WARN)
53+
u.notify(string.format(msg, endpoint, out.stdout), vim.log.levels.WARN)
4154
return
4255
end
4356

@@ -60,28 +73,9 @@ M.run_job = function(endpoint, method, body, callback, on_error_callback)
6073
on_error_callback(data)
6174
end
6275
end
63-
end, 0)
64-
end,
65-
on_stderr = function(_, data)
66-
if data then
67-
table.insert(stderr, data)
6876
end
69-
end,
70-
on_exit = function(code, status)
71-
vim.defer_fn(function()
72-
if #stderr ~= 0 then
73-
u.notify(
74-
string.format("Could not run command `%s %s`! Stderr was:", code.command, table.concat(code.args, " ")),
75-
vim.log.levels.ERROR
76-
)
77-
vim.notify(string.format("%s", table.concat(stderr, "\n")), vim.log.levels.ERROR)
78-
end
79-
if status ~= 0 then
80-
u.notify(string.format("Go server exited with non-zero code: %d", status), vim.log.levels.ERROR)
81-
end
82-
end, 0)
83-
end,
84-
}):start()
77+
end)
78+
end)
8579
end
8680

8781
return M

lua/gitlab/server.lua

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
local state = require("gitlab.state")
55
local u = require("gitlab.utils")
66
local job = require("gitlab.job")
7-
local Job = require("plenary.job")
87
local M = {}
98

109
-- Builds the binary if it doesn't exist, and starts the server. If the pre-existing binary has an older
@@ -62,8 +61,6 @@ M.start = function(callback)
6261

6362
local settings = vim.json.encode(go_server_settings)
6463

65-
local stderr_buf = ""
66-
6764
local ok, err = pcall(vim.system, { state.settings.server.binary, settings }, {
6865
stdout = function(_, data)
6966
if data == nil or parsed_port ~= nil then
@@ -85,18 +82,12 @@ M.start = function(callback)
8582
end
8683
end
8784
end,
88-
stderr = function(_, data)
89-
if data == nil or data == "" then
90-
return
91-
end
92-
stderr_buf = stderr_buf .. data
93-
end,
9485
}, function(out)
9586
if out.code ~= 0 then
9687
vim.schedule(function()
9788
local msg = "Golang gitlab server exited: code: " .. out.code .. ", signal: " .. (out.signal or 0)
98-
if stderr_buf ~= "" then
99-
msg = msg .. ", msg: " .. vim.trim(stderr_buf)
89+
if out.stderr ~= "" then
90+
msg = msg .. ", msg: " .. vim.trim(out.stderr)
10091
end
10192
u.notify(msg, vim.log.levels.ERROR)
10293
end)
@@ -214,44 +205,34 @@ M.restart = function(cb)
214205
end)
215206
end
216207

217-
-- Returns the version (git tag) that was baked into the binary when it was last built
208+
-- Runs `callback` with the version (git tag) that was baked into the binary when it was last built.
218209
M.get_version = function(callback)
219210
if not state.go_server_running then
220211
u.notify("Gitlab server not running", vim.log.levels.ERROR)
221-
return nil
212+
return
222213
end
223214
local parent_dir = u.get_root_path()
224215

225216
local version_output = vim.system({ "git", "describe", "--tags", "--always" }, { cwd = parent_dir }):wait()
226217
local plugin_version = version_output.code == 0 and vim.trim(version_output.stdout) or "unknown"
227218

228-
local args =
229-
{ "--noproxy", "localhost", "-s", "-X", "GET", string.format("localhost:%s/version", state.settings.server.port) }
230-
231-
-- We call the "/version" endpoint here instead of through the regular jobs pattern because earlier versions of the plugin
232-
-- may not have it. We handle a 404 as an "unknown" version error.
233-
Job:new({
234-
command = "curl",
235-
args = args,
236-
on_stdout = function(_, output)
237-
vim.defer_fn(function()
238-
if output == nil then
239-
callback({ plugin_version = plugin_version, binary_version = "unknown" })
240-
return
241-
end
242-
243-
local data_ok, data = pcall(vim.json.decode, output)
244-
if not data_ok or data == nil or data.version == nil then
245-
callback({ plugin_version = plugin_version, binary_version = "unknown" })
246-
return
247-
end
219+
-- We call the "/version" endpoint here instead of through the regular run_job pattern because
220+
-- earlier versions of the plugin may not have the endpoint. We handle a 404 as an "unknown"
221+
-- version error.
222+
local cmd = {
223+
"curl",
224+
"--noproxy",
225+
"localhost",
226+
"-s",
227+
"-X",
228+
"GET",
229+
string.format("localhost:%s/version", state.settings.server.port),
230+
}
231+
local result = vim.system(cmd):wait()
232+
local _, data = pcall(vim.json.decode, result.stdout)
233+
local binary_version = data and data.version and data.version or "unknown"
248234

249-
callback({ plugin_version = plugin_version, binary_version = data.version })
250-
end, 0)
251-
end,
252-
on_stderr = function() end,
253-
on_exit = function() end,
254-
}):start()
235+
callback({ plugin_version = plugin_version, binary_version = binary_version })
255236
end
256237

257238
return M

0 commit comments

Comments
 (0)