fix: show only line numbers in code snippet chip label

Based on user feedback in Issue #10085, the code reference chip
now displays only line numbers (e.g., "lines 10-25") instead of
file path and line numbers (e.g., "file.ts:10-25") since the code
is always from the current page.

Also adds test coverage for code-snippet module.
This commit is contained in:
Roo Code 2025-12-16 09:31:05 +00:00 committed by Hannes Rudolph
parent 9a05952af7
commit f72ba57df8
2 changed files with 100 additions and 3 deletions

View file

@ -0,0 +1,97 @@
import { describe, it, expect } from "vitest"
import {
CodeSnippet,
createCodeSnippetId,
formatCodeSnippetLabel,
expandCodeSnippet,
expandCodeSnippets,
} from "../code-snippet.js"
describe("code-snippet", () => {
describe("createCodeSnippetId", () => {
it("should create unique IDs", () => {
const id1 = createCodeSnippetId()
const id2 = createCodeSnippetId()
expect(id1).not.toBe(id2)
})
it("should start with 'snippet-' prefix", () => {
const id = createCodeSnippetId()
expect(id).toMatch(/^snippet-/)
})
})
describe("formatCodeSnippetLabel", () => {
it("should format label with only line numbers", () => {
const snippet: CodeSnippet = {
id: "test-id",
filePath: "src/components/Button.tsx",
startLine: 10,
endLine: 25,
content: "const Button = () => {}",
timestamp: Date.now(),
}
expect(formatCodeSnippetLabel(snippet)).toBe("lines 10-25")
})
it("should handle single line snippet", () => {
const snippet: CodeSnippet = {
id: "test-id",
filePath: "index.ts",
startLine: 1,
endLine: 1,
content: "export default {}",
timestamp: Date.now(),
}
expect(formatCodeSnippetLabel(snippet)).toBe("lines 1-1")
})
})
describe("expandCodeSnippet", () => {
it("should expand snippet to full format with file path", () => {
const snippet: CodeSnippet = {
id: "test-id",
filePath: "src/utils.ts",
startLine: 5,
endLine: 10,
content: "function helper() {\n return true;\n}",
timestamp: Date.now(),
}
const result = expandCodeSnippet(snippet)
expect(result).toContain("src/utils.ts:5-10")
expect(result).toContain("```")
expect(result).toContain("function helper()")
})
})
describe("expandCodeSnippets", () => {
it("should expand multiple snippets with spacing", () => {
const snippets: CodeSnippet[] = [
{
id: "test-1",
filePath: "file1.ts",
startLine: 1,
endLine: 5,
content: "const a = 1",
timestamp: Date.now(),
},
{
id: "test-2",
filePath: "file2.ts",
startLine: 10,
endLine: 15,
content: "const b = 2",
timestamp: Date.now(),
},
]
const result = expandCodeSnippets(snippets)
expect(result).toContain("file1.ts:1-5")
expect(result).toContain("file2.ts:10-15")
expect(result).toContain("\n\n")
})
it("should return empty string for empty array", () => {
expect(expandCodeSnippets([])).toBe("")
})
})
})

View file

@ -26,11 +26,11 @@ export function createCodeSnippetId(): string {
}
/**
* Formats a code snippet for display in a collapsed chip/pill format
* Formats a code snippet for display in a collapsed chip/pill format.
* Shows only line numbers since the code is always from the current page.
*/
export function formatCodeSnippetLabel(snippet: CodeSnippet): string {
const fileName = snippet.filePath.split("/").pop() || snippet.filePath
return `${fileName}:${snippet.startLine}-${snippet.endLine}`
return `lines ${snippet.startLine}-${snippet.endLine}`
}
/**