diff --git a/docs/docs/usage/public-methods.md b/docs/docs/usage/public-methods.md index 59a85e8..fee4993 100644 --- a/docs/docs/usage/public-methods.md +++ b/docs/docs/usage/public-methods.md @@ -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`. diff --git a/lua/kulala/parser/init.lua b/lua/kulala/parser/init.lua index 69bc957..5f16217 100644 --- a/lua/kulala/parser/init.lua +++ b/lua/kulala/parser/init.lua @@ -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 diff --git a/lua/kulala/parser/utils.lua b/lua/kulala/parser/utils.lua index 5709738..de72e40 100644 --- a/lua/kulala/parser/utils.lua +++ b/lua/kulala/parser/utils.lua @@ -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 diff --git a/lua/kulala/ui/selector.lua b/lua/kulala/ui/selector.lua index dec952f..5767948 100644 --- a/lua/kulala/ui/selector.lua +++ b/lua/kulala/ui/selector.lua @@ -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 = {} @@ -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 diff --git a/lua/telescope/_extensions/kulala.lua b/lua/telescope/_extensions/kulala.lua index 540a3ab..5439113 100644 --- a/lua/telescope/_extensions/kulala.lua +++ b/lua/telescope/_extensions/kulala.lua @@ -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") @@ -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() @@ -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({}), })