summaryrefslogtreecommitdiff
path: root/mut/neovim/pack/plugins/start/quicker.nvim/lua/quicker/util.lua
blob: 3794091dc2f4f3b117ff6231852bc35384189614 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local M = {}

---@param bufnr integer
---@return nil|integer
function M.buf_find_win(bufnr)
  for _, winid in ipairs(vim.api.nvim_list_wins()) do
    if vim.api.nvim_win_is_valid(winid) and vim.api.nvim_win_get_buf(winid) == bufnr then
      return winid
    end
  end
end

---@param loclist_win? integer Check if loclist is open for the given window. If nil, check quickfix.
M.is_open = function(loclist_win)
  if loclist_win then
    return vim.fn.getloclist(loclist_win or 0, { winid = 0 }).winid ~= 0
  else
    return vim.fn.getqflist({ winid = 0 }).winid ~= 0
  end
end

---@param winid nil|integer
---@return nil|"c"|"l"
M.get_win_type = function(winid)
  if not winid or winid == 0 then
    winid = vim.api.nvim_get_current_win()
  end
  local info = vim.fn.getwininfo(winid)[1]
  if info.quickfix == 0 then
    return nil
  elseif info.loclist == 0 then
    return "c"
  else
    return "l"
  end
end

---@param item QuickFixItem
---@return QuickFixUserData
M.get_user_data = function(item)
  if type(item.user_data) == "table" then
    return item.user_data
  else
    return {}
  end
end

---Get valid location extmarks for a line in the quickfix
---@param bufnr integer
---@param lnum integer
---@param line_len? integer how long this particular line is
---@param ns? integer namespace of extmarks
---@return table[] extmarks
M.get_lnum_extmarks = function(bufnr, lnum, line_len, ns)
  if not ns then
    ns = vim.api.nvim_create_namespace("quicker_locations")
  end
  if not line_len then
    local line = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, true)[1]
    line_len = line:len()
  end
  local extmarks = vim.api.nvim_buf_get_extmarks(
    bufnr,
    ns,
    { lnum - 1, 0 },
    { lnum - 1, line_len },
    { details = true }
  )
  return vim.tbl_filter(function(mark)
    return not mark[4].invalid
  end, extmarks)
end

---Return true if the window is a full-height leaf window
---@param winid? integer
---@return boolean
M.is_full_height_vsplit = function(winid)
  if not winid or winid == 0 then
    winid = vim.api.nvim_get_current_win()
  end
  local layout = vim.fn.winlayout()
  -- If the top layout is not vsplit, then it's not a vertical leaf
  if layout[1] ~= "row" then
    return false
  end
  for _, v in ipairs(layout[2]) do
    if v[1] == "leaf" and v[2] == winid then
      return true
    end
  end

  return false
end

return M