Skip to content
Merged
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
36 changes: 31 additions & 5 deletions lua/flutter-tools/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local M = {
buf = nil,
--@type integer
win = nil,
--@type boolean
shown = false,
}

M.filename = "__FLUTTER_DEV_LOG__"
Expand All @@ -28,7 +30,15 @@ local function close_dev_log()
M.win = nil
end

local function track_wipeout(buf)
api.nvim_create_autocmd("BufWipeout", {
buffer = buf,
callback = close_dev_log,
})
end

local function create(config)
M.shown = true
local opts = {
filename = M.filename,
filetype = "log",
Expand All @@ -42,13 +52,23 @@ local function create(config)
end
M.buf = buf
M.win = win
api.nvim_create_autocmd("BufWipeout", {
buffer = buf,
callback = close_dev_log,
})
track_wipeout(buf)
end)
end

local function create_hidden()
local buf = vim.fn.bufadd(M.filename)
vim.bo[buf].swapfile = false
vim.bo[buf].buftype = "nofile"
vim.bo[buf].buflisted = false
vim.fn.bufload(buf)
vim.bo[buf].filetype = "log"
vim.bo[buf].modifiable = false
M.buf = buf
M.win = nil
track_wipeout(buf)
end

function M.get_content()
if M.buf then return api.nvim_buf_get_lines(M.buf, 0, -1, false) end
end
Expand Down Expand Up @@ -95,7 +115,13 @@ end
function M.log(data)
local opts = config.dev_log
if opts.enabled then
if not exists() then create(opts) end
if not exists() then
if M.shown then
create_hidden()
else
create(opts)
end
end
if opts.filter and not opts.filter(data) then return end
append(M.buf, { data })
autoscroll(M.buf, M.win)
Expand Down
Loading