Skip to content

Commit

Permalink
Only collect anchors/blocks when prompted in completion (#590)
Browse files Browse the repository at this point in the history
* Only collect anchors/blocks when prompted in completion

Closes #589.

* changelog
  • Loading branch information
epwalsh committed May 31, 2024
1 parent 2d0432c commit fda8df4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added config option `ui.max_file_length` to disable the UI for files with more than this many lines. Defaults to 5000.
- 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.

### Changed

- Optimization: only show completions for blocks/anchors when prompted with `#`.

## [v3.7.13](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.13) - 2024-05-31

### Fixed
Expand Down
19 changes: 9 additions & 10 deletions lua/cmp_obsidian.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ source.complete = function(_, request, callback)

for note in iter(results) do
---@cast note obsidian.Note
assert(note.anchor_links)
assert(note.blocks)

-- Collect matching block links.
---@type obsidian.note.Block[]|?
local matching_blocks
if block_link then
assert(note.blocks)
matching_blocks = {}
for block_id, block_data in pairs(note.blocks) do
if vim.startswith("#" .. block_id, block_link) then
Expand All @@ -83,6 +82,7 @@ source.complete = function(_, request, callback)
---@type obsidian.note.HeaderAnchor[]|?
local matching_anchors
if anchor_link then
assert(note.anchor_links)
matching_anchors = {}
for anchor, anchor_data in pairs(note.anchor_links) do
if vim.startswith(anchor, anchor_link) then
Expand Down Expand Up @@ -120,10 +120,10 @@ source.complete = function(_, request, callback)
end

-- Add all blocks and anchors, let cmp sort it out.
for _, anchor_data in pairs(note.anchor_links) do
for _, anchor_data in pairs(note.anchor_links or {}) do
table.insert(completion_options, { label = label, alt_label = alt_label, anchor = anchor_data })
end
for _, block_data in pairs(note.blocks) do
for _, block_data in pairs(note.blocks or {}) do
table.insert(completion_options, { label = label, alt_label = alt_label, block = block_data })
end
end
Expand Down Expand Up @@ -280,7 +280,7 @@ source.complete = function(_, request, callback)

callback {
items = items,
isIncomplete = false,
isIncomplete = true,
}
end

Expand All @@ -292,11 +292,10 @@ source.complete = function(_, request, callback)
callback { isIncomplete = true }
end
else
client:find_notes_async(
search,
search_callback,
{ search = { ignore_case = true }, notes = { collect_anchor_links = true, collect_blocks = true } }
)
client:find_notes_async(search, search_callback, {
search = { ignore_case = true },
notes = { collect_anchor_links = anchor_link ~= nil, collect_blocks = block_link ~= nil },
})
end
end

Expand Down

0 comments on commit fda8df4

Please sign in to comment.