|
| 1 | +local function parse_tags(bufnr) |
| 2 | + local tags = {} |
| 3 | + local lines = vim.api.nvim_buf_get_lines(bufnr or 0, 0, -1, false) |
| 4 | + local filepath = vim.api.nvim_buf_get_name(bufnr or 0) |
| 5 | + |
| 6 | + for lnum, line in ipairs(lines) do |
| 7 | + -- Parse tags: #tag or #tag-with-dashes |
| 8 | + for tag in line:gmatch("#([%w%-_]+)") do |
| 9 | + table.insert(tags, { |
| 10 | + tag = "#" .. tag, |
| 11 | + line = lnum, |
| 12 | + col = line:find("#" .. vim.pesc(tag)), |
| 13 | + text = line:match("^%s*(.-)%s*$"), -- trim whitespace |
| 14 | + file = filepath, |
| 15 | + }) |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + return tags |
| 20 | +end |
| 21 | + |
| 22 | +local function parse_workspace_tags() |
| 23 | + local mod = require("down.mod") |
| 24 | + local workspace_mod = mod.get_mod("workspace") |
| 25 | + |
| 26 | + if not workspace_mod then |
| 27 | + return {} |
| 28 | + end |
| 29 | + |
| 30 | + local ws_path = workspace_mod.get_current_workspace_path() |
| 31 | + if not ws_path then |
| 32 | + return {} |
| 33 | + end |
| 34 | + |
| 35 | + -- Find all markdown files in workspace |
| 36 | + local files = vim.fn.globpath(ws_path, "**/*.md", false, true) |
| 37 | + local all_tags = {} |
| 38 | + |
| 39 | + for _, file in ipairs(files) do |
| 40 | + local bufnr = vim.fn.bufadd(file) |
| 41 | + vim.fn.bufload(bufnr) |
| 42 | + local tags = parse_tags(bufnr) |
| 43 | + |
| 44 | + for _, tag_info in ipairs(tags) do |
| 45 | + table.insert(all_tags, tag_info) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + return all_tags |
| 50 | +end |
| 51 | + |
| 52 | +local function tag_picker(opts) |
| 53 | + local snacks = require("snacks") |
| 54 | + opts = opts or {} |
| 55 | + opts.scope = opts.scope or "buffer" -- buffer, workspace, or all |
| 56 | + |
| 57 | + local tags = {} |
| 58 | + |
| 59 | + if opts.scope == "buffer" then |
| 60 | + tags = parse_tags(0) |
| 61 | + elseif opts.scope == "workspace" then |
| 62 | + tags = parse_workspace_tags() |
| 63 | + end |
| 64 | + |
| 65 | + if #tags == 0 then |
| 66 | + vim.notify("No tags found", vim.log.levels.INFO) |
| 67 | + return |
| 68 | + end |
| 69 | + |
| 70 | + local items = {} |
| 71 | + for _, tag in ipairs(tags) do |
| 72 | + local filename = vim.fn.fnamemodify(tag.file, ":t") |
| 73 | + table.insert(items, { |
| 74 | + text = string.format("%s %s:%s - %s", tag.tag, tag.line, tag.col, filename), |
| 75 | + line = tag.line, |
| 76 | + col = tag.col, |
| 77 | + file = tag.file, |
| 78 | + tag = tag.tag, |
| 79 | + }) |
| 80 | + end |
| 81 | + |
| 82 | + snacks.picker({ |
| 83 | + source = items, |
| 84 | + prompt = "Tags (" .. opts.scope .. ")", |
| 85 | + format = function(item) |
| 86 | + return item.text |
| 87 | + end, |
| 88 | + confirm = function(item) |
| 89 | + if item then |
| 90 | + vim.cmd("edit " .. item.file) |
| 91 | + vim.api.nvim_win_set_cursor(0, { item.line, item.col - 1 }) |
| 92 | + vim.cmd("normal! zz") |
| 93 | + end |
| 94 | + end, |
| 95 | + }) |
| 96 | +end |
| 97 | + |
| 98 | +return tag_picker |
0 commit comments