Skip to content

Commit

Permalink
Make Client:resolve_note a little fuzzy
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Mar 11, 2024
1 parent 979f154 commit 4d3f2c2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed renaming when passing file path
- Make `Client:resolve_note` fall back to fuzzy matching when there are no exact matches.

## [v3.7.1](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.1) - 2024-03-09

Expand Down
36 changes: 25 additions & 11 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -633,25 +633,39 @@ Client.resolve_note_async = function(self, query, callback, opts)

self:find_notes_async(query, function(results)
local query_lwr = string.lower(query)
local maybe_matches = {}

-- We'll gather both exact matches (of ID, filename, and aliases) and fuzzy matches.
-- If we end up with any exact matches, we'll return those. Otherwise we fall back to fuzzy
-- matches.
---@type obsidian.Note[]
local exact_matches = {}
---@type obsidian.Note[]
local fuzzy_matches = {}

for note in iter(results) do
if
query_lwr == string.lower(tostring(note.id))
or query_lwr == string.lower(note:display_name())
or query == note.path.name
then
table.insert(maybe_matches, note)
---@cast note obsidian.Note

local reference_ids = note:reference_ids { lowercase = true }

-- Check for exact match.
if util.tbl_contains(reference_ids, query_lwr) then
table.insert(exact_matches, note)
else
for alias in iter(note.aliases) do
if query_lwr == string.lower(alias) then
table.insert(maybe_matches, note)
-- Fall back to fuzzy match.
for ref_id in iter(reference_ids) do
if util.string_contains(ref_id, query_lwr) then
table.insert(fuzzy_matches, note)
break
end
end
end
end

return callback(unpack(maybe_matches))
if #exact_matches > 0 then
return callback(unpack(exact_matches))
else
return callback(unpack(fuzzy_matches))
end
end, { search = { sort = true, ignore_case = true }, notes = opts.notes })
end

Expand Down
22 changes: 22 additions & 0 deletions lua/obsidian/note.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,28 @@ Note.fname = function(self)
end
end

--- Get a list of all of the different string that can identify this note via references,
--- including the ID, aliases, and filename.
---@param opts { lowercase: boolean|? }|?
---@return string[]
Note.reference_ids = function(self, opts)
opts = opts or {}
---@type string[]
local ref_ids = { tostring(self.id), self:display_name() }
if self.path then
table.insert(ref_ids, self.path.name)
table.insert(ref_ids, self.path.stem)
end

vim.list_extend(ref_ids, self.aliases)

if opts.lowercase then
ref_ids = vim.tbl_map(string.lower, ref_ids)
end

return util.tbl_unique(ref_ids)
end

Note.should_save_frontmatter = function(self)
local fname = self:fname()
return (fname ~= nil and not util.tbl_contains(SKIP_UPDATING_FRONTMATTER, fname))
Expand Down

0 comments on commit 4d3f2c2

Please sign in to comment.