Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default keymaps #358

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions lua/kulala/config/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local FS = require("kulala.utils.fs")
local keymaps = require("kulala.config.keymaps")
local M = {}

M.defaults = {
Expand All @@ -9,9 +10,6 @@ M.defaults = {
-- Display mode
-- possible values: "split", "float"
display_mode = "split",
-- q to close the float (only used when display_mode is set to "float")
-- possible values: true, false
q_to_close_float = false,
-- split direction
-- possible values: "vertical", "horizontal"
split_direction = "vertical",
Expand Down Expand Up @@ -90,6 +88,42 @@ M.defaults = {
-- this will show the variable name and value as float
-- possible values: false, "float"
show_variable_info_text = false,

-- set to true to enable default keymaps (check docs or {plugins_path}/kulala.nvim/lua/kulala/config/keymaps.lua for details)
-- or override default keymaps as shown in the example below.
---@type boolean|table
global_keymaps = false,
--[[
{
["Send request"] = { -- sets global mapping
"<leader>Rs",
function() require("kulala").run() end,
mode = { "n", "v" }, -- optional mode, default is v
desc = "Send request" -- optional description, otherwise inferred from the key
},
["Send all requests"] = {
"<leader>Ra",
function() require("kulala").run_all() end,
mode = { "n", "v" },
ft = "http", -- sets mapping for *.http files only
},
["Replay the last request"] = {
"<leader>Rr",
function() require("kulala").replay() end,
ft = { "http", "rest" }, -- sets mapping for specified file types
},
["Find request"] = false -- set to false to disable
},
]]

-- Kulala UI keymaps, override with custom keymaps as required (check docs or {plugins_path}/kulala.nvim/lua/kulala/config/keymaps.lua for details)
---@type boolean|table
kulala_keymaps = true,
--[[
{
["Show headers"] = { "H", function() require("kulala.ui").show_headers() end, },
}
]]
}

M.default_contenttype = {
Expand All @@ -102,6 +136,7 @@ M.options = M.defaults

M.setup = function(config)
M.options = vim.tbl_deep_extend("force", M.defaults, config or {})
keymaps.setup_global_keymaps()
end

M.set = function(config)
Expand Down
249 changes: 249 additions & 0 deletions lua/kulala/config/keymaps.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
local M = {}

M.default_global_keymaps = {
["Open scratchpad"] = {
"<leader>Rb",
function()
require("kulala").scratchpad()
end,
},
["Open kulala"] = {
"<leader>Ro",
function()
require("kulala").open()
end,
},
["Close window"] = {
"<leader>Rq",
function()
require("kulala").close()
end,
ft = { "http", "rest" },
},
["Copy as cURL"] = {
"<leader>Rc",
function()
require("kulala").copy()
end,
ft = { "http", "rest" },
},
["Paste from curl"] = {
"<leader>RC",
function()
require("kulala").from_curl()
end,
ft = { "http", "rest" },
},
["Inspect current request"] = {
"<leader>Ri",
function()
require("kulala").inspect()
end,
ft = { "http", "rest" },
},
["Select environment"] = {
"<leader>Re",
function()
require("kulala").set_selected_env()
end,
ft = { "http", "rest" },
},
["Send request"] = {
"<leader>Rs",
function()
require("kulala").run()
end,
mode = { "n", "v" },
},
["Send request <cr>"] = {
"<CR>",
function()
require("kulala").run()
end,
mode = { "n", "v" },
ft = { "http", "rest" },
},
["Send all requests"] = {
"<leader>Ra",
function()
require("kulala").run_all()
end,
mode = { "n", "v" },
},
["Replay the last request"] = {
"<leader>Rr",
function()
require("kulala").replay()
end,
},
["Download GraphQL schema"] = {
"<leader>Rg",
function()
require("kulala").download_graphql_schema()
end,
ft = { "http", "rest" },
},
["Jump to next request"] = {
"<leader>Rn",
function()
require("kulala").jump_next()
end,
ft = { "http", "rest" },
},
["Jump to previous request"] = {
"<leader>Rp",
function()
require("kulala").jump_prev()
end,
ft = { "http", "rest" },
},
["Find request"] = {
"<leader>Rf",
function()
require("kulala").search()
end,
ft = { "http", "rest" },
},
["Toggle headers/body"] = {
"<leader>Rt",
function()
require("kulala").toggle_view()
end,
ft = { "http", "rest" },
},
["Show stats"] = {
"<leader>RS",
function()
require("kulala").show_stats()
end,
ft = { "http", "rest" },
},
}

-- Keymaps for Kulala window only
M.default_kulala_keymaps = {
["Show headers"] = {
"H",
function()
require("kulala.ui").show_headers()
end,
},
["Show body"] = {
"B",
function()
require("kulala.ui").show_body()
end,
},
["Show headers and body"] = {
"A",
function()
require("kulala.ui").show_headers_body()
end,
},
["Show verbose"] = {
"V",
function()
require("kulala.ui").show_verbose()
end,
},
["Show script output"] = {
"O",
function()
require("kulala.ui").show_script_output()
end,
},
["Show stats"] = {
"S",
function()
require("kulala.ui").show_stats()
end,
},
["Close"] = {
"q",
function()
require("kulala.ui").close_kulala_buffer()
end,
},
}

local function collect_global_keymaps()
local config = require("kulala.config")
local config_global_keymaps = config.options.global_keymaps
local global_keymaps, ft_keymaps = {}, {}

if not config_global_keymaps then
return
end

config_global_keymaps = type(config_global_keymaps) == "table"
and vim.tbl_extend("force", M.default_global_keymaps, config_global_keymaps)
or M.default_global_keymaps

vim.iter(config_global_keymaps):each(function(name, map)
if map then
map.desc = map.desc or name

if map.ft then
vim.iter({ map.ft }):flatten():each(function(ft)
ft_keymaps[ft] = vim.list_extend(ft_keymaps[ft] or {}, { map })
end)
else
global_keymaps = vim.list_extend(global_keymaps, { map })
end
end
end)

return global_keymaps, ft_keymaps
end

local function set_keymap(map, buf)
vim.keymap.set(map.mode or "n", map[1], map[2], { buffer = buf, desc = map.desc, silent = true })
end

local function create_ft_autocommand(ft, maps)
vim.api.nvim_create_autocmd({ "BufEnter", "BufRead", "BufNewFile", "BufFilePost" }, {
group = vim.api.nvim_create_augroup("Kulala filetype setup for *." .. ft, { clear = true }),
pattern = { "*." .. ft },
desc = "Kulala: setup keymaps for http filtypes",
callback = function(ev)
vim.iter(maps):each(function(map)
set_keymap(map, ev.buf)
end)
end,
})
end

M.setup_kulala_keymaps = function(buf)
local config = require("kulala.config")
local config_kulala_keymaps = config.options.kulala_keymaps

if not config_kulala_keymaps then
return
end

config_kulala_keymaps = type(config_kulala_keymaps) == "table"
and vim.tbl_extend("force", M.default_kulala_keymaps, config_kulala_keymaps)
or M.default_kulala_keymaps

vim.iter(config_kulala_keymaps or {}):each(function(name, map)
if map then
map.desc = map.desc or name
set_keymap(map, buf)
end
end)
end

M.setup_global_keymaps = function()
local global_keymaps, ft_keymaps = collect_global_keymaps()

vim.iter(global_keymaps or {}):each(function(map)
set_keymap(map)
end)

vim.iter(ft_keymaps or {}):each(function(ft, maps)
create_ft_autocommand(ft, maps)
end)
return global_keymaps, ft_keymaps
end

return M
16 changes: 6 additions & 10 deletions lua/kulala/ui/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local WINBAR = require("kulala.ui.winbar")
local GLOBALS = require("kulala.globals")
local CONFIG = require("kulala.config")
local KEYMAPS = require("kulala.config.keymaps")
local INLAY = require("kulala.inlay")
local PARSER = require("kulala.parser")
local CURL_PARSER = require("kulala.parser.curl")
Expand Down Expand Up @@ -29,7 +30,7 @@ local function get_current_line()
return vim.fn.line(".")
end

local close_kulala_buffer = function()
M.close_kulala_buffer = function()
local buf = get_kulala_buffer()
if buf then
vim.api.nvim_buf_delete(buf, { force = true })
Expand All @@ -40,15 +41,10 @@ end
-- This is necessary to prevent the buffer from being left behind
-- when the window is closed
local function set_maps_autocommands(buf)
local config = CONFIG.get()
local augroup = vim.api.nvim_create_augroup("kulala_window_closed", { clear = true })

if config.display_mode == "float" and config.q_to_close_float then
vim.keymap.set("n", "q", close_kulala_buffer, { buffer = buf, noremap = true, silent = true })
end
KEYMAPS.setup_kulala_keymaps(buf)

vim.api.nvim_create_autocmd("WinClosed", {
group = augroup,
group = vim.api.nvim_create_augroup("kulala_window_closed", { clear = true }),
buffer = buf,
callback = function()
if vim.fn.bufexists(buf) > 0 then
Expand All @@ -73,7 +69,7 @@ local open_kulala_buffer = function(filetype)
---This makes sure to replace current kulala buffer with a new one
---This is necessary to prevent bugs like this:
---https://github.com/mistweaverco/kulala.nvim/issues/128
close_kulala_buffer()
M.close_kulala_buffer()
vim.api.nvim_buf_set_name(buf, GLOBALS.UI_ID)

return buf
Expand Down Expand Up @@ -304,7 +300,7 @@ M.replay = function()
end

M.close = function()
close_kulala_buffer()
M.close_kulala_buffer()

local ext = vim.fn.expand("%:e")
if ext == "http" or ext == "rest" then
Expand Down
Loading