Skip to content

Commit 89637a6

Browse files
bingcokeNTBBloodbath
authored andcommitted
feat: add telescope extension (#278)
1 parent f3c57a0 commit 89637a6

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ use {
9999
-- Jump to request line on run
100100
jump_to_request = false,
101101
env_file = '.env',
102+
-- for telescope select
103+
env_pattern = "\\.env$",
104+
env_edit_command = "tabedit",
102105
custom_dynamic_variables = {},
103106
yank_dry_run = true,
104107
})
@@ -186,6 +189,26 @@ request method (e.g. `GET`) and run `rest.nvim`.
186189
Run `export DEBUG_PLENARY="debug"` before starting nvim. Logs will appear most
187190
likely in ~/.cache/nvim/rest.nvim.log
188191

192+
## Telescope
193+
194+
```lua
195+
196+
-- first load extension
197+
require("telescope").load_extension("rest")
198+
-- then use telescope
199+
require("telescope").extensions.rest.select_env()
200+
201+
```
202+
203+
### Mappings
204+
205+
- Enter: Select Env file
206+
- Ctrl+O: Edit Env file
207+
208+
### Config
209+
210+
- env_pattern: For env file pattern
211+
- env_edit_command: For env file edit command
189212

190213
## Contribute
191214

lua/rest-nvim/config/check.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ function check.validate(cfg)
2525
local ok, err = validate({
2626
client = { cfg.client, "string" },
2727
env_file = { cfg.env_file, "string" },
28+
env_pattern = { cfg.env_pattern, "string" },
29+
env_edit_command = { cfg.env_edit_command, "string" },
2830
encode_url = { cfg.encode_url, "boolean" },
2931
skip_ssl_verification = { cfg.skip_ssl_verification, "boolean" },
3032
custom_dynamic_variables = { cfg.custom_dynamic_variables, "table" },

lua/rest-nvim/config/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ local logger = require("rest-nvim.logger")
5252
---@class RestConfig
5353
---@field client string The HTTP client to be used when running requests, default is `"curl"`
5454
---@field env_file string Environment variables file to be used for the request variables in the document
55+
---@field env_pattern string Environment variables file pattern for telescope.nvim
56+
---@field env_edit_command string Neovim command to edit an environment file, default is `"tabedit"`
5557
---@field encode_url boolean Encode URL before making request
5658
---@field skip_ssl_verification boolean Skip SSL verification, useful for unknown certificates
5759
---@field custom_dynamic_variables { [string]: fun(): string }[] Table of custom dynamic variables
@@ -67,6 +69,8 @@ local logger = require("rest-nvim.logger")
6769
local default_config = {
6870
client = "curl",
6971
env_file = ".env",
72+
env_pattern = "\\.env$",
73+
env_edit_command = "tabedit",
7074
encode_url = true,
7175
skip_ssl_verification = false,
7276
custom_dynamic_variables = {},

lua/telescope/_extensions/rest.lua

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
local has_telescope, telescope = pcall(require, "telescope")
2+
3+
if not has_telescope then
4+
return
5+
end
6+
7+
local rest = require("rest-nvim")
8+
9+
local state = require("telescope.actions.state")
10+
11+
local action_state = require("telescope.actions.state")
12+
local actions = require("telescope.actions")
13+
local finders = require("telescope.finders")
14+
local pickers = require("telescope.pickers")
15+
local conf = require("telescope.config").values
16+
17+
local config = require("rest-nvim.config")
18+
19+
local function rest_env_select(opt)
20+
local pattern = config.get("env_pattern")
21+
local edit = config.get("env_edit_command")
22+
23+
local command = string.format("fd -H '%s'", pattern)
24+
local result = io.popen(command):read("*a")
25+
26+
local lines = {}
27+
for line in result:gmatch("[^\r\n]+") do
28+
table.insert(lines, line)
29+
end
30+
31+
pickers
32+
.new({}, {
33+
prompt_title = "Select Evn",
34+
finder = finders.new_table({
35+
results = lines,
36+
}),
37+
attach_mappings = function(prompt_bufnr, map)
38+
actions.select_default:replace(function()
39+
local selection = action_state.get_selected_entry()
40+
if selection == nil then
41+
return
42+
end
43+
actions.close(prompt_bufnr)
44+
rest.select_env(selection[1])
45+
end)
46+
map("i", "<c-o>", function()
47+
actions.close(prompt_bufnr)
48+
local selection = state.get_selected_entry(prompt_bufnr)
49+
if selection == nil then
50+
return
51+
end
52+
vim.api.nvim_command(edit .. " " .. selection[1])
53+
end)
54+
return true
55+
end,
56+
previewer = conf.grep_previewer({}),
57+
})
58+
:find()
59+
end
60+
61+
return telescope.register_extension({
62+
exports = {
63+
select_env = rest_env_select,
64+
},
65+
})

0 commit comments

Comments
 (0)