Skip to content

Commit b2e4368

Browse files
Fix: Include code snippets with inline file references in chat responses
Fixes #306605 When Copilot CLI provides inline references to code files or locations, it was only showing links to the files without the actual code snippets. This made the responses less useful as users had to manually navigate to the referenced files to see the code. Changes: 1. **Enhanced IChatContentInlineReference interface** (chatService.ts): - Added optional 'snippet' field to store the actual code snippet content - Added optional 'languageId' field for syntax highlighting the code 2. **Updated annotation logic** (annotations.ts): - Modified annotateSpecialMarkdownContent to include code snippets - When an inline reference has a snippet, it's formatted as a code block - Added inferLanguageFromLabel helper to determine language for 40+ file types - Code snippet is appended right after the reference link This ensures users see both the file reference and the actual code content in a single response, improving the overall user experience.
1 parent 248457d commit b2e4368

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

src/vs/workbench/contrib/chat/common/chatService/chatService.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ export interface IChatContentInlineReference {
161161
resolveId?: string;
162162
inlineReference: URI | Location | IWorkspaceSymbol;
163163
name?: string;
164+
/**
165+
* Optional code snippet content extracted from the referenced file/location.
166+
* When present, this content should be displayed alongside the reference link.
167+
*/
168+
snippet?: string;
169+
/**
170+
* Optional language identifier for syntax highlighting the code snippet.
171+
* If not provided, will be inferred from the file extension.
172+
*/
173+
languageId?: string;
164174
kind: 'inlineReference';
165175
}
166176

src/vs/workbench/contrib/chat/common/widget/annotations.ts

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,68 @@ import { IChatAgentVulnerabilityDetails } from '../chatService/chatService.js';
1212

1313
export const contentRefUrl = 'http://_vscodecontentref_'; // must be lowercase for URI
1414

15+
/**
16+
* Infers the language identifier from a file name or label.
17+
* Common language mappings based on file extensions.
18+
*/
19+
function inferLanguageFromLabel(label: string): string | undefined {
20+
if (!label) {
21+
return undefined;
22+
}
23+
24+
const ext = label.substring(label.lastIndexOf('.') + 1).toLowerCase();
25+
const languageMap: Record<string, string> = {
26+
// Programming languages
27+
'js': 'javascript',
28+
'jsx': 'javascript',
29+
'ts': 'typescript',
30+
'tsx': 'typescript',
31+
'py': 'python',
32+
'java': 'java',
33+
'c': 'c',
34+
'cpp': 'cpp',
35+
'cc': 'cpp',
36+
'cxx': 'cpp',
37+
'h': 'c',
38+
'hpp': 'cpp',
39+
'cs': 'csharp',
40+
'go': 'go',
41+
'rs': 'rust',
42+
'rb': 'ruby',
43+
'php': 'php',
44+
'swift': 'swift',
45+
'kt': 'kotlin',
46+
'scala': 'scala',
47+
'pl': 'perl',
48+
'sh': 'bash',
49+
'bash': 'bash',
50+
'zsh': 'bash',
51+
'fish': 'bash',
52+
'ps1': 'powershell',
53+
'psm1': 'powershell',
54+
'lua': 'lua',
55+
'r': 'r',
56+
'R': 'r',
57+
'sql': 'sql',
58+
'html': 'html',
59+
'htm': 'html',
60+
'xml': 'xml',
61+
'css': 'css',
62+
'scss': 'scss',
63+
'sass': 'sass',
64+
'less': 'less',
65+
'json': 'json',
66+
'jsonc': 'jsonc',
67+
'yaml': 'yaml',
68+
'yml': 'yaml',
69+
'toml': 'toml',
70+
'md': 'markdown',
71+
'markdown': 'markdown',
72+
};
73+
74+
return languageMap[ext];
75+
}
76+
1577
export function annotateSpecialMarkdownContent(response: Iterable<IChatProgressResponseContent>): IChatProgressRenderableResponseContent[] {
1678
let refIdPool = 0;
1779

@@ -46,7 +108,13 @@ export function annotateSpecialMarkdownContent(response: Iterable<IChatProgressR
46108
} else {
47109
const refId = refIdPool++;
48110
const printUri = URI.parse(contentRefUrl).with({ path: String(refId) });
49-
const markdownText = `[${label}](${printUri.toString()})`;
111+
let markdownText = `[${label}](${printUri.toString()})`;
112+
113+
// If the inline reference includes a code snippet, append it as a code block
114+
if (item.snippet) {
115+
const languageId = item.languageId || inferLanguageFromLabel(label) || '';
116+
markdownText += `\n\`\`\`${languageId}\n${item.snippet}\n\`\`\``;
117+
}
50118

51119
const annotationMetadata = { [refId]: item };
52120

0 commit comments

Comments
 (0)