local pickers = require("telescope.pickers") local finders = require("telescope.finders") local sorters = require("telescope.sorters") local config = require("telescope.config").values local actions = require("telescope.actions") local action_state = require("telescope.actions.state") local http_client = require("telescope_meilisearch.http_client") local function search_meilisearch(query) local url = "http://127.0.0.1:7700/indexes/notes/search" local payload = '{"q": "' .. query .. '"}' local headers = { "Content-Type: application/json", "Content-Length: " .. #payload } local _, body = http_client.make_request(url, "POST", headers, payload) local results = vim.fn.json_decode(body).hits return results end local function meilisearch(opts) local query = vim.fn.input("Meilisearch> ") if query == "" then return end local results = search_meilisearch(query) if #results == 0 then print("No results found") return end pickers.new(opts, { prompt_title = "Meilisearch", finder = finders.new_table { results = results, entry_maker = function(entry) return { value = entry.title, display = entry.title .. " - " .. entry.path, ordinal = entry.title, path = entry.path, line_number = entry.line_number, column_number = entry.column_number, } end, }, sorter = config.generic_sorter(opts), attach_mappings = function(_, map) map("i", "", function(prompt_bufnr) local selection = action_state.get_selected_entry() actions.close(prompt_bufnr) if selection then vim.cmd("e " .. selection.path) vim.cmd(selection.line_number .. "G") vim.cmd("normal! " .. selection.column_number .. "|") end end) return true end, }):find() end return { meilisearch = meilisearch }