-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicker.lua
More file actions
125 lines (116 loc) · 3.47 KB
/
picker.lua
File metadata and controls
125 lines (116 loc) · 3.47 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
local config = require("peekstack.config")
local location = require("peekstack.core.location")
local fs = require("peekstack.util.fs")
local M = {}
---@param chunks table
---@param path string
---@param dir_hl? string
---@param file_hl? string
function M.append_path_chunks(chunks, path, dir_hl, file_hl)
local file_group = file_hl or "Directory"
local dir, base = path:match("^(.*[/\\])(.+)$")
if dir and base then
if type(dir_hl) == "string" and dir_hl ~= "" then
chunks[#chunks + 1] = { dir, dir_hl }
else
chunks[#chunks + 1] = { dir, file_group }
end
chunks[#chunks + 1] = { base, file_group }
return
end
chunks[#chunks + 1] = { path, file_group }
end
---@param text? string
---@return string
local function normalize_label_text(text)
if type(text) ~= "string" then
return ""
end
local normalized = text:gsub("[\r\n\t]+", " "):gsub("%s+", " ")
return vim.trim(normalized)
end
---@param suffix string
---@return string, integer, integer
local function parse_suffix_location(suffix)
local path, line, col = suffix:match("^(.*):(%d+):(%d+)$")
if not path then
return suffix, 0, 0
end
return path, tonumber(line) or 0, tonumber(col) or 0
end
---@param loc PeekstackLocation
---@param preview_lines integer
---@param opts PeekstackDisplayTextOpts
---@return { label: string, symbol: string, path: string, display_lnum: integer, display_col: integer }
local function build_location_label_payload(loc, preview_lines, opts)
local suffix = location.display_text(loc, 0, opts)
local path, display_lnum, display_col = parse_suffix_location(suffix)
local symbol = preview_lines > 0 and normalize_label_text(loc.text) or ""
if symbol == "" then
return {
label = suffix,
symbol = "",
path = path,
display_lnum = display_lnum,
display_col = display_col,
}
end
return {
label = string.format("%s - %s", symbol, suffix),
symbol = symbol,
path = path,
display_lnum = display_lnum,
display_col = display_col,
}
end
---@return PeekstackDisplayTextOpts
local function display_text_opts()
local ui_path = config.get().ui.path or {}
local max_width = ui_path.max_width or 0
if max_width == 0 then
max_width = math.floor(vim.o.columns * 0.7)
end
return {
path_base = ui_path.base,
max_width = max_width,
}
end
---@param locations PeekstackLocation[]
---@param preview_lines integer
---@return PeekstackPickerItem[]
function M.build_items(locations, preview_lines)
local opts = display_text_opts()
local items = {}
for _, loc in ipairs(locations) do
local payload = build_location_label_payload(loc, preview_lines, opts)
table.insert(items, {
label = payload.label,
value = loc,
})
end
return items
end
---@param locations PeekstackLocation[]
---@param preview_lines integer
---@return PeekstackPickerExternalItem[]
function M.build_external_items(locations, preview_lines)
local opts = display_text_opts()
local items = {}
for _, loc in ipairs(locations) do
local start = loc.range and loc.range.start or {}
local payload = build_location_label_payload(loc, preview_lines, opts)
table.insert(items, {
label = payload.label,
symbol = payload.symbol,
path = payload.path,
display_lnum = payload.display_lnum,
display_col = payload.display_col,
value = loc,
file = fs.uri_to_fname(loc.uri),
lnum = (start.line or 0) + 1,
col = (start.character or 0) + 1,
})
end
return items
end
return M