Skip to content

Commit 4cb7d7c

Browse files
committed
refactor(marks): use vim.fn.getmarklist
highlight local buf text with `Directory` to match output of `:marks` Closes #2353
1 parent 3776d03 commit 4cb7d7c

4 files changed

Lines changed: 28 additions & 22 deletions

File tree

lua/fzf-lua/defaults.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,7 @@ M.defaults.profiles = {
11791179
}
11801180

11811181
M.defaults.marks = {
1182+
sort = false,
11821183
fzf_opts = { ["--no-multi"] = true },
11831184
actions = {
11841185
["enter"] = actions.goto_mark,

lua/fzf-lua/previewer/builtin.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,6 @@ function Previewer.marks:parse_entry(entry_str)
14891489
return vim.api.nvim_buf_get_mark(buf, mark)
14901490
end)
14911491
if pos and pos[1] > 0 then
1492-
assert(pos[1] == tonumber(lnum) or self.win.src_winid == self.win.fzf_winid)
14931492
bufnr = self.win.src_bufnr
14941493
filepath = api.nvim_buf_get_name(bufnr)
14951494
end

lua/fzf-lua/providers/nvim.lua

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -251,32 +251,37 @@ M.marks = function(opts)
251251
if not opts then return end
252252

253253
local contents = function(cb)
254-
local win = utils.CTX().winid
255254
local buf = utils.CTX().bufnr
256-
win = vim.api.nvim_win_is_valid(win) and win or 0
257-
buf = vim.api.nvim_win_is_valid(buf) and buf or 0
258-
local marks = vim.api.nvim_win_call(win, function()
259-
return vim.api.nvim_buf_call(buf, function() return vim.fn.execute("marks") end)
260-
end)
261-
marks = vim.split(marks, "\n")
262255
local entries = {}
263-
local pattern = opts.marks and opts.marks or ""
264-
for i = #marks, 3, -1 do
265-
local mark, line, col, text = marks[i]:match("(.)%s+(%d+)%s+(%d+)%s+(.*)")
266-
col = tostring(tonumber(col) + 1)
267-
if path.is_absolute(text) then
268-
text = path.HOME_to_tilde(text)
269-
end
270-
if not pattern or string.match(mark, pattern) then
271-
table.insert(entries, string.format("%s %s %s %s",
272-
utils.ansi_codes[opts.hls.buf_nr](string.format("%4s", mark)),
273-
utils.ansi_codes[opts.hls.path_linenr](string.format("%4s", tostring(line))),
274-
utils.ansi_codes[opts.hls.path_colnr](string.format("%3s", tostring(col))),
275-
text))
256+
local function add_mark(mark, line, col, text)
257+
if opts.marks and string.match(mark, opts.marks) then return end
258+
table.insert(entries, string.format("%s %s %s %s",
259+
utils.ansi_codes[opts.hls.buf_nr](string.format("%4s", mark)),
260+
utils.ansi_codes[opts.hls.path_linenr](string.format("%4s", tostring(line))),
261+
utils.ansi_codes[opts.hls.path_colnr](string.format("%3s", tostring(col))),
262+
text))
263+
end
264+
265+
-- local buffer marks
266+
for _, m in ipairs(vim.fn.getmarklist(buf)) do
267+
local mark, lnum, col = m.mark:sub(2, 2), m.pos[2], m.pos[3]
268+
local text = vim.api.nvim_buf_get_lines(buf, lnum - 1, lnum, false)[1]
269+
add_mark(mark, lnum, col, utils.ansi_from_hl("Directory", text or "-invalid-"))
270+
end
271+
272+
-- global marks
273+
for _, m in ipairs(vim.fn.getmarklist()) do
274+
local mark, lnum, col, file = m.mark:sub(2, 2), m.pos[2], m.pos[3], m.file
275+
file = path.relative_to(file, uv.cwd())
276+
if path.is_absolute(file) then
277+
file = path.HOME_to_tilde(file)
276278
end
279+
add_mark(mark, lnum, col, file or "-invalid-")
277280
end
278281

279-
table.sort(entries, function(a, b) return a < b end)
282+
if opts.sort then
283+
table.sort(entries, function(a, b) return a < b end)
284+
end
280285
table.insert(entries, 1,
281286
string.format("%-5s %s %s %s", "mark", "line", "col", "file/text"))
282287

lua/fzf-lua/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ _G.FzfLua = require("fzf-lua")
618618
---@field load table
619619

620620
---@class fzf-lua.config.Marks: fzf-lua.config.Base
621+
---@field sort boolean sort mark list?
621622
---@field marks string lua pattern to filter marks
622623

623624
---@class fzf-lua.config.Changes: fzf-lua.config.Jumps

0 commit comments

Comments
 (0)