Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-snippet-terminal-signature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stainless-code/codemap": patch
---

Print symbol signature in `codemap snippet` terminal output, matching `codemap show` and the documented contract.
39 changes: 38 additions & 1 deletion src/cli/cmd-snippet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createTables } from "../db";
import type { CodemapDatabase } from "../db";
import { hashContent } from "../hash";
import { openCodemapDatabase } from "../sqlite-db";
import { parseSnippetRest } from "./cmd-snippet";
import { parseSnippetRest, renderSnippetTerminal } from "./cmd-snippet";

describe("parseSnippetRest", () => {
it("returns help on --help / -h", () => {
Expand Down Expand Up @@ -69,6 +69,43 @@ describe("parseSnippetRest", () => {
});
});

describe("renderSnippetTerminal", () => {
it("prints signature between location and source", () => {
const lines: string[] = [];
const origLog = console.log;
console.log = (...args: unknown[]) => {
lines.push(
args.map((a) => (typeof a === "string" ? a : String(a))).join(" "),
);
};
try {
renderSnippetTerminal({
matches: [
{
name: "foo",
kind: "function",
file_path: "src/a.ts",
line_start: 1,
line_end: 2,
signature: "function foo(): void",
is_exported: 1,
parent_name: null,
visibility: null,
source: "line1\nline2",
stale: false,
missing: false,
},
],
});
} finally {
console.log = origLog;
}
expect(lines[0]).toBe("src/a.ts:1-2");
expect(lines[1]).toBe(" function foo(): void");
expect(lines[2]).toBe("line1\nline2");
});
});

describe("buildSnippetResult — source enrichment + envelope", () => {
let projectRoot: string;
let db: CodemapDatabase;
Expand Down
5 changes: 3 additions & 2 deletions src/cli/cmd-snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export async function runSnippetCmd(opts: SnippetOpts): Promise<void> {
console.log(JSON.stringify(result));
return;
}
renderTerminal(result);
renderSnippetTerminal(result);
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
emitErrorMaybeJson(msg, opts.json);
Expand All @@ -197,7 +197,7 @@ function describeFilter(
return parts.length === 0 ? "" : ` (filters: ${parts.join(", ")})`;
}

function renderTerminal(result: SnippetResult): void {
export function renderSnippetTerminal(result: SnippetResult): void {
let anyStale = false;
for (let i = 0; i < result.matches.length; i++) {
const m = result.matches[i]!;
Expand All @@ -207,6 +207,7 @@ function renderTerminal(result: SnippetResult): void {
console.log(
`${m.file_path}:${m.line_start}-${m.line_end}${stalePrefix}${missingPrefix}`,
);
console.log(` ${m.signature}`);
if (m.source !== undefined) console.log(m.source);
if (m.stale) anyStale = true;
}
Expand Down
Loading