Skip to content

Commit

Permalink
feat(search): searching for named requests now (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
gorillamoe authored Oct 9, 2024
1 parent a543d6e commit bfc6c10
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 14 deletions.
15 changes: 13 additions & 2 deletions docs/docs/usage/public-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,19 @@ the `body` and `headers` view of the last run request.

### search

`require('kulala').search()` searches for all `.http` and `.rest` files
in the current working directory.
`require('kulala').search()` searches for all *named* requests in the current buffer.

:::tip

Named requests are those that have a name like so:

```http
# @name MY_REQUEST_NAME
GET http://example.com
```

:::


It tries to load up a telescope prompt to select a
file or fallback to using `vim.ui.select`.
Expand Down
3 changes: 2 additions & 1 deletion lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,8 @@ end
---@field headers_raw table -- The headers as they appear in the document
---@field body_raw string|nil -- The raw body as it appears in the document
---@field body_computed string|nil -- The computed body as sent by curl; with variables and dynamic variables replaced
---@field body_display string|nil -- The body with variables and dynamic variables replaced and sanitized (e.g. with binary files replaced with a placeholder)
---@field body_display string|nil -- The body with variables and dynamic variables replaced and sanitized
---(e.g. with binary files replaced with a placeholder)
---@field body string|nil -- The body with variables and dynamic variables replaced
---@field environment table -- The environment- and document-variables
---@field cmd table -- The command to execute the request
Expand Down
14 changes: 14 additions & 0 deletions lua/kulala/parser/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ local M = {}
-- that would make the code more readable and easier to maintain
-- but it would also make it slower

---Get the value of a meta tag from the request
---@param request table The request to check
---@param tag string The meta tag to check for
---@return string|nil
M.get_meta_tag = function(request, tag)
tag = tag:lower()
for _, meta in ipairs(request.metadata) do
if meta.name:lower() == tag then
return meta.value
end
end
return nil
end

---Check if a request has a specific meta tag
---@param request table The request to check
---@param tag string The meta tag to check for
Expand Down
25 changes: 21 additions & 4 deletions lua/kulala/ui/selector.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local DB = require("kulala.db")
local FS = require("kulala.utils.fs")
local Parser = require("kulala.parser")
local ParserUtils = require("kulala.parser.utils")

local M = {}

Expand Down Expand Up @@ -28,15 +30,30 @@ function M.select_env()
end

M.search = function()
local files = FS.find_all_http_files()
if #files == 0 then
local _, requests = Parser.get_document()

if requests == nil then
return
end

local line_starts = {}
local names = {}

for _, request in ipairs(requests) do
local request_name = ParserUtils.get_meta_tag(request, "name")
if request_name ~= nil then
table.insert(names, request_name)
line_starts[request_name] = request.start_line
end
end
if #names == 0 then
return
end
vim.ui.select(files, { prompt = "Search" }, function(result)
vim.ui.select(names, { prompt = "Search" }, function(result)
if not result then
return
end
vim.cmd("e " .. result)
vim.cmd("normal! " .. line_starts[result] + 1 .. "G")
end)
end

Expand Down
50 changes: 43 additions & 7 deletions lua/telescope/_extensions/kulala.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ if not has_telescope then
end

local DB = require("kulala.db")
local FS = require("kulala.utils.fs")
local Parser = require("kulala.parser")
local ParserUtils = require("kulala.parser.utils")

local action_state = require("telescope.actions.state")
local actions = require("telescope.actions")
Expand All @@ -15,15 +16,28 @@ local previewers = require("telescope.previewers")
local config = require("telescope.config").values

local function kulala_search(_)
-- a list of all the .http/.rest files in the current directory
-- and its subdirectories
local files = FS.find_all_http_files()
local _, requests = Parser.get_document()

if requests == nil then
return
end

local data = {}
local names = {}

for _, request in ipairs(requests) do
local request_name = ParserUtils.get_meta_tag(request, "name")
if request_name ~= nil then
table.insert(names, request_name)
data[request_name] = request
end
end

pickers
.new({}, {
prompt_title = "Search",
finder = finders.new_table({
results = files,
results = names,
}),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
Expand All @@ -32,12 +46,34 @@ local function kulala_search(_)
if selection == nil then
return
end
vim.cmd("e " .. selection.value)
local request = data[selection.value]
vim.cmd("normal! " .. request.start_line + 1 .. "G")
end)
return true
end,
previewer = previewers.vim_buffer_cat.new({
previewer = previewers.new_buffer_previewer({
title = "Preview",
define_preview = function(self, entry)
local request = data[entry.value]
if request == nil then
return
end
local lines = {}
local http_version = request.http_version and "HTTP/" .. request.http_version or "HTTP/1.1"
table.insert(lines, request.method .. " " .. request.url .. " " .. http_version)
for key, value in pairs(request.headers) do
table.insert(lines, key .. ": " .. value)
end
if request.body_display ~= nil then
table.insert(lines, "")
local body_as_table = vim.split(request.body_display, "\r?\n")
for _, line in ipairs(body_as_table) do
table.insert(lines, line)
end
end
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
vim.bo[self.state.bufnr].filetype = "http"
end,
}),
sorter = config.generic_sorter({}),
})
Expand Down

0 comments on commit bfc6c10

Please sign in to comment.