Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lua/flutter-tools/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,31 @@ local function has_recoverable_error(lines)
return false, nil
end

local ERROR_NOTIFY_DELAY_MS = 200
---@type string[]
local pending_errors = {}
local error_timer = nil

local function flush_errors()
if #pending_errors == 0 then return end
local lines = pending_errors
pending_errors = {}
ui.notify(lines, ui.ERROR, { timeout = 5000 })
end

---stderr arrives one line at a time, so collect a burst of lines into a single notification
---@param line string
local function queue_error_notification(line)
table.insert(pending_errors, line)
if not error_timer then error_timer = assert(vim.uv.new_timer()) end
error_timer:stop()
error_timer:start(ERROR_NOTIFY_DELAY_MS, 0, vim.schedule_wrap(flush_errors))
end

---Handle output from flutter run command
---@param is_err boolean if this is stdout or stderr
local function on_run_data(is_err, data)
if is_err and config.dev_log.notify_errors then ui.notify(data, ui.ERROR, { timeout = 5000 }) end
if is_err and config.dev_log.notify_errors then queue_error_notification(data) end
update_device_from_output(data)
dev_log.log(data)
end
Expand Down Expand Up @@ -660,6 +681,7 @@ if __TEST then
M.__get_run_args = get_run_args
M.__update_device_from_output = update_device_from_output
M.__set_current_device = set_current_device
M.__on_run_data = on_run_data
end

return M
4 changes: 2 additions & 2 deletions lua/flutter-tools/runners/debugger_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function DebuggerRunner:run(
if body and body.output then
for line in body.output:gmatch("[^\r\n]+") do
if not started then table.insert(before_start_logs, line) end
on_run_data(body.category == "sterr", line)
on_run_data(body.category == "stderr", line)
end
end
end,
Expand Down Expand Up @@ -305,7 +305,7 @@ function DebuggerRunner:attach(paths, args, cwd, on_run_data, on_run_exit)
if body and body.output then
for line in body.output:gmatch("[^\r\n]+") do
if not started then table.insert(before_start_logs, line) end
on_run_data(body.category == "sterr", line)
on_run_data(body.category == "stderr", line)
end
end
end, function(before_start_logs) on_run_exit(before_start_logs, args) end)
Expand Down
19 changes: 19 additions & 0 deletions tests/commands_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,23 @@ describe("commands", function()

assert.is_nil(commands.current_device())
end)

it("should batch consecutive error lines into one notification", function()
local config = require("flutter-tools.config")
local ui = require("flutter-tools.ui")
local original_notify = ui.notify
local notifications = {}
ui.notify = function(msg) table.insert(notifications, msg) end
config.dev_log.notify_errors = true

commands.__on_run_data(true, "Error: first line")
commands.__on_run_data(true, "second line")
commands.__on_run_data(false, "regular output")
commands.__on_run_data(true, "third line")
vim.wait(1000, function() return #notifications > 0 end)

config.dev_log.notify_errors = false
ui.notify = original_notify
assert.are.same({ { "Error: first line", "second line", "third line" } }, notifications)
end)
end)
Loading