local keymap = vim.keymap -- Picker for markdown headers -- local pickers = require("telescope.pickers") -- local finders = require("telescope.finders") -- local conf = require("telescope.config").values -- local actions = require("telescope.actions") -- local action_state = require("telescope.actions.state") -- -- local function markdown_headings() -- local bufnr = vim.api.nvim_get_current_buf() -- 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 -- table.insert(headings, { -- lnum = lnum, -- text = heading, -- display = string.format("%4d: %s", lnum, heading) -- }) -- end -- end -- pickers.new({}, { -- prompt_title = 'Markdown Headings', -- finder = finders.new_table { -- results = headings, -- entry_maker = function(entry) -- return { -- value = entry, -- display = entry.display, -- ordinal = entry.text, -- lnum = entry.lnum, -- } -- end -- }, -- sorter = conf.generic_sorter({}), -- attach_mappings = function(prompt_bufnr, map) -- actions.select_default:replace(function() -- actions.close(prompt_bufnr) -- local selection = action_state.get_selected_entry() -- vim.api.nvim_win_set_cursor(0, { selection.value.lnum, 0 }) -- end) -- return true -- end, -- }):find() -- end -- -- keymap.set("n", "fdh", markdown_headings, { desc = "Grep: Markdown headings" }) -- FZF lua headings picker local picker = require("fzf-lua") local function fzf_lua_markdown_headings() local bufnr = vim.api.nvim_get_current_buf() local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) local headings = {} -- 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) end fzf_cb() -- EOF end, { prompt = "Markdown headings> ", actions = { ['default'] = function(selected) local line_num = tonumber(selected[1]:match("%d+")) vim.api.nvim_win_set_cursor(0, { line_num, 0 }) end } }) end keymap.set("n", "fdh", fzf_lua_markdown_headings, { desc = "Grep: Markdown headings" })