summaryrefslogtreecommitdiff
path: root/lua/99/ops/search.lua
blob: 0619f47d56e9e0f32aae77f6db21397a7d05a50b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
local make_prompt = require("99.ops.make-prompt")
local CleanUp = require("99.ops.clean-up")
local QFixHelpers = require("99.ops.qfix-helpers")

local make_clean_up = CleanUp.make_clean_up
local make_observer = CleanUp.make_observer

--- @param context _99.Prompt
--- @param response string
local function create_search_locations(context, response)
  local qf_list = QFixHelpers.create_qfix_entries(response)
  context.logger:set_area("search"):debug("qf_list created", "qf_list", qf_list)
  context.data = {
    type = "search",
    qfix_items = qf_list,
    response = response,
  }

  if #qf_list > 0 then
    require("99").qfix(context.xid)
  else
    vim.notify("No search results found", vim.log.levels.INFO)
  end
end

--- @param context _99.Prompt
---@param opts _99.ops.SearchOpts
local function search(context, opts)
  opts = opts or {}

  local logger = context.logger:set_area("search")
  logger:debug("search", "with opts", opts.additional_prompt)

  local clean_up = make_clean_up(function()
    context:stop()
  end)

  local prompt, refs =
    make_prompt(context, context._99.prompts.prompts.semantic_search(), opts)

  context:add_prompt_content(prompt)
  context:add_references(refs)
  context:add_clean_up(clean_up)

  --- TODO: part of the context request clean up there needs to be a refactoring of
  --- make observer... it really should just be within the context observer creation.
  --- same with cleanup.. that should just be clean_ups from context, instead of a
  --- once cleanup function wrapper.
  ---
  --- i think an interface, CleanUpI could be something that is worth it :)
  context:start_request(make_observer(clean_up, function(status, response)
    if status == "cancelled" then
      logger:debug("request cancelled for search")
    elseif status == "failed" then
      logger:error(
        "request failed for search",
        "error response",
        response or "no response provided"
      )
    elseif status == "success" then
      create_search_locations(context, response)
    end
  end))
end
return search