|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 | 6 | import * as dom from 'vs/base/browser/dom';
|
| 7 | +import { toErrorMessage } from 'vs/base/common/errorMessage'; |
7 | 8 | import { MarkdownString } from 'vs/base/common/htmlContent';
|
8 | 9 | import { revive } from 'vs/base/common/marshalling';
|
9 | 10 | import { basename } from 'vs/base/common/resources';
|
10 | 11 | import { URI } from 'vs/base/common/uri';
|
11 | 12 | import { IRange } from 'vs/editor/common/core/range';
|
12 | 13 | import { Location } from 'vs/editor/common/languages';
|
13 |
| -import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; |
14 | 14 | import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
|
15 | 15 | import { ILabelService } from 'vs/platform/label/common/label';
|
| 16 | +import { ILogService } from 'vs/platform/log/common/log'; |
16 | 17 | import { IChatProgressRenderableResponseContent, IChatProgressResponseContent } from 'vs/workbench/contrib/chat/common/chatModel';
|
17 | 18 | import { ChatRequestDynamicVariablePart, ChatRequestTextPart, IParsedChatRequest } from 'vs/workbench/contrib/chat/common/chatParserTypes';
|
18 | 19 | import { IChatAgentMarkdownContentWithVulnerability, IChatAgentVulnerabilityDetails, IChatContentInlineReference } from 'vs/workbench/contrib/chat/common/chatService';
|
19 | 20 |
|
20 | 21 | const variableRefUrl = 'http://_vscodedecoration_';
|
21 | 22 |
|
22 |
| -export function convertParsedRequestToMarkdown(accessor: ServicesAccessor, parsedRequest: IParsedChatRequest): string { |
23 |
| - let result = ''; |
24 |
| - for (const part of parsedRequest.parts) { |
25 |
| - if (part instanceof ChatRequestTextPart) { |
26 |
| - result += part.text; |
27 |
| - } else { |
28 |
| - const labelService = accessor.get(ILabelService); |
29 |
| - const uri = part instanceof ChatRequestDynamicVariablePart && part.data.map(d => d.value).find((d): d is URI => d instanceof URI) |
30 |
| - || undefined; |
31 |
| - const title = uri ? labelService.getUriLabel(uri, { relative: true }) : ''; |
| 23 | +export class ChatMarkdownDecorationsRenderer { |
| 24 | + constructor( |
| 25 | + @IKeybindingService private readonly keybindingService: IKeybindingService, |
| 26 | + @ILabelService private readonly labelService: ILabelService, |
| 27 | + @ILogService private readonly logService: ILogService |
| 28 | + ) { |
32 | 29 |
|
33 |
| - result += `[${part.text}](${variableRefUrl}${title})`; |
34 |
| - } |
35 | 30 | }
|
36 | 31 |
|
37 |
| - return result; |
38 |
| -} |
| 32 | + convertParsedRequestToMarkdown(parsedRequest: IParsedChatRequest): string { |
| 33 | + let result = ''; |
| 34 | + for (const part of parsedRequest.parts) { |
| 35 | + if (part instanceof ChatRequestTextPart) { |
| 36 | + result += part.text; |
| 37 | + } else { |
| 38 | + const uri = part instanceof ChatRequestDynamicVariablePart && part.data.map(d => d.value).find((d): d is URI => d instanceof URI) |
| 39 | + || undefined; |
| 40 | + const title = uri ? this.labelService.getUriLabel(uri, { relative: true }) : ''; |
39 | 41 |
|
40 |
| -export function walkTreeAndAnnotateReferenceLinks(accessor: ServicesAccessor, element: HTMLElement): void { |
41 |
| - const keybindingService = accessor.get(IKeybindingService); |
42 |
| - |
43 |
| - element.querySelectorAll('a').forEach(a => { |
44 |
| - const href = a.getAttribute('data-href'); |
45 |
| - if (href) { |
46 |
| - if (href.startsWith(variableRefUrl)) { |
47 |
| - const title = href.slice(variableRefUrl.length); |
48 |
| - a.parentElement!.replaceChild( |
49 |
| - renderResourceWidget(a.textContent!, title), |
50 |
| - a); |
51 |
| - } else if (href.startsWith(contentRefUrl)) { |
52 |
| - renderFileWidget(href, a); |
53 |
| - } else if (href.startsWith('command:')) { |
54 |
| - injectKeybindingHint(a, href, keybindingService); |
| 42 | + result += `[${part.text}](${variableRefUrl}${title})`; |
55 | 43 | }
|
56 | 44 | }
|
57 |
| - }); |
58 |
| -} |
59 | 45 |
|
60 |
| -function injectKeybindingHint(a: HTMLAnchorElement, href: string, keybindingService: IKeybindingService): void { |
61 |
| - const command = href.match(/command:([^\)]+)/)?.[1]; |
62 |
| - if (command) { |
63 |
| - const kb = keybindingService.lookupKeybinding(command); |
64 |
| - if (kb) { |
65 |
| - const keybinding = kb.getLabel(); |
66 |
| - if (keybinding) { |
67 |
| - a.textContent = `${a.textContent} (${keybinding})`; |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + walkTreeAndAnnotateReferenceLinks(element: HTMLElement): void { |
| 50 | + element.querySelectorAll('a').forEach(a => { |
| 51 | + const href = a.getAttribute('data-href'); |
| 52 | + if (href) { |
| 53 | + if (href.startsWith(variableRefUrl)) { |
| 54 | + const title = href.slice(variableRefUrl.length); |
| 55 | + a.parentElement!.replaceChild( |
| 56 | + this.renderResourceWidget(a.textContent!, title), |
| 57 | + a); |
| 58 | + } else if (href.startsWith(contentRefUrl)) { |
| 59 | + this.renderFileWidget(href, a); |
| 60 | + } else if (href.startsWith('command:')) { |
| 61 | + this.injectKeybindingHint(a, href, this.keybindingService); |
| 62 | + } |
68 | 63 | }
|
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + private renderFileWidget(href: string, a: HTMLAnchorElement): void { |
| 68 | + // TODO this can be a nicer FileLabel widget with an icon. Do a simple link for now. |
| 69 | + const fullUri = URI.parse(href); |
| 70 | + let location: Location | { uri: URI; range: undefined }; |
| 71 | + try { |
| 72 | + location = revive(JSON.parse(fullUri.fragment)); |
| 73 | + } catch (err) { |
| 74 | + this.logService.error('Invalid chat widget render data JSON', toErrorMessage(err)); |
| 75 | + return; |
69 | 76 | }
|
| 77 | + |
| 78 | + if (!location.uri || !URI.isUri(location.uri)) { |
| 79 | + this.logService.error(`Invalid chat widget render data: ${fullUri.fragment}`); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const fragment = location.range ? `${location.range.startLineNumber}-${location.range.endLineNumber}` : ''; |
| 84 | + a.setAttribute('data-href', location.uri.with({ fragment }).toString()); |
| 85 | + |
| 86 | + const label = this.labelService.getUriLabel(location.uri, { relative: true }); |
| 87 | + a.title = location.range ? |
| 88 | + `${label}#${location.range.startLineNumber}-${location.range.endLineNumber}` : |
| 89 | + label; |
70 | 90 | }
|
71 |
| -} |
72 | 91 |
|
73 |
| -function renderResourceWidget(name: string, title: string): HTMLElement { |
74 |
| - const container = dom.$('span.chat-resource-widget'); |
75 |
| - const alias = dom.$('span', undefined, name); |
76 |
| - alias.title = title; |
77 |
| - container.appendChild(alias); |
78 |
| - return container; |
79 |
| -} |
80 | 92 |
|
81 |
| -function renderFileWidget(href: string, a: HTMLAnchorElement): void { |
82 |
| - // TODO this can be a nicer FileLabel widget with an icon. Do a simple link for now. |
83 |
| - const fullUri = URI.parse(href); |
84 |
| - const location: Location | { uri: URI; range: undefined } = revive(JSON.parse(fullUri.fragment)); |
85 |
| - const fragment = location.range ? `${location.range.startLineNumber}-${location.range.endLineNumber}` : ''; |
86 |
| - a.setAttribute('data-href', location.uri.with({ fragment }).toString()); |
| 93 | + private renderResourceWidget(name: string, title: string): HTMLElement { |
| 94 | + const container = dom.$('span.chat-resource-widget'); |
| 95 | + const alias = dom.$('span', undefined, name); |
| 96 | + alias.title = title; |
| 97 | + container.appendChild(alias); |
| 98 | + return container; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + private injectKeybindingHint(a: HTMLAnchorElement, href: string, keybindingService: IKeybindingService): void { |
| 103 | + const command = href.match(/command:([^\)]+)/)?.[1]; |
| 104 | + if (command) { |
| 105 | + const kb = keybindingService.lookupKeybinding(command); |
| 106 | + if (kb) { |
| 107 | + const keybinding = kb.getLabel(); |
| 108 | + if (keybinding) { |
| 109 | + a.textContent = `${a.textContent} (${keybinding})`; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
87 | 114 | }
|
88 | 115 |
|
89 | 116 | export interface IMarkdownVulnerability {
|
|
0 commit comments