Skip to content

Commit f3c290e

Browse files
committed
refactor(picker): deduplicate path chunk formatting
1 parent 6de5142 commit f3c290e

4 files changed

Lines changed: 36 additions & 26 deletions

File tree

lua/peekstack/picker/snacks.lua

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@ local picker_util = require("peekstack.util.picker")
22

33
local M = {}
44

5-
---@param chunks table
6-
---@param path string
7-
local function append_path_chunks(chunks, path)
8-
local dir, base = path:match("^(.*[/\\])(.+)$")
9-
if dir and base then
10-
chunks[#chunks + 1] = { dir, "SnacksPickerDir" }
11-
chunks[#chunks + 1] = { base, "SnacksPickerFile" }
12-
return
13-
end
14-
chunks[#chunks + 1] = { path, "SnacksPickerFile" }
15-
end
16-
175
---@param item table
186
---@return table
197
local function format_item(item)
@@ -28,7 +16,7 @@ local function format_item(item)
2816
if type(path) ~= "string" or path == "" then
2917
path = item.text or ""
3018
end
31-
append_path_chunks(chunks, path)
19+
picker_util.append_path_chunks(chunks, path, "SnacksPickerDir", "SnacksPickerFile")
3220

3321
if type(item.display_lnum) == "number" and item.display_lnum > 0 then
3422
chunks[#chunks + 1] = { ":", "SnacksPickerDelim" }

lua/peekstack/picker/telescope.lua

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ local function hl(primary, fallback)
1212
return fallback
1313
end
1414

15-
---@param chunks table
16-
---@param path string
17-
local function append_path_chunks(chunks, path)
18-
local dir, base = path:match("^(.*[/\\])(.+)$")
19-
if dir and base then
20-
chunks[#chunks + 1] = { dir, hl("TelescopeResultsComment", "Comment") }
21-
chunks[#chunks + 1] = { base, hl("TelescopeResultsIdentifier", "Directory") }
22-
return
23-
end
24-
chunks[#chunks + 1] = { path, hl("TelescopeResultsIdentifier", "Directory") }
25-
end
26-
2715
---@param displayer fun(chunks: table): string
2816
---@param item PeekstackPickerExternalItem
2917
---@return string
@@ -35,7 +23,12 @@ local function display_entry(displayer, item)
3523
end
3624

3725
local path = item.path or item.label
38-
append_path_chunks(chunks, path)
26+
picker_util.append_path_chunks(
27+
chunks,
28+
path,
29+
hl("TelescopeResultsComment", "Comment"),
30+
hl("TelescopeResultsIdentifier", "Directory")
31+
)
3932

4033
if type(item.display_lnum) == "number" and item.display_lnum > 0 then
4134
chunks[#chunks + 1] = { ":", hl("TelescopeResultsComment", "Comment") }

lua/peekstack/util/picker.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ local fs = require("peekstack.util.fs")
44

55
local M = {}
66

7+
---@param chunks table
8+
---@param path string
9+
---@param dir_hl? string
10+
---@param file_hl? string
11+
function M.append_path_chunks(chunks, path, dir_hl, file_hl)
12+
local file_group = file_hl or "Directory"
13+
local dir, base = path:match("^(.*[/\\])(.+)$")
14+
if dir and base then
15+
if type(dir_hl) == "string" and dir_hl ~= "" then
16+
chunks[#chunks + 1] = { dir, dir_hl }
17+
else
18+
chunks[#chunks + 1] = { dir, file_group }
19+
end
20+
chunks[#chunks + 1] = { base, file_group }
21+
return
22+
end
23+
chunks[#chunks + 1] = { path, file_group }
24+
end
25+
726
---@param text? string
827
---@return string
928
local function normalize_label_text(text)

tests/picker_util_spec.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,14 @@ describe("peekstack.util.picker", function()
8080
local items = picker_util.build_external_items({ location }, 1)
8181
assert.equals("errMsgFailedToStartServer - /tmp/sample.lua:10:3", items[1].label)
8282
end)
83+
84+
it("appends split path chunks with given highlight groups", function()
85+
local chunks = {}
86+
picker_util.append_path_chunks(chunks, "/tmp/sample.lua", "DirHl", "FileHl")
87+
88+
assert.same({
89+
{ "/tmp/", "DirHl" },
90+
{ "sample.lua", "FileHl" },
91+
}, chunks)
92+
end)
8393
end)

0 commit comments

Comments
 (0)