Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
feat: toggle floating window (#67)
Browse files Browse the repository at this point in the history
Add `DevdocsToggle` command for toggling floating windows.
  • Loading branch information
luckasRanarison committed Nov 29, 2023
1 parent f6a575f commit b5d896a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Available commands:
- `DevdocsOpenFloat`: Open documentation in a floating window, 0 or 1 arg.
- `DevdocsOpenCurrent`: Open documentation for the current filetype in a normal buffer.
- `DevdocsOpenCurrentFloat`: Open documentation for the current filetype in a floating window.
- `DevdocsToggle`: Toggle floating window.
- `DevdocsUpdate`: Update documentation, 0-n args.
- `DevdocsUpdateAll`: Update all documentations.

Expand Down
13 changes: 13 additions & 0 deletions lua/nvim-devdocs/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,17 @@ M.setup = function(new_config)
return default
end

M.get_float_options = function()
local ui = vim.api.nvim_list_uis()[1]
local row = (ui.height - M.options.float_win.height) * 0.5
local col = (ui.width - M.options.float_win.width) * 0.5
local float_opts = M.options.float_win

float_opts.row = M.options.float_win.row or row
float_opts.col = M.options.float_win.col or col
float_opts.zindex = 10

return float_opts
end

return M
17 changes: 17 additions & 0 deletions lua/nvim-devdocs/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

local list = require("nvim-devdocs.list")
local state = require("nvim-devdocs.state")
local notify = require("nvim-devdocs.notify")
local pickers = require("nvim-devdocs.pickers")
local operations = require("nvim-devdocs.operations")
Expand Down Expand Up @@ -75,6 +76,21 @@ M.update_all = function()
end
end

M.toggle = function()
local buf = state.get("last_buf")
local win = state.get("last_win")

if not buf then return end

if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true)
state.set("last_win", nil)
else
win = vim.api.nvim_open_win(buf, true, config.get_float_options())
state.set("last_win", win)
end
end

M.keywordprg = function(args)
local keyword = args.fargs[1]

Expand Down Expand Up @@ -104,6 +120,7 @@ M.setup = function(opts)
cmd("DevdocsKeywordprg", M.keywordprg, { nargs = "?" })
cmd("DevdocsUpdate", M.update, { nargs = "*", complete = completion.get_updatable })
cmd("DevdocsUpdateAll", M.update_all, {})
cmd("DevdocsToggle", M.toggle, {})
end

return M
11 changes: 2 additions & 9 deletions lua/nvim-devdocs/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,9 @@ M.open = function(entry, bufnr, float)
if not float then
vim.api.nvim_set_current_buf(bufnr)
else
local ui = vim.api.nvim_list_uis()[1]
local row = (ui.height - config.options.float_win.height) * 0.5
local col = (ui.width - config.options.float_win.width) * 0.5
local float_opts = config.options.float_win

float_opts.row = config.options.float_win.row or row
float_opts.col = config.options.float_win.col or col
float_opts.zindex = 10

local win = nil
local last_win = state.get("last_win")
local float_opts = config.get_float_options()

if last_win and vim.api.nvim_win_is_valid(last_win) then
win = last_win
Expand All @@ -305,6 +297,7 @@ M.open = function(entry, bufnr, float)

vim.bo[bufnr].keywordprg = ":DevdocsKeywordprg"

state.set("last_buf", bufnr)
keymaps.set_keymaps(bufnr, entry)
config.options.after_open(bufnr)
end
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-devdocs/state.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
local M = {}

---@alias nvim_devdocs.StateKey "current_doc" | "preview_lines" | "last_win" | "last_mode" | "last_buf"

local state = {
current_doc = nil, -- ex: "javascript", used for `keywordprg`
preview_lines = nil,
last_win = nil,
last_mode = nil, -- "normal" | "float"
last_buf = nil,
}

---@param key nvim_devdocs.StateKey
---@return any
M.get = function(key) return state[key] end

---@param key nvim_devdocs.StateKey
M.set = function(key, value) state[key] = value end

return M

0 comments on commit b5d896a

Please sign in to comment.