|
1 | 1 | local log = require "obsidian.log"
|
2 | 2 | local util = require "obsidian.util"
|
| 3 | +local search = require "obsidian.search" |
3 | 4 | local LocationList = require "obsidian.location_list"
|
4 | 5 |
|
5 | 6 | ---@param client obsidian.Client
|
6 |
| -return function(client, data) |
| 7 | +---@param tags string[] |
| 8 | +local function gather_tag_location_list(client, tags) |
7 | 9 | -- Gather tag locations.
|
8 | 10 | local tag_locations = {}
|
9 |
| - local tags = util.tbl_unique(data.fargs) |
10 | 11 | local tag_locs = client:find_tags(tags, { sort = true })
|
11 | 12 | for _, tag_loc in ipairs(tag_locs) do
|
12 | 13 | for _, tag in ipairs(tags) do
|
@@ -92,9 +93,57 @@ return function(client, data)
|
92 | 93 | loclist:render(view_lines, folds, highlights)
|
93 | 94 |
|
94 | 95 | log.info(
|
95 |
| - "Showing tag locations.\n\n" |
| 96 | + "Showing tag locations for '%s'.\n\n" |
96 | 97 | .. "TIPS:\n\n"
|
97 | 98 | .. "- Hit ENTER on a match to go to the tag location\n"
|
98 |
| - .. "- Hit ENTER on a group header to toggle the fold, or use normal fold mappings" |
| 99 | + .. "- Hit ENTER on a group header to toggle the fold, or use normal fold mappings", |
| 100 | + table.concat(tags, "', '") |
99 | 101 | )
|
100 | 102 | end
|
| 103 | + |
| 104 | +---@param client obsidian.Client |
| 105 | +return function(client, data) |
| 106 | + local tags = data.fargs |
| 107 | + |
| 108 | + if vim.tbl_isempty(tags) then |
| 109 | + -- Check for visual selection. |
| 110 | + local _, csrow, cscol, _ = unpack(assert(vim.fn.getpos "'<")) |
| 111 | + local _, cerow, cecol, _ = unpack(assert(vim.fn.getpos "'>")) |
| 112 | + if data.line1 == csrow and data.line2 == cerow then |
| 113 | + local lines = vim.fn.getline(csrow, cerow) |
| 114 | + if #lines ~= 1 then |
| 115 | + log.err "Only in-line visual selections allowed" |
| 116 | + return |
| 117 | + end |
| 118 | + |
| 119 | + local line = assert(lines[1]) |
| 120 | + local tag = string.sub(line, cscol, cecol) |
| 121 | + |
| 122 | + if not string.match(tag, "^#?" .. search.Patterns.TagCharsRequired .. "$") then |
| 123 | + log.err("Visual selection '%s' is not a valid tag", tag) |
| 124 | + return |
| 125 | + end |
| 126 | + |
| 127 | + if vim.startswith(tag, "#") then |
| 128 | + tag = string.sub(tag, 2) |
| 129 | + end |
| 130 | + |
| 131 | + tags = { tag } |
| 132 | + else |
| 133 | + -- Otherwise check for a tag under the cursor. |
| 134 | + local tag = util.cursor_tag() |
| 135 | + if tag then |
| 136 | + tags = { tag } |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + if vim.tbl_isempty(tags) then |
| 142 | + log.warn "Please provide a tag argument" |
| 143 | + return |
| 144 | + end |
| 145 | + |
| 146 | + tags = util.tbl_unique(tags) |
| 147 | + |
| 148 | + return gather_tag_location_list(client, tags) |
| 149 | +end |
0 commit comments