aboutsummaryrefslogtreecommitdiffstats
path: root/lua/pi/select_to_quickfix.lua
blob: ebd156139d83684ea17a181dc3f9c3c1ea2cbf34 (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
local api = vim.api

local function create_quickfix_entry(selection)
  local filename, linenumber = selection:match "([^:]+):(%d+)"

  return {
    filename = vim.fn.getcwd() .. "/" .. filename,
    lnum = tonumber(linenumber),
    col = 1,
    text = filename,
  }
end

local function add_selections_to_quickfix(selections)
  local quickfix_list = {}

  for _, selection in ipairs(selections) do
    local entry = create_quickfix_entry(selection)
    table.insert(quickfix_list, entry)
  end

  vim.fn.setqflist(quickfix_list, "r")
end

local function get_selections()
  local _, end_line, _, _ = unpack(vim.fn.getpos "'>")
  local _, start_line, _, _ = unpack(vim.fn.getpos "'<")
  local lines = api.nvim_buf_get_lines(0, start_line - 1, end_line, false)

  local selections = {}
  for _, line in ipairs(lines) do
    for selection in line:gmatch "%S+" do
      table.insert(selections, selection)
    end
  end

  return selections
end

local function select_to_quickfix()
  local selections = get_selections()

  if #selections > 0 then
    add_selections_to_quickfix(selections)
    print "Selections added to quickfix list"
  else
    print "No selections found"
  end
end

return {
  handle = select_to_quickfix,
}