|
1 | 1 | -- This module contains the logic responsible for building and starting |
2 | 2 | -- the Golang server. The Go server is responsible for making API calls |
3 | 3 | -- to Gitlab and returning the data |
4 | | -local List = require("gitlab.utils.list") |
5 | 4 | local state = require("gitlab.state") |
6 | 5 | local u = require("gitlab.utils") |
7 | 6 | local job = require("gitlab.job") |
@@ -62,58 +61,50 @@ M.start = function(callback) |
62 | 61 | state.chosen_mr_iid = 0 -- Do not let this interfere with subsequent reviewer.open() calls |
63 | 62 |
|
64 | 63 | local settings = vim.json.encode(go_server_settings) |
65 | | - if vim.fn.has("win32") then |
66 | | - settings = settings:gsub('"', '\\"') |
67 | | - end |
68 | | - |
69 | | - local command = string.format('"%s" "%s"', state.settings.server.binary, settings) |
70 | 64 |
|
71 | | - local job_id = vim.fn.jobstart(command, { |
72 | | - on_stdout = function(_, data) |
73 | | - -- if port was not provided then we need to parse it from output of server |
74 | | - if parsed_port == nil then |
75 | | - for _, line in ipairs(data) do |
76 | | - port = line:match("Server started on port:%s+(%d+)") |
77 | | - if port ~= nil then |
78 | | - parsed_port = port |
79 | | - state.settings.server.port = port |
80 | | - break |
81 | | - end |
82 | | - end |
83 | | - end |
| 65 | + local stderr_buf = "" |
84 | 66 |
|
85 | | - -- This assumes that first output of server will be parsable and port will be correctly set. |
86 | | - -- Make sure that this actually check if port was correctly parsed based on server output |
87 | | - -- because server outputs port only if it started successfully. |
88 | | - if parsed_port ~= nil and not callback_called then |
89 | | - state.go_server_running = true |
90 | | - callback() |
91 | | - callback_called = true |
| 67 | + local ok, err = pcall(vim.system, { state.settings.server.binary, settings }, { |
| 68 | + stdout = function(_, data) |
| 69 | + if data == nil or parsed_port ~= nil then |
| 70 | + return |
92 | 71 | end |
93 | | - end, |
94 | | - on_stderr = function(_, errors) |
95 | | - local err_msg = List.new(errors):reduce(function(agg, err) |
96 | | - if err ~= "" and err ~= nil then |
97 | | - agg = agg .. err .. "\n" |
| 72 | + for line in data:gmatch("[^\r\n]+") do |
| 73 | + local matched = line:match("Server started on port:%s+(%d+)") |
| 74 | + if matched ~= nil then |
| 75 | + parsed_port = matched |
| 76 | + vim.schedule(function() |
| 77 | + state.settings.server.port = matched |
| 78 | + state.go_server_running = true |
| 79 | + if not callback_called then |
| 80 | + callback_called = true |
| 81 | + callback() |
| 82 | + end |
| 83 | + end) |
| 84 | + break |
98 | 85 | end |
99 | | - return agg |
100 | | - end, "") |
101 | | - |
102 | | - if err_msg ~= "" then |
103 | | - u.notify(err_msg, vim.log.levels.ERROR) |
104 | 86 | end |
105 | 87 | end, |
106 | | - on_exit = function(job_id, exit_code) |
107 | | - if exit_code ~= 0 then |
108 | | - u.notify( |
109 | | - "Golang gitlab server exited: job_id: " .. job_id .. ", exit_code: " .. exit_code, |
110 | | - vim.log.levels.ERROR |
111 | | - ) |
| 88 | + stderr = function(_, data) |
| 89 | + if data == nil or data == "" then |
| 90 | + return |
112 | 91 | end |
| 92 | + stderr_buf = stderr_buf .. data |
113 | 93 | end, |
114 | | - }) |
115 | | - if job_id <= 0 then |
116 | | - u.notify("Could not start gitlab.nvim binary", vim.log.levels.ERROR) |
| 94 | + }, function(out) |
| 95 | + if out.code ~= 0 then |
| 96 | + vim.schedule(function() |
| 97 | + 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) |
| 100 | + end |
| 101 | + u.notify(msg, vim.log.levels.ERROR) |
| 102 | + end) |
| 103 | + end |
| 104 | + end) |
| 105 | + |
| 106 | + if not ok then |
| 107 | + u.notify("Could not start gitlab.nvim binary: " .. tostring(err), vim.log.levels.ERROR) |
117 | 108 | end |
118 | 109 | end |
119 | 110 |
|
|
0 commit comments