Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: render keybinding label for chat /help command link #199831

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/vs/workbench/contrib/chat/browser/chatListRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { FileKind } from 'vs/platform/files/common/files';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { WorkbenchCompressibleAsyncDataTree, WorkbenchList } from 'vs/platform/list/browser/listService';
import { ILogService } from 'vs/platform/log/common/log';
import { IOpenerService } from 'vs/platform/opener/common/opener';
Expand Down Expand Up @@ -140,6 +141,7 @@ export class ChatListItemRenderer extends Disposable implements ITreeRenderer<Ch
@IEditorService private readonly editorService: IEditorService,
@IProductService productService: IProductService,
@IThemeService private readonly themeService: IThemeService,
@IKeybindingService private readonly keybindingService: IKeybindingService,
) {
super();
this.renderer = this.instantiationService.createInstance(MarkdownRenderer, {});
Expand Down Expand Up @@ -814,7 +816,18 @@ export class ChatListItemRenderer extends Disposable implements ITreeRenderer<Ch
const disposables = new DisposableStore();
let codeBlockIndex = 0;

markdown = new MarkdownString(markdown.value, {
// inject keybinding hints for command links
const value = markdown.value.replace(/\[([^\]]+)\]\(command:([^\)]+)\)/g, (match, label, command) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer doing this on the other side of the markdown renderer, like

export function walkTreeAndAnnotateReferenceLinks(element: HTMLElement): void {
, because then we could be sure that the command link was actually allowed and not stripped by the sanitizer, or that it was actually a link and not something in a codeblock. This is fine for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would hit this

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const keybinding = this.keybindingService.lookupKeybinding(command);
const keybindingLabel = keybinding?.getLabel();
if (keybindingLabel) {
// ideally we would use the keybindingLabel renderer but dompurify sanitizes the styling
return `${match} (\`${keybindingLabel}\`)`;
}
return match;
});

markdown = new MarkdownString(value, {
isTrusted: {
// Disable all other config options except isTrusted
enabledCommands: typeof markdown.isTrusted === 'object' ? markdown.isTrusted?.enabledCommands : [] ?? []
Expand Down