From 72be8c6b99c8b04c961a71c2a14464bfe5a63faf Mon Sep 17 00:00:00 2001 From: j-hui Date: Sat, 9 Dec 2023 16:01:39 -0500 Subject: [PATCH] feat: integrate with nvim-tree to avoid collisions close #163 --- README.md | 21 ++++++---- doc/fidget-option.txt | 19 ++++++++++ lua/fidget.lua | 1 + lua/fidget/integration.lua | 9 +++++ lua/fidget/integration/nvim-tree.lua | 57 ++++++++++++++++++++-------- lua/fidget/notification/window.lua | 2 +- scripts/build-options-docs.lua | 2 +- scripts/build-options-docs.sh | 1 + 8 files changed, 88 insertions(+), 24 deletions(-) create mode 100644 lua/fidget/integration.lua diff --git a/README.md b/README.md index fa2c624..11f5d04 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Extensible UI for Neovim notifications and LSP progress messages.
Demo setup -*Note that this demo may not always reflect the exact behavior of the latest release.* +_Note that this demo may not always reflect the exact behavior of the latest release._ This screen recording was taken as I opened a Rust file I'm working on, triggering `rust-analyzer` to send me some LSP progress messages. @@ -45,12 +45,12 @@ rendering and make the demo look glitchy...) Visible elements: -- Terminal + font: [Kitty](https://sw.kovidgoyal.net/kitty/) + [Comic Shanns Mono](https://github.com/shannpersand/comic-shanns) -- Editor: [Neovim v0.9.4](https://github.com/neovim/neovim/tree/v0.9.4) -- Theme: [catppuccin/nvim (mocha, dark)](https://github.com/catppuccin/nvim) -- Status line: [nvim-lualine/lualine.nvim](https://github.com/nvim-lualine/lualine.nvim) -- Color columns: `:set colorcolumn=81,121,+1,+2` (sorry) -- Scrollbar: [petertriho/nvim-scrollbar](https://github.com/petertriho/nvim-scrollbar) +- Terminal + font: [Kitty](https://sw.kovidgoyal.net/kitty/) + [Comic Shanns Mono](https://github.com/shannpersand/comic-shanns) +- Editor: [Neovim v0.9.4](https://github.com/neovim/neovim/tree/v0.9.4) +- Theme: [catppuccin/nvim (mocha, dark)](https://github.com/catppuccin/nvim) +- Status line: [nvim-lualine/lualine.nvim](https://github.com/nvim-lualine/lualine.nvim) +- Color columns: `:set colorcolumn=81,121,+1,+2` (sorry) +- Scrollbar: [petertriho/nvim-scrollbar](https://github.com/petertriho/nvim-scrollbar)
@@ -221,6 +221,13 @@ For instance, using [Lazy](https://github.com/folke/lazy.nvim): }, }, + -- Options related to integrating with other plugins + integration = { + ["nvim-tree"] = { + enable = true, -- Integrate with nvim-tree/nvim-tree.lua (if installed) + }, + }, + -- Options related to logging logger = { level = vim.log.levels.WARN, -- Minimum logging level diff --git a/doc/fidget-option.txt b/doc/fidget-option.txt index e8f9952..8122756 100644 --- a/doc/fidget-option.txt +++ b/doc/fidget-option.txt @@ -18,6 +18,7 @@ Nvim LSP client options ......................... |fidget.option.progress.lsp| Notification options ............................ |fidget.option.notification| Notifications rendering options ............ |fidget.option.notification.view| Notifications window options ............. |fidget.option.notification.window| +nvim-tree integration .................. |fidget.option.integration.nvim-tree| Logging options ....................................... |fidget.option.logger| @@ -721,6 +722,24 @@ notification.window.relative *fidget.option.notification.window.relative* `"editor"` +============================================================================== +nvim-tree integration *fidget.option.integration.nvim-tree* + + +integration.nvim-tree.enable *fidget.option.integration.nvim-tree.enable* + + Integrate with nvim-tree/nvim-tree.lua (if installed) + + Dynamically offset Fidget's notifications window when the nvim-tree window + is open on the right side + the Fidget window is "editor"-relative. + + Type: ~ + `boolean` + + Default: ~ + `true` + + ============================================================================== Logging options *fidget.option.logger* diff --git a/lua/fidget.lua b/lua/fidget.lua index 37e5d14..e09dfe1 100644 --- a/lua/fidget.lua +++ b/lua/fidget.lua @@ -33,6 +33,7 @@ fidget.options = { progress = fidget.progress, notification = fidget.notification, logger = fidget.logger, + integration = require("fidget.integration") } ---@options ]] diff --git a/lua/fidget/integration.lua b/lua/fidget/integration.lua new file mode 100644 index 0000000..d0e36c8 --- /dev/null +++ b/lua/fidget/integration.lua @@ -0,0 +1,9 @@ +local M = {} + +M.options = { + ["nvim-tree"] = require("fidget.integration.nvim-tree") +} + +require("fidget.options").declare(M, "integration", M.options) + +return M diff --git a/lua/fidget/integration/nvim-tree.lua b/lua/fidget/integration/nvim-tree.lua index 70f38b4..89ab991 100644 --- a/lua/fidget/integration/nvim-tree.lua +++ b/lua/fidget/integration/nvim-tree.lua @@ -1,26 +1,53 @@ local M = {} -require("fidget.options").declare(M, "integration.nvim-tree", { - enable = false, -}, function() - if M.option.enable == true then - local ntree = require("nvim-tree") - local win = require("fidget.notification.window") +-- Only register one callback, so setup() can be called multiple times. +local already_subscribed = false - ntree.api.events.subscribe(ntree.api.events.TreeOpen, function() - if win.relative == "editor" then +---@options integration.nvim-tree [[ +--- nvim-tree integration +M.options = { + --- Integrate with nvim-tree/nvim-tree.lua (if installed) + --- + --- Dynamically offset Fidget's notifications window when the nvim-tree window + --- is open on the right side + the Fidget window is "editor"-relative. + --- + ---@type boolean + enable = true, +} +---@options ]] - end - end) +require("fidget.options").declare(M, "integration.nvim-tree", M.options, function() + if not M.options.enable or already_subscribed then + return + end + + local ok, api = pcall(function() return require("nvim-tree.api") end) + if not ok then + return + end - ntree.api.events.subscribe(ntree.api.events.TreeClose, function() - win.set_x_offset(0) - end) + already_subscribed = true - ntree.api.events.subscripe(ntree.api.events.Resize, function(size) + local win = require("fidget.notification.window") - end) + local function resize() + if win.options.relative == "editor" then + local winid = api.tree.winid() + local col = vim.api.nvim_win_get_position(winid)[2] + if col > 1 then + local width = vim.api.nvim_win_get_width(winid) + win.set_x_offset(width) + end + end + end + + local function reset() + win.set_x_offset(0) end + + api.events.subscribe(api.events.Event.TreeOpen, resize) + api.events.subscribe(api.events.Event.Resize, resize) + api.events.subscribe(api.events.Event.TreeClose, reset) end) return M diff --git a/lua/fidget/notification/window.lua b/lua/fidget/notification/window.lua index ee94171..3d8d7b1 100644 --- a/lua/fidget/notification/window.lua +++ b/lua/fidget/notification/window.lua @@ -135,7 +135,7 @@ local state = { --- Useful for temporarily adding additional padding to account for space --- taken up by other plugins' windows. --- - ---@type number|nil + ---@type number x_offset = 0, } diff --git a/scripts/build-options-docs.lua b/scripts/build-options-docs.lua index 9c60e04..7b7c40e 100644 --- a/scripts/build-options-docs.lua +++ b/scripts/build-options-docs.lua @@ -129,7 +129,7 @@ for _, filename in ipairs(files) do done = false -- Look for ---@options (prefix) [[ - found, _, cap1 = string.find(line, "%-%-%-@options%s*([%w%.]*)%s*%[%[") + found, _, cap1 = string.find(line, "%-%-%-@options%s*([%w%._-]*)%s*%[%[") if found then if module.prefix ~= nil then print("Error: file contains multiple @options tags: " .. filename) diff --git a/scripts/build-options-docs.sh b/scripts/build-options-docs.sh index 1f97bda..5bb2a09 100755 --- a/scripts/build-options-docs.sh +++ b/scripts/build-options-docs.sh @@ -36,6 +36,7 @@ $lua scripts/build-options-docs.lua --strip 1 --tag fidget.option \ lua/fidget/notification.lua \ lua/fidget/notification/view.lua \ lua/fidget/notification/window.lua \ + lua/fidget/integration/nvim-tree.lua \ lua/fidget/logger.lua \ >> "$txt"