Skip to content

Commit 1794015

Browse files
committed
Code cleanup: extract constants, document regex, remove dead code
- Extract search scoring constants for maintainability - Document TypeRef regex patterns with examples - Remove unused handleTocNavigate callback from Sidebar
1 parent 3548e1b commit 1794015

3 files changed

Lines changed: 37 additions & 20 deletions

File tree

src/lib/components/api/TypeRef.svelte

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
let parts = $derived.by(() => {
2222
const result: TypePart[] = [];
2323
24-
// Regex to match potential class names (PascalCase or known types)
25-
// Split by common type syntax characters while preserving them
24+
// Tokenize type string by brackets, commas, and union operators
25+
// Example: "list[ClassName]" → ["list", "[", "ClassName", "]"]
26+
// Example: "Optional[int | str]" → ["Optional", "[", "int", " | ", "str", "]"]
2627
const tokens = type.split(/(\[|\]|,\s*|\s*\|\s*)/);
2728
2829
for (const token of tokens) {
2930
if (!token) continue;
3031
31-
// Check if this token looks like a class name (PascalCase)
32+
// Match PascalCase class names (no dots)
33+
// Matches: "Integrator", "V1", "MyClass"
34+
// Does NOT match: "list", "pathsim.Connection"
3235
if (/^[A-Z][a-zA-Z0-9_]*$/.test(token)) {
3336
const target = lookupRef(token);
3437
if (target) {
@@ -37,7 +40,10 @@
3740
result.push({ text: token, isLink: false });
3841
}
3942
}
40-
// Check if this is a full module path like pathsim.connection.Connection
43+
// Match fully qualified module paths ending with class name
44+
// Format: module.submodule.ClassName
45+
// Matches: "pathsim.connection.Connection", "numpy.ndarray.ndarray"
46+
// Does NOT match: "Connection", "pathsim.connection"
4147
else if (/^[a-z][a-z0-9_]*(\.[a-z_][a-z0-9_]*)*\.[A-Z][a-zA-Z0-9_]*$/.test(token)) {
4248
// Try lookup by full path first
4349
let target = lookupRef(token);

src/lib/components/layout/Sidebar.svelte

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@
7070
goto(`${base}/${result.path}`);
7171
}
7272
73-
function handleTocNavigate(id: string) {
74-
// Could track navigation for analytics or other purposes
75-
}
76-
7773
function getTypeIcon(type: SearchResult['type']): string {
7874
switch (type) {
7975
case 'page': return 'file';
@@ -137,7 +133,7 @@
137133

138134
{#if !showResults && isApiPage && $apiModulesStore.length > 0}
139135
<div class="sidebar-scrollable">
140-
<ApiToc modules={$apiModulesStore} onNavigate={handleTocNavigate} />
136+
<ApiToc modules={$apiModulesStore} />
141137
</div>
142138
{:else if !showResults && isExamplesListPage && $exampleGroupsStore.length > 0}
143139
<div class="sidebar-scrollable">

src/lib/utils/search.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55

66
import searchIndex from '$lib/api/generated/search-index.json';
77

8+
// Scoring weights for search relevance
9+
const SCORE_EXACT_MATCH = 100;
10+
const SCORE_PREFIX_MATCH = 50;
11+
const SCORE_CONTAINS = 30;
12+
const SCORE_MODULE_CONTAINS = 15;
13+
const SCORE_DESCRIPTION = 10;
14+
const SCORE_PARENT_CLASS = 20;
15+
const SCORE_TAGS = 25;
16+
17+
// Type boost multipliers
18+
const BOOST_PAGE = 1.5;
19+
const BOOST_CLASS = 1.2;
20+
const BOOST_EXAMPLE = 1.15;
21+
const BOOST_FUNCTION = 1.1;
22+
823
export type SearchResultType = 'page' | 'module' | 'class' | 'function' | 'method' | 'example';
924

1025
export interface SearchResult {
@@ -41,39 +56,39 @@ export function search(query: string, limit: number = 20): SearchResult[] {
4156
for (const term of terms) {
4257
// Exact name match (highest priority)
4358
if (lowerName === term) {
44-
score += 100;
59+
score += SCORE_EXACT_MATCH;
4560
}
4661
// Name starts with term
4762
else if (lowerName.startsWith(term)) {
48-
score += 50;
63+
score += SCORE_PREFIX_MATCH;
4964
}
5065
// Name contains term
5166
else if (lowerName.includes(term)) {
52-
score += 30;
67+
score += SCORE_CONTAINS;
5368
}
5469
// Module/category contains term
5570
else if (lowerModule.includes(term)) {
56-
score += 15;
71+
score += SCORE_MODULE_CONTAINS;
5772
}
5873
// Description contains term
5974
else if (lowerDesc.includes(term)) {
60-
score += 10;
75+
score += SCORE_DESCRIPTION;
6176
}
6277
// Parent class contains term
6378
else if (result.parentClass?.toLowerCase().includes(term)) {
64-
score += 20;
79+
score += SCORE_PARENT_CLASS;
6580
}
6681
// Tags contain term (for examples)
6782
else if (result.tags?.some(tag => tag.toLowerCase().includes(term))) {
68-
score += 25;
83+
score += SCORE_TAGS;
6984
}
7085
}
7186

7287
// Boost by type (pages highest, then classes, examples, functions)
73-
if (result.type === 'page') score *= 1.5;
74-
if (result.type === 'class') score *= 1.2;
75-
if (result.type === 'example') score *= 1.15;
76-
if (result.type === 'function') score *= 1.1;
88+
if (result.type === 'page') score *= BOOST_PAGE;
89+
if (result.type === 'class') score *= BOOST_CLASS;
90+
if (result.type === 'example') score *= BOOST_EXAMPLE;
91+
if (result.type === 'function') score *= BOOST_FUNCTION;
7792

7893
return { result, score };
7994
})

0 commit comments

Comments
 (0)