FTplugin: regex -> TS markdown headings grepper

This commit is contained in:
t0xa 2026-02-06 16:13:24 +03:00
parent 879f382cfa
commit 85416a3d53

View file

@ -57,17 +57,33 @@ local function fzf_lua_markdown_headings()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local headings = {}
for lnum, line in ipairs(lines) do
local heading = line:match('^(#+%s+.+)$')
if heading then
vim.notify("Found heading: " .. heading, vim.log.levels.INFO)
table.insert(headings, {
lnum = lnum,
text = heading,
display = string.format("%4d: %s", lnum, heading)
})
end
-- Check if treesitter parser is available
local ok, parser = pcall(vim.treesitter.get_parser, bufnr, 'markdown')
if not ok or not parser then
vim.notify("Treesitter markdown parser not available", vim.log.levels.ERROR)
return
end
local query = vim.treesitter.query.parse('markdown', [[
(atx_heading
(atx_h1_marker)) @heading
]])
local tree = parser:parse()[1]
local root = tree:root()
for id, node in query:iter_captures(root, bufnr, 0, -1) do
local start_row, start_col, end_row, end_col = node:range()
local lnum = start_row + 1 -- Treesitter is 0-indexed, Vim is 1-indexed
local text = vim.treesitter.get_node_text(node, bufnr)
table.insert(headings, {
lnum = lnum,
text = text,
display = string.format("%4d: %s", lnum, text)
})
end
picker.fzf_exec(function(fzf_cb)
for _, heading in ipairs(headings) do
fzf_cb(heading.display)