Skip to content

Commit fda8df4

Browse files
authored
Only collect anchors/blocks when prompted in completion (#590)
* Only collect anchors/blocks when prompted in completion Closes #589. * changelog
1 parent 2d0432c commit fda8df4

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Added config option `ui.max_file_length` to disable the UI for files with more than this many lines. Defaults to 5000.
1313
- Added config option `search_max_lines` (defaults to 1000) for controlling the max number of lines read from disk from note files during certain searches.
1414

15+
### Changed
16+
17+
- Optimization: only show completions for blocks/anchors when prompted with `#`.
18+
1519
## [v3.7.13](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.13) - 2024-05-31
1620

1721
### Fixed

lua/cmp_obsidian.lua

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ source.complete = function(_, request, callback)
5959

6060
for note in iter(results) do
6161
---@cast note obsidian.Note
62-
assert(note.anchor_links)
63-
assert(note.blocks)
6462

6563
-- Collect matching block links.
6664
---@type obsidian.note.Block[]|?
6765
local matching_blocks
6866
if block_link then
67+
assert(note.blocks)
6968
matching_blocks = {}
7069
for block_id, block_data in pairs(note.blocks) do
7170
if vim.startswith("#" .. block_id, block_link) then
@@ -83,6 +82,7 @@ source.complete = function(_, request, callback)
8382
---@type obsidian.note.HeaderAnchor[]|?
8483
local matching_anchors
8584
if anchor_link then
85+
assert(note.anchor_links)
8686
matching_anchors = {}
8787
for anchor, anchor_data in pairs(note.anchor_links) do
8888
if vim.startswith(anchor, anchor_link) then
@@ -120,10 +120,10 @@ source.complete = function(_, request, callback)
120120
end
121121

122122
-- Add all blocks and anchors, let cmp sort it out.
123-
for _, anchor_data in pairs(note.anchor_links) do
123+
for _, anchor_data in pairs(note.anchor_links or {}) do
124124
table.insert(completion_options, { label = label, alt_label = alt_label, anchor = anchor_data })
125125
end
126-
for _, block_data in pairs(note.blocks) do
126+
for _, block_data in pairs(note.blocks or {}) do
127127
table.insert(completion_options, { label = label, alt_label = alt_label, block = block_data })
128128
end
129129
end
@@ -280,7 +280,7 @@ source.complete = function(_, request, callback)
280280

281281
callback {
282282
items = items,
283-
isIncomplete = false,
283+
isIncomplete = true,
284284
}
285285
end
286286

@@ -292,11 +292,10 @@ source.complete = function(_, request, callback)
292292
callback { isIncomplete = true }
293293
end
294294
else
295-
client:find_notes_async(
296-
search,
297-
search_callback,
298-
{ search = { ignore_case = true }, notes = { collect_anchor_links = true, collect_blocks = true } }
299-
)
295+
client:find_notes_async(search, search_callback, {
296+
search = { ignore_case = true },
297+
notes = { collect_anchor_links = anchor_link ~= nil, collect_blocks = block_link ~= nil },
298+
})
300299
end
301300
end
302301

0 commit comments

Comments
 (0)