Skip to content

Commit 6fdc518

Browse files
feat: implement toggling the dev log
Revert to default botright 30vsplit Respect user config in toggling dev log
1 parent 5aa227f commit 6fdc518

4 files changed

Lines changed: 29 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ require("flutter-tools").setup {} -- use defaults
165165
- `FlutterSuper` - Go to super class, method using custom LSP method `dart/textDocument/super`.
166166
- `FlutterReanalyze` - Forces LSP server reanalyze using custom LSP method `dart/reanalyze`.
167167
- `FlutterRename` - Renames and updates imports if `lsp.settings.renameFilesWithClasses == "always"`
168+
- `FlutterLogToggle` - Toggles the log window.
169+
- `FlutterLogClear` - Clears the log window.
168170

169171
<hr/>
170172

lua/flutter-tools.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ local function setup_commands()
4848
nargs = "*",
4949
})
5050
--- Log
51+
command("FlutterLogToggle", function(data) log.toggle_dev_log(config.dev_log) end)
5152
command("FlutterLogClear", log.clear)
5253
--- LSP
5354
command("FlutterSuper", lsp.dart_lsp_super)

lua/flutter-tools/log.lua

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local ui = lazy.require("flutter-tools.ui") ---@module "flutter-tools.ui"
33
local utils = lazy.require("flutter-tools.utils") ---@module "flutter-tools.utils"
44

55
local api = vim.api
6+
local fn = vim.fn
67
local fmt = string.format
78

89
local M = {
@@ -22,12 +23,22 @@ local function exists()
2223
return is_valid
2324
end
2425

25-
local function close_dev_log()
26+
27+
local function is_open()
28+
local wins = fn.win_findbuf(M.buf)
29+
return wins and #wins > 0
30+
end
31+
32+
local function delete_dev_log()
2633
M.buf = nil
2734
M.win = nil
2835
end
2936

30-
local function create(config)
37+
local function close_dev_log()
38+
if api.nvim_win_is_valid(M.win) then api.nvim_win_close(M.win, true) end
39+
end
40+
41+
local function open_dev_log(config)
3142
local opts = {
3243
filename = M.filename,
3344
filetype = "log",
@@ -40,13 +51,22 @@ local function create(config)
4051
end
4152
M.buf = buf
4253
M.win = win
54+
4355
api.nvim_create_autocmd("BufWipeout", {
4456
buffer = buf,
45-
callback = close_dev_log,
57+
callback = delete_dev_log,
4658
})
4759
end)
4860
end
4961

62+
function M.toggle_dev_log(config)
63+
if is_open() then
64+
close_dev_log()
65+
else
66+
open_dev_log(config)
67+
end
68+
end
69+
5070
function M.get_content()
5171
if M.buf then return api.nvim_buf_get_lines(M.buf, 0, -1, false) end
5272
end
@@ -86,7 +106,7 @@ end
86106
---@param opts table
87107
function M.log(data, opts)
88108
if opts.enabled then
89-
if not exists() then create(opts) end
109+
if not exists() then open_dev_log(opts) end
90110
append(M.buf, { data })
91111
autoscroll(M.buf, M.win)
92112
end

lua/flutter-tools/ui.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ end
133133
---@param on_open fun(buf: integer, win: integer)
134134
---@return nil
135135
function M.open_win(opts, on_open)
136-
local open_cmd = opts.open_cmd or "botright 30vnew"
136+
local open_cmd = opts.open_cmd or "botright 30vsplit"
137137
local name = opts.filename or "__Flutter_Tools_Unknown__"
138138
open_cmd = fmt("%s %s", open_cmd, name)
139-
140139
vim.cmd(open_cmd)
140+
141141
local win = api.nvim_get_current_win()
142142
local buf = api.nvim_get_current_buf()
143143
vim.bo[buf].filetype = opts.filetype

0 commit comments

Comments
 (0)