-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathrun.lua
More file actions
89 lines (77 loc) · 2.03 KB
/
run.lua
File metadata and controls
89 lines (77 loc) · 2.03 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
local class = require('java-core.utils.class')
local notify = require('java-core.utils.notify')
---@class java.Run
---@field name string
---@field main_class string
---@field buffer number
---@field is_running boolean
---@field is_manually_stoped boolean
---@field private term_chan_id number
---@field private job_chan_id number | nil
---@field private is_failure boolean
local Run = class()
---@param dap_config java-dap.DapLauncherConfig
function Run:_init(dap_config)
self.name = dap_config.name
self.main_class = dap_config.mainClass
self.buffer = vim.api.nvim_create_buf(false, true)
self.term_chan_id = vim.api.nvim_open_term(self.buffer, {
on_input = function(_, _, _, data)
self:send_job(data)
end,
})
end
---@param cmd string[]
function Run:start(cmd)
local merged_cmd = table.concat(cmd, ' ')
self.is_running = true
self:send_term(merged_cmd)
self.job_chan_id = vim.fn.jobstart(merged_cmd, {
pty = true,
on_stdout = function(_, data)
self:send_term(data)
end,
on_exit = function(_, exit_code)
self:on_job_exit(exit_code)
end,
})
end
function Run:stop()
if not self.job_chan_id then
return
end
self.is_manually_stoped = true
vim.fn.jobstop(self.job_chan_id)
vim.fn.jobwait({ self.job_chan_id }, 1000)
self.job_chan_id = nil
end
---@private
---Send data to execution job channel
---@param data string
function Run:send_job(data)
if self.job_chan_id then
vim.fn.chansend(self.job_chan_id, data)
end
end
---@private
---Send message to terminal channel
---@param data string
function Run:send_term(data)
vim.fn.chansend(self.term_chan_id, data)
end
---@private
---Runs when the current job exists
---@param exit_code number
function Run:on_job_exit(exit_code)
local message = string.format('Process finished with exit code::%s\n', exit_code)
self:send_term(message)
self.is_running = false
if exit_code == 0 or self.is_manually_stoped then
self.is_failure = false
self.is_manually_stoped = false
else
self.is_failure = true
notify.error(string.format('%s %s', self.name, message))
end
end
return Run