From 85416a3d5307eb842512673749a867b0b4fd122b Mon Sep 17 00:00:00 2001 From: t0xa Date: Fri, 6 Feb 2026 16:13:24 +0300 Subject: [PATCH] FTplugin: regex -> TS markdown headings grepper --- ftplugin/markdown.lua | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/ftplugin/markdown.lua b/ftplugin/markdown.lua index 8c2590f..d32d7ff 100644 --- a/ftplugin/markdown.lua +++ b/ftplugin/markdown.lua @@ -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)