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

Commit

Permalink
refactor: keymaps
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Nov 29, 2023
1 parent 0f9487a commit f6a575f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
18 changes: 0 additions & 18 deletions lua/nvim-devdocs/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,4 @@ M.setup = function(new_config)
return default
end

---@param bufnr number
---@param entry DocEntry
M.set_keymaps = function(bufnr, entry)
local slug = entry.alias:gsub("-", "~")
local keymaps = M.options.mappings
local set_buf_keymap = function(key, action, description)
vim.keymap.set("n", key, action, { buffer = bufnr, desc = description })
end

if type(keymaps.open_in_browser) == "string" and keymaps.open_in_browser ~= "" then
set_buf_keymap(
keymaps.open_in_browser,
function() vim.ui.open("https://devdocs.io/" .. slug .. "/" .. entry.link) end,
"Open in the browser"
)
end
end

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

local config = require("nvim-devdocs.config")

local function set_buf_keymap(key, action, bufnr, description)
vim.keymap.set("n", key, action, { buffer = bufnr, desc = description })
end

local mappings = {
open_in_browser = {
desc = "Open in the browser",
handler = function(entry)
local slug = entry.alias:gsub("-", "~")
vim.ui.open("https://devdocs.io/" .. slug .. "/" .. entry.link)
end,
},
}

---@param bufnr number
---@param entry DocEntry
M.set_keymaps = function(bufnr, entry)
for map, key in pairs(config.options.mappings) do
if type(key) == "string" and key ~= "" then
local value = mappings[map]
if value then set_buf_keymap(key, function() value.handler(entry) end, bufnr, value.desc) end
end
end
end

return M
3 changes: 2 additions & 1 deletion lua/nvim-devdocs/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local list = require("nvim-devdocs.list")
local state = require("nvim-devdocs.state")
local notify = require("nvim-devdocs.notify")
local config = require("nvim-devdocs.config")
local keymaps = require("nvim-devdocs.keymaps")
local build_docs = require("nvim-devdocs.build")

local devdocs_site_url = "https://devdocs.io"
Expand Down Expand Up @@ -304,7 +305,7 @@ M.open = function(entry, bufnr, float)

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

config.set_keymaps(bufnr, entry)
keymaps.set_keymaps(bufnr, entry)
config.options.after_open(bufnr)
end

Expand Down

0 comments on commit f6a575f

Please sign in to comment.