571
571
--- @field tag_start integer |? The index within ' text' where the tag starts.
572
572
--- @field tag_end integer |? The index within ' text' where the tag ends.
573
573
574
- --- Find all tags starting with the given term(s).
574
+ --- Find all tags starting with the given search term(s).
575
575
---
576
- --- @param term string | string[]
576
+ --- @param term string | string[] The search term
577
577
--- @param opts obsidian.SearchOpts | boolean |? search options or a boolean indicating if sorting should be used
578
578
--- @param timeout integer |? Timeout in milliseconds.
579
579
---
586
586
587
587
--- An async version of 'find_tags()'.
588
588
---
589
- --- @param term string | string[]
589
+ --- @param term string | string[] The search term
590
590
--- @param opts obsidian.SearchOpts | boolean |? search options or a boolean indicating if sorting should be used
591
591
--- @param callback fun ( tags : obsidian.TagLocation[] )
592
592
Client .find_tags_async = function (self , term , opts , callback )
@@ -676,24 +676,18 @@ Client.find_tags_async = function(self, term, opts, callback)
676
676
for match in iter (search .find_tags (line )) do
677
677
local m_start , m_end , _ = unpack (match )
678
678
local tag = string.sub (line , m_start + 1 , m_end )
679
- for t in iter (terms ) do
680
- -- NOTE: this works when 't' is an empty string too.
681
- if vim .startswith (string.lower (tag ), t ) then
682
- add_match (tag , path , note , match_data .line_number , line , m_start , m_end )
683
- n_matches = n_matches + 1
684
- break
685
- end
679
+ if string.match (tag , " ^" .. search .Patterns .TagCharsRequired .. " $" ) then
680
+ add_match (tag , path , note , match_data .line_number , line )
686
681
end
687
682
end
688
683
689
684
-- check for tags in frontmatter
690
685
if n_matches == 0 and note .tags ~= nil and (vim .startswith (line , " tags:" ) or string.match (line , " %s*- " )) then
691
686
for tag in iter (note .tags ) do
692
687
tag = tostring (tag )
693
- for t in iter (terms ) do
694
- if vim . startswith ( string.lower (tag ) , t ) then
688
+ for _ , t in ipairs (terms ) do
689
+ if string.len ( t ) == 0 or util . string_contains (tag , t ) then
695
690
add_match (tag , path , note , match_data .line_number , line )
696
- break
697
691
end
698
692
end
699
693
end
@@ -706,13 +700,25 @@ Client.find_tags_async = function(self, term, opts, callback)
706
700
local search_terms = {}
707
701
for t in iter (terms ) do
708
702
if string.len (t ) > 0 then
709
- search_terms [# search_terms + 1 ] = " #" .. t .. search .Patterns .TagCharsOptional -- tag in the wild
710
- search_terms [# search_terms + 1 ] = " \\ s*- " .. t .. search .Patterns .TagCharsOptional -- frontmatter tag in multiline list
711
- search_terms [# search_terms + 1 ] = " tags: .*" .. t .. search .Patterns .TagCharsOptional -- frontmatter tag in inline list
703
+ -- tag in the wild
704
+ search_terms [# search_terms + 1 ] = " #" .. search .Patterns .TagCharsOptional .. t .. search .Patterns .TagCharsOptional
705
+ -- frontmatter tag in multiline list
706
+ search_terms [# search_terms + 1 ] = " \\ s*- "
707
+ .. search .Patterns .TagCharsOptional
708
+ .. t
709
+ .. search .Patterns .TagCharsOptional
710
+ -- frontmatter tag in inline list
711
+ search_terms [# search_terms + 1 ] = " tags: .*"
712
+ .. search .Patterns .TagCharsOptional
713
+ .. t
714
+ .. search .Patterns .TagCharsOptional
712
715
else
713
- search_terms [# search_terms + 1 ] = " #" .. search .Patterns .TagCharsRequired -- tag in the wild
714
- search_terms [# search_terms + 1 ] = " \\ s*- " .. search .Patterns .TagCharsRequired -- frontmatter tag in multiline list
715
- search_terms [# search_terms + 1 ] = " tags: .*" .. search .Patterns .TagCharsRequired -- frontmatter tag in inline list
716
+ -- tag in the wild
717
+ search_terms [# search_terms + 1 ] = " #" .. search .Patterns .TagCharsRequired
718
+ -- frontmatter tag in multiline list
719
+ search_terms [# search_terms + 1 ] = " \\ s*- " .. search .Patterns .TagCharsRequired
720
+ -- frontmatter tag in inline list
721
+ search_terms [# search_terms + 1 ] = " tags: .*" .. search .Patterns .TagCharsRequired
716
722
end
717
723
end
718
724
@@ -766,14 +772,16 @@ Client.find_tags_async = function(self, term, opts, callback)
766
772
end , callback )
767
773
end
768
774
769
- --- Gather a list of all tags in the vault.
775
+ --- Gather a list of all tags in the vault. If 'term' is provided, only tags that partially match the search
776
+ --- term will be included.
770
777
---
771
- --- @param timeout integer |? Timeout in milliseconds.
778
+ --- @param term string |? An optional search term to match tags
779
+ --- @param timeout integer |? Timeout in milliseconds
772
780
---
773
781
--- @return string[]
774
- Client .list_tags = function (self , timeout )
782
+ Client .list_tags = function (self , term , timeout )
775
783
local tags = {}
776
- for _ , tag_loc in ipairs (self :find_tags (" " , nil , timeout )) do
784
+ for _ , tag_loc in ipairs (self :find_tags (term and term or " " , nil , timeout )) do
777
785
tags [tag_loc .tag ] = true
778
786
end
779
787
return vim .tbl_keys (tags )
782
790
--- An async version of 'list_tags()'.
783
791
---
784
792
--- @param callback fun ( tags : string[] )
785
- Client .list_tags_async = function (self , callback )
786
- self :find_tags_async (" " , nil , function (tag_locations )
793
+ --- @param term string |?
794
+ Client .list_tags_async = function (self , callback , term )
795
+ self :find_tags_async (term and term or " " , nil , function (tag_locations )
787
796
local tags = {}
788
797
for _ , tag_loc in ipairs (tag_locations ) do
789
798
tags [tag_loc .tag ] = true
0 commit comments