Skip to content

Commit 32740c3

Browse files
authored
Merge pull request #10 from AztecProtocol/feat/prioritize-sdk-sources
feat: prioritize SDK sources over example apps in search results
2 parents 078c719 + 1f24983 commit 32740c3

2 files changed

Lines changed: 108 additions & 8 deletions

File tree

src/utils/search.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ export function findExample(name: string): FileInfo | null {
182182
return match || null;
183183
}
184184

185+
/**
186+
* Get numeric priority for a search result (lower = higher priority).
187+
* SDK sources rank above example apps.
188+
*/
189+
export function getResultPriority(result: SearchResult): number {
190+
const { repo, file } = result;
191+
192+
if (repo === "aztec-packages") {
193+
if (/\baztec-nr\b/.test(file) || /\byarn-project\b/.test(file)) return 1;
194+
if (/\bnoir-contracts\b/.test(file)) return 2;
195+
// Other aztec-packages paths (boxes, playground, etc.)
196+
return 4;
197+
}
198+
199+
if (repo === "noir") {
200+
if (/\bnoir_stdlib\b/.test(file)) return 3;
201+
return 4;
202+
}
203+
204+
// Example apps / community repos
205+
return 5;
206+
}
207+
185208
// --- Helper functions ---
186209

187210
/**
@@ -197,8 +220,6 @@ function parseRgOutput(output: string, maxResults: number): SearchResult[] {
197220
const lines = output.split("\n").filter(Boolean);
198221

199222
for (const line of lines) {
200-
if (results.length >= maxResults) break;
201-
202223
// Format: /path/to/file:linenum:content
203224
const match = line.match(/^(.+?):(\d+):(.*)$/);
204225
if (match) {
@@ -215,7 +236,8 @@ function parseRgOutput(output: string, maxResults: number): SearchResult[] {
215236
}
216237
}
217238

218-
return results;
239+
results.sort((a, b) => getResultPriority(a) - getResultPriority(b));
240+
return results.slice(0, maxResults);
219241
}
220242

221243
function manualSearch(
@@ -243,15 +265,11 @@ function manualSearch(
243265
}
244266

245267
for (const file of files) {
246-
if (results.length >= maxResults) break;
247-
248268
try {
249269
const content = readFileSync(file, "utf-8");
250270
const lines = content.split("\n");
251271

252272
for (let i = 0; i < lines.length; i++) {
253-
if (results.length >= maxResults) break;
254-
255273
if (searchRegex.test(lines[i])) {
256274
const relativePath = relative(REPOS_DIR, file);
257275
const repoPart = relativePath.split("/")[0];
@@ -275,7 +293,8 @@ function manualSearch(
275293
// Globby error, return empty
276294
}
277295

278-
return results;
296+
results.sort((a, b) => getResultPriority(a) - getResultPriority(b));
297+
return results.slice(0, maxResults);
279298
}
280299

281300
function findContracts(basePath: string, repoName: string): FileInfo[] {

tests/utils/search.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
readFile,
3131
findExample,
3232
getFileType,
33+
getResultPriority,
3334
} from "../../src/utils/search.js";
3435

3536
const mockExecSync = vi.mocked(execSync);
@@ -365,3 +366,83 @@ describe("findExample", () => {
365366
expect(result).toBeNull();
366367
});
367368
});
369+
370+
describe("getResultPriority", () => {
371+
it("ranks aztec-nr / yarn-project as highest priority (1)", () => {
372+
expect(getResultPriority({ repo: "aztec-packages", file: "aztec-packages/yarn-project/aztec.js/src/main.ts", content: "", line: 1 })).toBe(1);
373+
expect(getResultPriority({ repo: "aztec-packages", file: "aztec-packages/aztec-nr/aztec/src/lib.nr", content: "", line: 1 })).toBe(1);
374+
});
375+
376+
it("ranks noir-contracts as priority 2", () => {
377+
expect(getResultPriority({ repo: "aztec-packages", file: "aztec-packages/noir-projects/noir-contracts/token/src/main.nr", content: "", line: 1 })).toBe(2);
378+
});
379+
380+
it("ranks noir_stdlib as priority 3", () => {
381+
expect(getResultPriority({ repo: "noir", file: "noir/noir_stdlib/src/hash.nr", content: "", line: 1 })).toBe(3);
382+
});
383+
384+
it("ranks other aztec-packages and noir paths as priority 4", () => {
385+
expect(getResultPriority({ repo: "aztec-packages", file: "aztec-packages/boxes/token/src/main.nr", content: "", line: 1 })).toBe(4);
386+
expect(getResultPriority({ repo: "noir", file: "noir/tooling/nargo/src/lib.rs", content: "", line: 1 })).toBe(4);
387+
});
388+
389+
it("ranks example repos as lowest priority (5)", () => {
390+
expect(getResultPriority({ repo: "aztec-examples", file: "aztec-examples/token/src/main.nr", content: "", line: 1 })).toBe(5);
391+
expect(getResultPriority({ repo: "aztec-starter", file: "aztec-starter/src/main.nr", content: "", line: 1 })).toBe(5);
392+
expect(getResultPriority({ repo: "gregoswap", file: "gregoswap/src/main.nr", content: "", line: 1 })).toBe(5);
393+
});
394+
});
395+
396+
describe("search result sorting", () => {
397+
it("sorts ripgrep results by source priority", () => {
398+
mockExistsSync.mockReturnValue(true);
399+
mockExecSync.mockReturnValue(
400+
"/fake/repos/gregoswap/src/main.nr:1:fn transfer() {\n" +
401+
"/fake/repos/aztec-packages/yarn-project/aztec.js/src/main.ts:5:fn transfer() {\n" +
402+
"/fake/repos/aztec-examples/token/src/main.nr:3:fn transfer() {\n" +
403+
"/fake/repos/aztec-packages/noir-projects/noir-contracts/token/src/main.nr:10:fn transfer() {\n" +
404+
"/fake/repos/noir/noir_stdlib/src/hash.nr:7:fn transfer() {\n"
405+
);
406+
407+
const results = searchCode("transfer");
408+
expect(results[0].repo).toBe("aztec-packages");
409+
expect(results[0].file).toContain("yarn-project");
410+
expect(results[1].repo).toBe("aztec-packages");
411+
expect(results[1].file).toContain("noir-contracts");
412+
expect(results[2].repo).toBe("noir");
413+
expect(results[2].file).toContain("noir_stdlib");
414+
expect(results[3].repo).toBe("gregoswap");
415+
expect(results[4].repo).toBe("aztec-examples");
416+
});
417+
418+
it("sorts manual search results by source priority", () => {
419+
mockExistsSync.mockReturnValue(true);
420+
mockExecSync.mockImplementation(() => { throw new Error("rg not found"); });
421+
mockGlobbySync.mockReturnValue([
422+
"/fake/repos/aztec-starter/src/main.nr",
423+
"/fake/repos/aztec-packages/yarn-project/aztec.js/src/lib.nr",
424+
]);
425+
mockReadFileSync
426+
.mockReturnValueOnce("fn transfer() {" as any)
427+
.mockReturnValueOnce("fn transfer() {" as any);
428+
429+
const results = searchCode("transfer");
430+
expect(results).toHaveLength(2);
431+
expect(results[0].repo).toBe("aztec-packages");
432+
expect(results[1].repo).toBe("aztec-starter");
433+
});
434+
435+
it("applies sorting before maxResults limit", () => {
436+
mockExistsSync.mockReturnValue(true);
437+
mockExecSync.mockReturnValue(
438+
"/fake/repos/gregoswap/src/a.nr:1:match\n" +
439+
"/fake/repos/aztec-examples/src/b.nr:2:match\n" +
440+
"/fake/repos/aztec-packages/yarn-project/c.nr:3:match\n"
441+
);
442+
443+
const results = searchCode("match", { maxResults: 2 });
444+
expect(results).toHaveLength(2);
445+
// SDK result should be kept even though it appeared last in raw output
446+
expect(results[0].repo).toBe("aztec-packages");
447+
});
448+
});

0 commit comments

Comments
 (0)