From f6a575fd27181f81e00f6eb16941f426ff30ac31 Mon Sep 17 00:00:00 2001 From: Luckas Date: Wed, 29 Nov 2023 17:05:58 +0300 Subject: [PATCH] refactor: keymaps --- lua/nvim-devdocs/config.lua | 18 ------------------ lua/nvim-devdocs/keymaps.lua | 30 ++++++++++++++++++++++++++++++ lua/nvim-devdocs/operations.lua | 3 ++- 3 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 lua/nvim-devdocs/keymaps.lua diff --git a/lua/nvim-devdocs/config.lua b/lua/nvim-devdocs/config.lua index 7d008f2..007a59c 100644 --- a/lua/nvim-devdocs/config.lua +++ b/lua/nvim-devdocs/config.lua @@ -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 diff --git a/lua/nvim-devdocs/keymaps.lua b/lua/nvim-devdocs/keymaps.lua new file mode 100644 index 0000000..75d2d08 --- /dev/null +++ b/lua/nvim-devdocs/keymaps.lua @@ -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 diff --git a/lua/nvim-devdocs/operations.lua b/lua/nvim-devdocs/operations.lua index da1d7b4..d7754de 100644 --- a/lua/nvim-devdocs/operations.lua +++ b/lua/nvim-devdocs/operations.lua @@ -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" @@ -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