Skip to content

Commit

Permalink
feat: add telescope extension (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcoke authored and NTBBloodbath committed Feb 12, 2024
1 parent 2d7bd3d commit de3c0fd
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ use {
-- Jump to request line on run
jump_to_request = false,
env_file = '.env',
-- for telescope select
env_pattern = "\\.env$",
env_edit_command = "tabedit",
custom_dynamic_variables = {},
yank_dry_run = true,
search_back = true,
Expand Down Expand Up @@ -189,6 +192,26 @@ request method (e.g. `GET`) and run `rest.nvim`.
Run `export DEBUG_PLENARY="debug"` before starting nvim. Logs will appear most
likely in ~/.cache/nvim/rest.nvim.log

## Telescope

```lua

-- first load extension
require("telescope").load_extension("rest")
-- then use telescope
require("telescope").extensions.rest.select_env()

```

### Mappings

- Enter: Select Env file
- Ctrl+O: Edit Env file

### Config

- env_pattern: For env file pattern
- env_edit_command: For env file edit command

## Contribute

Expand Down
2 changes: 2 additions & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ local config = {
},
jump_to_request = false,
env_file = ".env",
env_pattern = "\\.env$",
env_edit_command = "tabedit",
custom_dynamic_variables = {},
yank_dry_run = true,
search_back = true,
Expand Down
65 changes: 65 additions & 0 deletions lua/telescope/_extensions/rest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
local has_telescope, telescope = pcall(require, "telescope")

if not has_telescope then
return
end

local rest = require("rest-nvim")

local state = require("telescope.actions.state")

local action_state = require("telescope.actions.state")
local actions = require("telescope.actions")
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local conf = require("telescope.config").values

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

local function rest_env_select(opt)
local pattern = config.get("env_pattern")
local edit = config.get("env_edit_command")

local command = string.format("fd -H '%s'", pattern)
local result = io.popen(command):read("*a")

local lines = {}
for line in result:gmatch("[^\r\n]+") do
table.insert(lines, line)
end

pickers
.new({}, {
prompt_title = "Select Evn",
finder = finders.new_table({
results = lines,
}),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
if selection == nil then
return
end
actions.close(prompt_bufnr)
rest.select_env(selection[1])
end)
map("i", "<c-o>", function()
actions.close(prompt_bufnr)
local selection = state.get_selected_entry(prompt_bufnr)
if selection == nil then
return
end
vim.api.nvim_command(edit .. " " .. selection[1])
end)
return true
end,
previewer = conf.grep_previewer({}),
})
:find()
end

return telescope.register_extension({
exports = {
select_env = rest_env_select,
},
})

0 comments on commit de3c0fd

Please sign in to comment.