summaryrefslogtreecommitdiff
path: root/lua/99/ops/qfix-helpers.lua
blob: 9e55314488b0e9c80190e982e4951296aa4b3023 (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
local M = {}

--- @return _99.Search.Result | nil
function M.parse_line(line)
  local filepath, lnum_raw, rest = line:match("^(.-):([^:]+):(.+)$")
  if not filepath or not lnum_raw or not rest then
    return nil
  end

  local col_raw, _, notes = rest:match("^([^,]+),([^,]+),?(.*)$")
  if not col_raw then
    return nil
  end

  local lnum = tonumber(lnum_raw) or 1
  local col = tonumber(col_raw) or 1

  return {
    filename = filepath,
    lnum = lnum,
    col = col,
    text = notes or "",
  }
end

--- @param response string
--- @return _99.Search.Result[]
function M.create_qfix_entries(response)
  local lines = vim.split(response, "\n")
  local qf_list = {} --[[ @as _99.Search.Result[] ]]

  for _, line in ipairs(lines) do
    local res = M.parse_line(line)
    if res then
      table.insert(qf_list, res)
    end
  end
  return qf_list
end

return M