(_ context.Context, data wshrpc.FetchSuggestionsData)
| 163 | } |
| 164 | |
| 165 | func fetchBookmarkSuggestions(_ context.Context, data wshrpc.FetchSuggestionsData) (*wshrpc.FetchSuggestionsResponse, error) { |
| 166 | if data.SuggestionType != "bookmark" { |
| 167 | return nil, fmt.Errorf("unsupported suggestion type: %q", data.SuggestionType) |
| 168 | } |
| 169 | |
| 170 | // scoredEntry holds a bookmark along with its computed score, the match positions for the |
| 171 | // field that will be used for display, the positions for the secondary field (if any), |
| 172 | // and its original index in the Bookmarks list. |
| 173 | type scoredEntry struct { |
| 174 | bookmark wconfig.WebBookmark |
| 175 | score int |
| 176 | matchPos []int // positions for the field that's used as Display |
| 177 | subMatchPos []int // positions for the other field (if any) |
| 178 | origIndex int |
| 179 | } |
| 180 | |
| 181 | bookmarks := wconfig.GetWatcher().GetFullConfig().Bookmarks |
| 182 | bookmarks = filterBookmarksForValid(bookmarks) |
| 183 | |
| 184 | searchTerm := data.Query |
| 185 | var patternRunes []rune |
| 186 | if searchTerm != "" { |
| 187 | patternRunes = []rune(strings.ToLower(searchTerm)) |
| 188 | } |
| 189 | |
| 190 | var scoredEntries []scoredEntry |
| 191 | var slab util.Slab |
| 192 | |
| 193 | bookmarkKeys := utilfn.GetMapKeys(bookmarks) |
| 194 | // sort by display:order and then by key |
| 195 | sort.Slice(bookmarkKeys, func(i, j int) bool { |
| 196 | bookmarkA := bookmarks[bookmarkKeys[i]] |
| 197 | bookmarkB := bookmarks[bookmarkKeys[j]] |
| 198 | if bookmarkA.DisplayOrder != bookmarkB.DisplayOrder { |
| 199 | return bookmarkA.DisplayOrder < bookmarkB.DisplayOrder |
| 200 | } |
| 201 | return bookmarkKeys[i] < bookmarkKeys[j] |
| 202 | }) |
| 203 | for i, bmkey := range bookmarkKeys { |
| 204 | bookmark := bookmarks[bmkey] |
| 205 | // If no search term, include all bookmarks (score 0, no positions). |
| 206 | if searchTerm == "" { |
| 207 | scoredEntries = append(scoredEntries, scoredEntry{ |
| 208 | bookmark: bookmark, |
| 209 | score: 0, |
| 210 | origIndex: i, |
| 211 | }) |
| 212 | continue |
| 213 | } |
| 214 | |
| 215 | // For bookmarks with a title, Display is set to the title and SubText to the URL. |
| 216 | // We perform fuzzy matching on both fields. |
| 217 | if bookmark.Title != "" { |
| 218 | // Fuzzy match against the title. |
| 219 | candidateTitle := strings.ToLower(bookmark.Title) |
| 220 | textTitle := util.ToChars([]byte(candidateTitle)) |
| 221 | resultTitle, titlePositionsPtr := algo.FuzzyMatchV2(false, true, true, &textTitle, patternRunes, true, &slab) |
| 222 | var titleScore int |
no test coverage detected