Skip to content

Commit 2301839

Browse files
feat(main): Fix alias highlighting in search results #301
1 parent 68b9096 commit 2301839

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

src/matcher.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ export interface MatchQueryResult {
3838
meta?: string[];
3939
score?: number;
4040
ranges?: { start: number; end: number }[];
41+
// All alias ranges for highlighting purposes (only used for alias matches)
42+
allAliasRanges?: {
43+
alias: string;
44+
ranges: { start: number; end: number }[];
45+
}[];
4146
}
4247

4348
function matchQuery(
@@ -176,6 +181,10 @@ function matchQuery(
176181
alias: bestMatch.value,
177182
query,
178183
ranges: bestMatch.ranges,
184+
allAliasRanges: prefixNameMatchedAliases.map((x) => ({
185+
alias: x.value,
186+
ranges: x.ranges || [],
187+
})),
179188
});
180189
}
181190
if (nameMatchedAliases.length > 0) {
@@ -186,6 +195,10 @@ function matchQuery(
186195
alias: bestMatch.value,
187196
query,
188197
ranges: bestMatch.ranges,
198+
allAliasRanges: nameMatchedAliases.map((x) => ({
199+
alias: x.value,
200+
ranges: x.ranges || [],
201+
})),
189202
});
190203
}
191204
if (options.fuzzyTarget && fuzzyNameMatchedAliases.length > 0) {
@@ -197,6 +210,10 @@ function matchQuery(
197210
score: bestMatch.score,
198211
query,
199212
ranges: bestMatch.ranges,
213+
allAliasRanges: fuzzyNameMatchedAliases.map((x) => ({
214+
alias: x.value,
215+
ranges: x.ranges || [],
216+
})),
200217
});
201218
}
202219

src/ui/suggestion-factory.ts

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ function createHighlightedText(
8585
if (range.start > lastEnd + 1) {
8686
const beforeText = text.slice(lastEnd + 1, range.start);
8787
if (beforeText) {
88-
fragment.appendChild(document.createTextNode(beforeText));
88+
// Wrap text nodes in span to ensure proper display in flex containers
89+
const textSpan = createSpan({ text: beforeText });
90+
fragment.appendChild(textSpan);
8991
}
9092
}
9193

@@ -106,7 +108,9 @@ function createHighlightedText(
106108
if (lastEnd + 1 < text.length) {
107109
const remainingText = text.slice(lastEnd + 1);
108110
if (remainingText) {
109-
fragment.appendChild(document.createTextNode(remainingText));
111+
// Wrap text nodes in span to ensure proper display in flex containers
112+
const textSpan = createSpan({ text: remainingText });
113+
fragment.appendChild(textSpan);
110114
}
111115
}
112116

@@ -145,12 +149,23 @@ function createItemDiv(
145149
: item.file.basename;
146150

147151
// Find relevant match results for title highlighting
148-
const titleMatchResults = item.matchResults.filter(
149-
(result) =>
150-
result.type === "name" ||
151-
result.type === "prefix-name" ||
152-
result.type === "fuzzy-name",
153-
);
152+
// When showing alias as title, use alias match results
153+
// When showing file name as title, use only file name match results (not alias matches)
154+
const titleMatchResults = shouldShowAliasAsTitle
155+
? item.matchResults.filter(
156+
(result) =>
157+
(result.type === "name" ||
158+
result.type === "prefix-name" ||
159+
result.type === "fuzzy-name") &&
160+
result.alias,
161+
)
162+
: item.matchResults.filter(
163+
(result) =>
164+
(result.type === "name" ||
165+
result.type === "prefix-name" ||
166+
result.type === "fuzzy-name") &&
167+
!result.alias,
168+
);
154169

155170
const titleDiv = createDiv({
156171
cls: [
@@ -299,6 +314,10 @@ function createDescriptionDiv(args: {
299314
linkResultsNum: number;
300315
headerResultsNum: number;
301316
options: Options;
317+
aliasMatchDetails: {
318+
alias: string;
319+
ranges: { start: number; end: number }[];
320+
}[];
302321
}): Elements["descriptionDiv"] {
303322
const {
304323
item,
@@ -309,6 +328,7 @@ function createDescriptionDiv(args: {
309328
linkResultsNum,
310329
headerResultsNum,
311330
options,
331+
aliasMatchDetails,
312332
} = args;
313333

314334
const descriptionDiv = createDiv({
@@ -328,7 +348,25 @@ function createDescriptionDiv(args: {
328348
cls: "another-quick-switcher__item__description__alias",
329349
});
330350
aliasSpan.insertAdjacentHTML("beforeend", ALIAS);
331-
aliasSpan.appendText(x);
351+
352+
// Find matching ranges for this specific alias using allAliasRanges
353+
const ranges: { start: number; end: number }[] = [];
354+
355+
// Collect all ranges for this specific alias from allAliasRanges
356+
for (const result of item.matchResults) {
357+
if (result.allAliasRanges) {
358+
for (const aliasRange of result.allAliasRanges) {
359+
if (aliasRange.alias === x) {
360+
ranges.push(...aliasRange.ranges);
361+
}
362+
}
363+
}
364+
}
365+
366+
// Apply highlighting using createHighlightedText
367+
const highlightedContent = createHighlightedText(x, ranges);
368+
aliasSpan.appendChild(highlightedContent);
369+
332370
aliasDiv.appendChild(aliasSpan);
333371
}
334372
descriptionDiv.appendChild(aliasDiv);
@@ -454,6 +492,14 @@ export function createElements(
454492
headerResults.flatMap((xs) => uniq(xs.meta ?? [])),
455493
);
456494

495+
// Extract alias match details for highlighting in description
496+
const aliasMatchDetails: {
497+
alias: string;
498+
ranges: { start: number; end: number }[];
499+
}[] = item.matchResults
500+
.filter((result) => result.allAliasRanges)
501+
.flatMap((result) => result.allAliasRanges!);
502+
457503
const descriptionDiv =
458504
aliases.length !== 0 ||
459505
tags.length !== 0 ||
@@ -468,6 +514,7 @@ export function createElements(
468514
linkResultsNum,
469515
headerResultsNum,
470516
options,
517+
aliasMatchDetails,
471518
})
472519
: undefined;
473520

0 commit comments

Comments
 (0)