-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.lua
More file actions
116 lines (102 loc) · 2.71 KB
/
cli.lua
File metadata and controls
116 lines (102 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
local M = {}
local config = require("coderabbit.config")
function M.is_available()
local cfg = config.get()
return vim.fn.executable(cfg.cli.binary) == 1
end
--- Build the command table for `cr review --agent`.
local function build_cmd(opts)
local cfg = config.get()
local cmd = { cfg.cli.binary, "review", "--agent", "--no-color" }
local review_type = opts.type or cfg.review.type
if review_type then
table.insert(cmd, "--type")
table.insert(cmd, review_type)
end
local base = opts.base or cfg.review.base
if base then
table.insert(cmd, "--base")
table.insert(cmd, base)
end
local base_commit = opts.base_commit or cfg.review.base_commit
if base_commit then
table.insert(cmd, "--base-commit")
table.insert(cmd, base_commit)
end
for _, arg in ipairs(cfg.cli.extra_args) do
table.insert(cmd, arg)
end
return cmd
end
--- Run a review asynchronously.
--- @param opts table Review options (type, base, base_commit)
--- @param callbacks table { on_line, on_exit }
--- on_line(line: string) — called for each stdout line
--- on_exit(code: number, stderr: string) — called on process exit
--- @return number job_id
function M.review(opts, callbacks)
local cmd = build_cmd(opts)
local stderr_chunks = {}
local cfg = config.get()
local timer = nil
local timed_out = false
local job_id = vim.fn.jobstart(cmd, {
cwd = vim.fn.getcwd(),
stdout_buffered = false,
on_stdout = function(_, data, _)
if not data then
return
end
for _, line in ipairs(data) do
if line ~= "" then
vim.schedule(function()
if callbacks.on_line then
callbacks.on_line(line)
end
end)
end
end
end,
on_stderr = function(_, data, _)
if data then
for _, line in ipairs(data) do
if line ~= "" then
table.insert(stderr_chunks, line)
end
end
end
end,
on_exit = function(_, code, _)
if timer then
vim.fn.timer_stop(timer)
end
if timed_out then
return
end
local stderr = table.concat(stderr_chunks, "\n")
vim.schedule(function()
if callbacks.on_exit then
callbacks.on_exit(code, stderr)
end
end)
end,
})
if job_id > 0 and cfg.cli.timeout > 0 then
timer = vim.fn.timer_start(cfg.cli.timeout, function()
timed_out = true
vim.fn.jobstop(job_id)
vim.schedule(function()
if callbacks.on_exit then
callbacks.on_exit(-1, "Review timed out")
end
end)
end)
end
return job_id
end
function M.cancel(job_id)
if job_id and job_id > 0 then
pcall(vim.fn.jobstop, job_id)
end
end
return M