Skip to content

Commit bc36e2e

Browse files
authored
feat(log): add toggle command for log buffer (#411)
- Added a new command `FlutterLogToggle` to toggle the visibility of the log buffer. - Updated the documentation to include the new command. Modified the log function to use the same buffer number when appending lines. - Adjusted the open command for the log buffer to use a horizontal split.
1 parent 377f21c commit bc36e2e

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ 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+
- `FlutterLogClear` - Clears the log buffer.
169+
- `FlutterLogToggle` - Toggles the log buffer.
170+
168171

169172
<hr/>
170173

@@ -253,7 +256,7 @@ require("flutter-tools").setup {
253256
-- takes a log_line as string argument; returns a boolean or nil;
254257
-- the log_line is only added to the output if the function returns true
255258
notify_errors = false, -- if there is an error whilst running then notify the user
256-
open_cmd = "botright 30vnew", -- command to use to open the log buffer
259+
open_cmd = "15split", -- command to use to open the log buffer
257260
focus_on_open = true, -- focus on the newly opened log window
258261
},
259262
dev_tools = {

doc/flutter-tools.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ APP VERSION
198198
- `FlutterSuper` - Go to super class, method using custom LSP method `dart/textDocument/super`.
199199
- `FlutterReanalyze` - Forces LSP server reanalyze using custom LSP method `dart/reanalyze`.
200200
- `FlutterRename` - Renames and updates imports if `lsp.settings.renameFilesWithClasses == "always"`
201+
- `FlutterLogClear` - Clears the log buffer.
202+
- `FlutterLogToggle` - Toggles the log buffer.
201203

202204

203205
FLUTTERRUN ~
@@ -289,7 +291,7 @@ both are set.
289291
-- takes a log_line as string argument; returns a boolean or nil;
290292
-- the log_line is only added to the output if the function returns true
291293
notify_errors = false, -- if there is an error whilst running then notify the user
292-
open_cmd = "botright 30vnew", -- command to use to open the log buffer
294+
open_cmd = "15split", -- command to use to open the log buffer
293295
focus_on_open = true, -- focus on the newly opened log window
294296
},
295297
dev_tools = {

lua/flutter-tools.lua

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ local function setup_commands()
4646
})
4747
--- Log
4848
command("FlutterLogClear", log.clear)
49+
command("FlutterLogToggle", log.toggle)
4950
--- LSP
5051
command("FlutterSuper", lsp.dart_lsp_super)
5152
command("FlutterReanalyze", lsp.dart_reanalyze)

lua/flutter-tools/commands.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ end
6666
---@param is_err boolean if this is stdout or stderr
6767
local function on_run_data(is_err, data)
6868
if is_err and config.dev_log.notify_errors then ui.notify(data, ui.ERROR, { timeout = 5000 }) end
69-
dev_log.log(data, config.dev_log)
69+
dev_log.log(data)
7070
end
7171

7272
local function shutdown()

lua/flutter-tools/log.lua

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local lazy = require("flutter-tools.lazy")
22
local ui = lazy.require("flutter-tools.ui") ---@module "flutter-tools.ui"
33
local utils = lazy.require("flutter-tools.utils") ---@module "flutter-tools.utils"
4+
local config = lazy.require("flutter-tools.config") ---@module "flutter-tools.config"
45

56
local api = vim.api
67
local fmt = string.format
@@ -91,8 +92,8 @@ end
9192
--- Open a log showing the output from a command
9293
--- in this case flutter run
9394
---@param data string
94-
---@param opts table
95-
function M.log(data, opts)
95+
function M.log(data)
96+
local opts = config.dev_log
9697
if opts.enabled then
9798
if not exists() then create(opts) end
9899
if opts.filter and not opts.filter(data) then return end
@@ -117,4 +118,19 @@ function M.clear()
117118
end
118119
end
119120

121+
M.toggle = function()
122+
local wins = vim.api.nvim_list_wins()
123+
for _, id in pairs(wins) do
124+
local bufnr = vim.api.nvim_win_get_buf(id)
125+
if vim.api.nvim_buf_get_name(bufnr):match(".*/([^/]+)$") == M.filename then
126+
return vim.api.nvim_win_close(id, true)
127+
end
128+
end
129+
create(config.dev_log)
130+
autoscroll(M.buf, M.win)
131+
-- Auto scroll to bottom
132+
local buf_length = vim.api.nvim_buf_line_count(M.buf)
133+
pcall(vim.api.nvim_win_set_cursor, M.win, { buf_length, 0 })
134+
end
135+
120136
return M

0 commit comments

Comments
 (0)