Skip to content

Commit 07fb32f

Browse files
authored
Merge pull request #5 from diegoortizmatajira/main
Add create_keybindings option
2 parents 751b54c + 4faa89b commit 07fb32f

File tree

3 files changed

+130
-132
lines changed

3 files changed

+130
-132
lines changed

lua/http_client/config.lua

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
local M = {}
22

33
M.defaults = {
4-
default_env_file = '.env.json',
5-
request_timeout = 30000, -- 30 seconds
6-
split_direction = "right",
7-
keybindings = {
8-
select_env_file = "<leader>he",
9-
set_env = "<leader>hs",
10-
run_request = "<leader>hr",
11-
stop_request = "<leader>hx",
12-
dry_run = "<leader>hd",
13-
toggle_verbose = "<leader>hv",
14-
copy_curl = "<leader>hc",
15-
},
4+
default_env_file = ".env.json",
5+
request_timeout = 30000, -- 30 seconds
6+
split_direction = "right",
7+
create_keybindings = true,
8+
keybindings = {
9+
select_env_file = "<leader>he",
10+
set_env = "<leader>hs",
11+
run_request = "<leader>hr",
12+
stop_request = "<leader>hx",
13+
dry_run = "<leader>hd",
14+
toggle_verbose = "<leader>hv",
15+
copy_curl = "<leader>hc",
16+
},
1617
}
1718

1819
M.options = {}
1920

2021
function M.setup(opts)
21-
M.options = vim.tbl_deep_extend("force", {}, M.defaults, opts or {})
22+
opts = opts or {}
23+
M.options = vim.tbl_deep_extend("force", M.defaults, opts) or M.defaults
2224
end
2325

2426
function M.get(opt)
25-
return M.options[opt]
27+
return M.options[opt]
2628
end
2729

2830
return M
29-

lua/http_client/init.lua

Lines changed: 110 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,123 @@
11
local M = {}
22

3-
M.config = require('http_client.config')
4-
3+
M.config = require("http_client.config")
54

65
local function setup_docs()
7-
if vim.fn.has("nvim-0.7") == 1 then
8-
vim.api.nvim_create_autocmd("BufWinEnter", {
9-
group = vim.api.nvim_create_augroup("http_client_docs", {}),
10-
pattern = "*/http_client/doc/*.txt",
11-
callback = function()
12-
vim.cmd("silent! helptags " .. vim.fn.expand("%:p:h"))
13-
end,
14-
})
15-
end
6+
if vim.fn.has("nvim-0.7") == 1 then
7+
vim.api.nvim_create_autocmd("BufWinEnter", {
8+
group = vim.api.nvim_create_augroup("http_client_docs", {}),
9+
pattern = "*/http_client/doc/*.txt",
10+
callback = function()
11+
vim.cmd("silent! helptags " .. vim.fn.expand("%:p:h"))
12+
end,
13+
})
14+
end
1615
end
1716

1817
local function set_keybindings()
19-
local opts = { noremap = true, silent = true }
20-
21-
vim.api.nvim_create_autocmd("FileType", {
22-
pattern = "http",
23-
callback = function()
24-
vim.keymap.set('n', M.config.get('keybindings').select_env_file, ':HttpEnvFile<CR>', opts)
25-
vim.keymap.set('n', M.config.get('keybindings').set_env, ':HttpEnv<CR>', { noremap = true, buffer = true })
26-
vim.keymap.set('n', M.config.get('keybindings').run_request, ':HttpRun<CR>', opts)
27-
vim.keymap.set('n', M.config.get('keybindings').stop_request, ':HttpStop<CR>', opts)
28-
vim.keymap.set('n', M.config.get('keybindings').dry_run, ':HttpDryRun<CR>', opts)
29-
vim.keymap.set('n', M.config.get('keybindings').toggle_verbose, ':HttpVerbose<CR>', opts)
30-
vim.keymap.set('n', M.config.get('keybindings').copy_curl, ':HttpCopyCurl<CR>', opts)
31-
end
32-
})
18+
if M.config.get("create_keybindings") then
19+
local opts = { noremap = true, silent = true }
20+
vim.api.nvim_create_autocmd("FileType", {
21+
pattern = "http",
22+
callback = function()
23+
vim.keymap.set("n", M.config.get("keybindings").select_env_file, ":HttpEnvFile<CR>", opts)
24+
vim.keymap.set(
25+
"n",
26+
M.config.get("keybindings").set_env,
27+
":HttpEnv<CR>",
28+
{ noremap = true, buffer = true }
29+
)
30+
vim.keymap.set("n", M.config.get("keybindings").run_request, ":HttpRun<CR>", opts)
31+
vim.keymap.set("n", M.config.get("keybindings").stop_request, ":HttpStop<CR>", opts)
32+
vim.keymap.set("n", M.config.get("keybindings").dry_run, ":HttpDryRun<CR>", opts)
33+
vim.keymap.set("n", M.config.get("keybindings").toggle_verbose, ":HttpVerbose<CR>", opts)
34+
vim.keymap.set("n", M.config.get("keybindings").copy_curl, ":HttpCopyCurl<CR>", opts)
35+
end,
36+
})
37+
end
3338
end
3439

3540
M.setup = function(opts)
36-
M.config.setup(opts)
37-
38-
-- Load all necessary modules
39-
M.environment = require('http_client.core.environment')
40-
M.file_utils = require('http_client.utils.file_utils')
41-
M.http_client = require('http_client.core.http_client')
42-
M.parser = require('http_client.core.parser')
43-
M.ui = require('http_client.ui.display')
44-
M.dry_run = require('http_client.ui.dry_run')
45-
M.v = require('http_client.utils.verbose')
46-
M.commands = require('http_client.commands')
47-
48-
49-
-- Set up commands
50-
vim.api.nvim_create_user_command('HttpEnvFile', function()
51-
M.commands.select_env.select_env_file(M.config.get())
52-
end, {
53-
desc = 'Select an environment file for HTTP requests.'
54-
})
55-
56-
vim.api.nvim_create_user_command('HttpEnv', function()
57-
M.commands.select_env.select_env()
58-
end, {
59-
desc = 'Select an environment for HTTP request (requires one argument).',
60-
})
61-
62-
vim.api.nvim_create_user_command('HttpRun', function()
63-
M.commands.request.run_request()
64-
end, {
65-
desc = 'Run the HTTP request under cursor. Use ! to enable verbose mode.',
66-
})
67-
68-
vim.api.nvim_create_user_command('HttpRunAll', function()
69-
M.commands.request.run_all()
70-
end, {
71-
desc = 'Run all HTTP requests in the current file.'
72-
})
73-
74-
vim.api.nvim_create_user_command('HttpStop', function()
75-
M.commands.request.stop_request()
76-
end, {
77-
desc = 'Stop the currently running HTTP request.'
78-
})
79-
80-
81-
82-
vim.api.nvim_create_user_command('HttpVerbose', function()
83-
local current_state = M.v.get_verbose_mode()
84-
M.v.set_verbose_mode(not current_state)
85-
print(string.format("HTTP Client verbose mode %s", not current_state and "enabled" or "disabled"))
86-
end, {
87-
desc = 'Toggle verbose mode for HTTP request.'
88-
})
89-
90-
vim.api.nvim_create_user_command('HttpDryRun', function()
91-
M.dry_run.display_dry_run(M)
92-
end, {
93-
desc = 'Perform a dry run of the HTTP request without sending it.'
94-
})
95-
96-
vim.api.nvim_create_user_command('HttpCopyCurl', function()
97-
M.commands.utils.copy_curl()
98-
end, {
99-
desc = 'Copy curl command for the HTTP request under cursor.'
100-
})
101-
102-
103-
104-
setup_docs()
105-
set_keybindings()
106-
107-
108-
M.health = require('http_client.health')
109-
-- Register health check
110-
local health = vim.health or M.health
111-
if health.register then
112-
-- Register the health check with the new API
113-
health.register("http_client", M.health.check)
114-
else
115-
-- Fallback for older Neovim versions
116-
vim.api.nvim_create_autocmd("VimEnter", {
117-
callback = function()
118-
M.health.check()
119-
end,
120-
})
121-
end
41+
M.config.setup(opts)
42+
43+
-- Load all necessary modules
44+
M.environment = require("http_client.core.environment")
45+
M.file_utils = require("http_client.utils.file_utils")
46+
M.http_client = require("http_client.core.http_client")
47+
M.parser = require("http_client.core.parser")
48+
M.ui = require("http_client.ui.display")
49+
M.dry_run = require("http_client.ui.dry_run")
50+
M.v = require("http_client.utils.verbose")
51+
M.commands = require("http_client.commands")
52+
53+
-- Set up commands
54+
vim.api.nvim_create_user_command("HttpEnvFile", function()
55+
M.commands.select_env.select_env_file(M.config.get())
56+
end, {
57+
desc = "Select an environment file for HTTP requests.",
58+
})
59+
60+
vim.api.nvim_create_user_command("HttpEnv", function()
61+
M.commands.select_env.select_env()
62+
end, {
63+
desc = "Select an environment for HTTP request (requires one argument).",
64+
})
65+
66+
vim.api.nvim_create_user_command("HttpRun", function()
67+
M.commands.request.run_request()
68+
end, {
69+
desc = "Run the HTTP request under cursor. Use ! to enable verbose mode.",
70+
})
71+
72+
vim.api.nvim_create_user_command("HttpRunAll", function()
73+
M.commands.request.run_all()
74+
end, {
75+
desc = "Run all HTTP requests in the current file.",
76+
})
77+
78+
vim.api.nvim_create_user_command("HttpStop", function()
79+
M.commands.request.stop_request()
80+
end, {
81+
desc = "Stop the currently running HTTP request.",
82+
})
83+
84+
vim.api.nvim_create_user_command("HttpVerbose", function()
85+
local current_state = M.v.get_verbose_mode()
86+
M.v.set_verbose_mode(not current_state)
87+
print(string.format("HTTP Client verbose mode %s", not current_state and "enabled" or "disabled"))
88+
end, {
89+
desc = "Toggle verbose mode for HTTP request.",
90+
})
91+
92+
vim.api.nvim_create_user_command("HttpDryRun", function()
93+
M.dry_run.display_dry_run(M)
94+
end, {
95+
desc = "Perform a dry run of the HTTP request without sending it.",
96+
})
97+
98+
vim.api.nvim_create_user_command("HttpCopyCurl", function()
99+
M.commands.utils.copy_curl()
100+
end, {
101+
desc = "Copy curl command for the HTTP request under cursor.",
102+
})
103+
104+
setup_docs()
105+
set_keybindings()
106+
107+
M.health = require("http_client.health")
108+
-- Register health check
109+
local health = vim.health or M.health
110+
if health.register then
111+
-- Register the health check with the new API
112+
health.register("http_client", M.health.check)
113+
else
114+
-- Fallback for older Neovim versions
115+
vim.api.nvim_create_autocmd("VimEnter", {
116+
callback = function()
117+
M.health.check()
118+
end,
119+
})
120+
end
122121
end
123122

124123
return M
125-

plugin/http_client.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
if vim.g.loaded_http_client then
2-
return
2+
return
33
end
44
vim.g.loaded_http_client = true
55

6-
local http_client = require("http_client")
7-
8-
http_client.setup()
9-
6+
-- WARN: Commenting out because it ommits user configuration provided in the setup function when manually running setup({opts})
7+
-- local http_client = require("http_client")
8+
-- http_client.setup()

0 commit comments

Comments
 (0)