From abc187003997cb4140b2c7e153487ad492cda049 Mon Sep 17 00:00:00 2001 From: Anton Shcherbyna Date: Mon, 30 Nov 2026 21:21:52 +0000 Subject: [PATCH] fix: index API parameter documentation Index detailed API docs and parameter names at lower priority so users can find functions by parameter help without degrading exact-name ranking. AI-assisted with OpenAI Codex (GPT-5-based model); implementation and tests were reviewed before submission. --- .../search/search.worker.test.ts | 70 +++++++++++++++++++ src/documentation/search/search.worker.ts | 60 ++++++++++++---- 2 files changed, 117 insertions(+), 13 deletions(-) diff --git a/src/documentation/search/search.worker.test.ts b/src/documentation/search/search.worker.test.ts index 2651d59b1..eac323078 100644 --- a/src/documentation/search/search.worker.test.ts +++ b/src/documentation/search/search.worker.test.ts @@ -135,6 +135,76 @@ describe("buildReferenceIndex", () => { }); }); +describe("buildApiIndex", () => { + const reference: Toolkit = { + id: "reference", + description: "Reference stuff", + name: "Reference", + contents: [], + language: "en", + }; + const api: ApiDocsResponse = { + "microbit.display": { + id: "microbit.display", + name: "display", + fullName: "microbit.display", + kind: "module", + docString: "Control the LED display.", + children: [ + { + id: "microbit.display.delay", + name: "delay", + fullName: "microbit.display.delay", + kind: "variable", + docString: "A timing value.", + }, + { + id: "microbit.display.show", + name: "show", + fullName: "microbit.display.show", + kind: "function", + params: [ + { name: "image", category: "simple" }, + { name: "loop", category: "simple", defaultValue: "False" }, + ], + docString: + "Shows an image.\n\nExample: `display.show(Image.HEART)`\n\n:param image: The image to show.\n:param delay: The time between frames.\nThe animation can happen in the background.", + }, + ], + }, + }; + + it("finds API entries by parameter documentation", async () => { + const search = await buildIndex(reference, api, undefined, undefined); + + const results = search.search("background").api; + + expect(results).toHaveLength(1); + expect(results[0].id).toEqual("microbit.display.show"); + expect(results[0].extract.content).toContainEqual({ + extract: "background.", + type: "match", + }); + }); + + it("finds API entries by parameter name", async () => { + const search = await buildIndex(reference, api, undefined, undefined); + + expect(search.search("loop").api.map((result) => result.id)).toContain( + "microbit.display.show" + ); + }); + + it("ranks an exact API name above a parameter documentation match", async () => { + const search = await buildIndex(reference, api, undefined, undefined); + + expect(search.search("delay").api.map((result) => result.id)).toEqual([ + "microbit.display.delay", + "microbit.display.show", + ]); + }); +}); + describe("SearchWorker", () => { it("blocks queries on initialization", async () => { const postMessage = vi.fn(); diff --git a/src/documentation/search/search.worker.ts b/src/documentation/search/search.worker.ts index 7551aaff1..526fd94da 100644 --- a/src/documentation/search/search.worker.ts +++ b/src/documentation/search/search.worker.ts @@ -123,6 +123,7 @@ const getExtracts = ( content: SearchableContent ): Extracts => { const allContentPositions: Position[] = []; + const allDetailPositions: Position[] = []; const allTitlePositions: Position[] = []; for (const match of Object.values(matchMetadata)) { @@ -136,11 +137,21 @@ const getExtracts = ( allContentPositions.push(p); }); } + if (match.detail) { + match.detail.position.forEach((p) => { + allDetailPositions.push(p); + }); + } } + const detailOnlyMatch = + allContentPositions.length === 0 && allDetailPositions.length > 0; + return { title: fullStringExtracts(allTitlePositions, content.title), - content: contextExtracts(allContentPositions, content.content), + content: detailOnlyMatch + ? contextExtracts(allDetailPositions, content.detail ?? "") + : contextExtracts(allContentPositions, content.content), }; }; @@ -163,6 +174,12 @@ export interface SearchableContent { containerTitle: string; title: string; content: string; + /** + * Lower-priority content that should be searchable but not outrank names or + * summaries. For API entries this contains parameter names and the detailed + * part of the docstring. + */ + detail?: string; } const defaultString = (string: string | undefined): string => { @@ -203,27 +220,43 @@ const apiSearchableContent = ( toolkit: ApiDocsResponse ): SearchableContent[] => { const content: SearchableContent[] = []; + const searchableEntry = ( + entry: ApiDocsEntry, + title: string, + containerTitle: string + ): SearchableContent => { + const { summary, remainder } = splitDocString( + defaultString(entry.docString) + ); + const parameterNames = entry.params + ?.filter((parameter) => parameter.name && parameter.name !== "self") + .map((parameter) => parameter.name) + .join(" "); + return { + id: entry.id, + title, + containerTitle, + content: summary, + detail: [parameterNames, remainder].filter(Boolean).join("\n\n"), + }; + }; const addNestedDocs = ( moduleName: string, entries: ApiDocsEntry[] | undefined ): void => { entries?.forEach((c) => { - content.push({ - id: c.id, - title: c.fullName.substring(moduleName.length + 1), - containerTitle: moduleName, - content: splitDocString(defaultString(c.docString)).summary, - }); + content.push( + searchableEntry( + c, + c.fullName.substring(moduleName.length + 1), + moduleName + ) + ); addNestedDocs(moduleName, c.children); }); }; for (const module of Object.values(toolkit)) { - content.push({ - id: module.id, - title: module.fullName, - containerTitle: module.fullName, - content: splitDocString(defaultString(module.docString)).summary, - }); + content.push(searchableEntry(module, module.fullName, module.fullName)); addNestedDocs(module.fullName, module.children); } return content; @@ -256,6 +289,7 @@ export const buildSearchIndex = ( }); this.field("title", { boost: 10 }); this.field("content"); + this.field("detail", { boost: 0.25 }); this.use(languagePlugin); plugins.forEach((p) => this.use(p));