FTplugin: regex -> TS markdown headings grepper
This commit is contained in:
parent
879f382cfa
commit
85416a3d53
1 changed files with 26 additions and 10 deletions
|
|
@ -57,17 +57,33 @@ local function fzf_lua_markdown_headings()
|
||||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||||
local headings = {}
|
local headings = {}
|
||||||
|
|
||||||
for lnum, line in ipairs(lines) do
|
-- Check if treesitter parser is available
|
||||||
local heading = line:match('^(#+%s+.+)$')
|
local ok, parser = pcall(vim.treesitter.get_parser, bufnr, 'markdown')
|
||||||
if heading then
|
if not ok or not parser then
|
||||||
vim.notify("Found heading: " .. heading, vim.log.levels.INFO)
|
vim.notify("Treesitter markdown parser not available", vim.log.levels.ERROR)
|
||||||
table.insert(headings, {
|
return
|
||||||
lnum = lnum,
|
|
||||||
text = heading,
|
|
||||||
display = string.format("%4d: %s", lnum, heading)
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
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)
|
picker.fzf_exec(function(fzf_cb)
|
||||||
for _, heading in ipairs(headings) do
|
for _, heading in ipairs(headings) do
|
||||||
fzf_cb(heading.display)
|
fzf_cb(heading.display)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue