Skip to content

Commit c8b7d91

Browse files
authored
Add :ObsidianTags command (#345)
* Add `:ObsidainTags` command Closes #249. * CHANGELOG * Clean up
1 parent 5f469fa commit c8b7d91

10 files changed

+347
-143
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Added
11+
12+
- Added `:ObsidianTags` command.
13+
1014
### Changed
1115

1216
- Changed API of client methods `Client:find_tags()` and `Client:find_tags_async()`. The return value (or value passed to the callback) is now a list of objects representing the location of tags found. These objects have the following fields: `tag: string`, `path: string|Path`, `line: integer`.

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ _Keep in mind this plugin is not meant to replace Obsidian, but to complement it
5050

5151
- `:ObsidianBacklinks` for getting a location list of references to the current buffer.
5252

53+
- `:ObsidianTags [TAG ...]` for getting a location list of all occurrences of the given tags.
54+
5355
- `:ObsidianToday [OFFSET]` to open/create a new daily note. This command also takes an optional offset in days, e.g. use `:ObsidianToday -1` to go to yesterday's note. Unlike `:ObsidianYesterday` and `:ObsidianTomorrow` this command does not differentiate between weekdays and weekends.
5456

5557
- `:ObsidianYesterday` to open/create the daily note for the previous working day.
@@ -343,7 +345,15 @@ This is a complete list of all of the options that can be passed to `require("ob
343345

344346
-- Optional, customize the backlinks interface.
345347
backlinks = {
346-
-- The default height of the backlinks pane.
348+
-- The default height of the backlinks location list.
349+
height = 10,
350+
-- Whether or not to wrap lines.
351+
wrap = true,
352+
},
353+
354+
-- Optional, customize the tags interface.
355+
tags = {
356+
-- The default height of the tags location list.
347357
height = 10,
348358
-- Whether or not to wrap lines.
349359
wrap = true,

lua/obsidian/backlinks.lua

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ local abc = require "obsidian.abc"
22
local async = require "plenary.async"
33
local channel = require("plenary.async.control").channel
44
local AsyncExecutor = require("obsidian.async").AsyncExecutor
5+
local LocationList = require "obsidian.location_list"
56
local Note = require "obsidian.note"
67
local search = require "obsidian.search"
78
local log = require "obsidian.log"
89
local util = require "obsidian.util"
9-
local qf = require "obsidian.quickfix"
1010
local iter = require("obsidian.itertools").iter
1111

1212
local NAMESPACE = "ObsidianBacklinks"
@@ -129,7 +129,7 @@ Backlinks.view = function(self, callback)
129129
end, function(backlink_matches)
130130
vim.schedule(function()
131131
if not vim.tbl_isempty(backlink_matches) then
132-
local qfw = qf.new(self.client, self.bufnr, self.winnr, NAMESPACE, self.client.opts.backlinks)
132+
local loclist = LocationList.new(self.client, self.bufnr, self.winnr, NAMESPACE, self.client.opts.backlinks)
133133

134134
local view_lines = {}
135135
local highlights = {}
@@ -141,9 +141,10 @@ Backlinks.view = function(self, callback)
141141
highlights[#highlights + 1] = { group = "CursorLineNr", line = #view_lines - 1, col_start = 0, col_end = 1 }
142142
highlights[#highlights + 1] = { group = "Directory", line = #view_lines - 1, col_start = 2, col_end = -1 }
143143

144+
local display_path = assert(self.client:vault_relative_path(match.note.path))
145+
144146
-- Line for backlink within note.
145147
for line_match in iter(match.matches) do
146-
local display_path = assert(self.client:vault_relative_path(match.note.path))
147148
local text, ref_indices, ref_strs = search.find_and_replace_refs(line_match.text)
148149
local text_start = 4 + display_path:len() + tostring(line_match.line):len()
149150
view_lines[#view_lines + 1] = (" %s:%s:%s"):format(display_path, line_match.line, text)
@@ -152,32 +153,32 @@ Backlinks.view = function(self, callback)
152153
for i, ref_idx in ipairs(ref_indices) do
153154
local ref_str = ref_strs[i]
154155
if string.find(ref_str, tostring(self.note.id), 1, true) ~= nil then
155-
table.insert(highlights, {
156+
highlights[#highlights + 1] = {
156157
group = "Search",
157158
line = #view_lines - 1,
158159
col_start = text_start + ref_idx[1] - 1,
159160
col_end = text_start + ref_idx[2],
160-
})
161+
}
161162
end
162163
end
163164

164165
-- Add highlight for path and line number
165-
table.insert(highlights, {
166+
highlights[#highlights + 1] = {
166167
group = "Comment",
167168
line = #view_lines - 1,
168169
col_start = 2,
169170
col_end = text_start,
170-
})
171+
}
171172
end
172173

173-
table.insert(folds, { range = { #view_lines - #match.matches, #view_lines } })
174-
table.insert(view_lines, "")
174+
folds[#folds + 1] = { range = { #view_lines - #match.matches, #view_lines } }
175+
view_lines[#view_lines + 1] = ""
175176
end
176177

177178
-- Remove last blank line.
178179
view_lines[#view_lines] = nil
179180

180-
qfw:render(view_lines, folds, highlights)
181+
loclist:render(view_lines, folds, highlights)
181182
end
182183

183184
if callback ~= nil then

0 commit comments

Comments
 (0)