Skip to content

Commit

Permalink
feat: implement toggling the dev log
Browse files Browse the repository at this point in the history
Revert to default botright 30vsplit

Respect user config in toggling dev log
  • Loading branch information
JordanH-Apadmi committed Jun 25, 2024
1 parent 5aa227f commit 6fdc518
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ require("flutter-tools").setup {} -- use defaults
- `FlutterSuper` - Go to super class, method using custom LSP method `dart/textDocument/super`.
- `FlutterReanalyze` - Forces LSP server reanalyze using custom LSP method `dart/reanalyze`.
- `FlutterRename` - Renames and updates imports if `lsp.settings.renameFilesWithClasses == "always"`
- `FlutterLogToggle` - Toggles the log window.
- `FlutterLogClear` - Clears the log window.

<hr/>

Expand Down
1 change: 1 addition & 0 deletions lua/flutter-tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ local function setup_commands()
nargs = "*",
})
--- Log
command("FlutterLogToggle", function(data) log.toggle_dev_log(config.dev_log) end)
command("FlutterLogClear", log.clear)
--- LSP
command("FlutterSuper", lsp.dart_lsp_super)
Expand Down
28 changes: 24 additions & 4 deletions lua/flutter-tools/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local ui = lazy.require("flutter-tools.ui") ---@module "flutter-tools.ui"
local utils = lazy.require("flutter-tools.utils") ---@module "flutter-tools.utils"

local api = vim.api
local fn = vim.fn
local fmt = string.format

local M = {
Expand All @@ -22,12 +23,22 @@ local function exists()
return is_valid
end

local function close_dev_log()

local function is_open()
local wins = fn.win_findbuf(M.buf)
return wins and #wins > 0
end

local function delete_dev_log()
M.buf = nil
M.win = nil
end

local function create(config)
local function close_dev_log()
if api.nvim_win_is_valid(M.win) then api.nvim_win_close(M.win, true) end
end

local function open_dev_log(config)
local opts = {
filename = M.filename,
filetype = "log",
Expand All @@ -40,13 +51,22 @@ local function create(config)
end
M.buf = buf
M.win = win

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

function M.toggle_dev_log(config)
if is_open() then
close_dev_log()
else
open_dev_log(config)
end
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 @@ -86,7 +106,7 @@ end
---@param opts table
function M.log(data, opts)
if opts.enabled then
if not exists() then create(opts) end
if not exists() then open_dev_log(opts) end
append(M.buf, { data })
autoscroll(M.buf, M.win)
end
Expand Down
4 changes: 2 additions & 2 deletions lua/flutter-tools/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ end
---@param on_open fun(buf: integer, win: integer)
---@return nil
function M.open_win(opts, on_open)
local open_cmd = opts.open_cmd or "botright 30vnew"
local open_cmd = opts.open_cmd or "botright 30vsplit"
local name = opts.filename or "__Flutter_Tools_Unknown__"
open_cmd = fmt("%s %s", open_cmd, name)

vim.cmd(open_cmd)

local win = api.nvim_get_current_win()
local buf = api.nvim_get_current_buf()
vim.bo[buf].filetype = opts.filetype
Expand Down

0 comments on commit 6fdc518

Please sign in to comment.