Skip to content

Commit

Permalink
Use custom hovers for rendered chat markdown responses
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Jul 29, 2024
1 parent ebaf073 commit 927f728
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class ChatMarkdownDecorationsRenderer {
const uri = part instanceof ChatRequestDynamicVariablePart && part.data instanceof URI ?
part.data :
undefined;
const title = uri ? encodeURIComponent(this.labelService.getUriLabel(uri, { relative: true })) :
const title = uri ? this.labelService.getUriLabel(uri, { relative: true }) :
part instanceof ChatRequestSlashCommandPart ? part.slashCommand.detail :
part instanceof ChatRequestAgentSubcommandPart ? part.command.description :
part instanceof ChatRequestVariablePart ? (this.chatVariablesService.getVariable(part.variableName)?.description ?? '') :
Expand Down
25 changes: 24 additions & 1 deletion src/vs/workbench/contrib/chat/browser/chatMarkdownRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
*--------------------------------------------------------------------------------------------*/

import { MarkdownRenderOptions, MarkedOptions } from 'vs/base/browser/markdownRenderer';
import { getDefaultHoverDelegate } from 'vs/base/browser/ui/hover/hoverDelegateFactory';
import { IMarkdownString } from 'vs/base/common/htmlContent';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { IMarkdownRendererOptions, IMarkdownRenderResult, MarkdownRenderer } from 'vs/editor/browser/widget/markdownRenderer/browser/markdownRenderer';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { IHoverService } from 'vs/platform/hover/browser/hover';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { ITrustedDomainService } from 'vs/workbench/contrib/url/browser/trustedDomainService';

Expand Down Expand Up @@ -56,6 +59,7 @@ export class ChatMarkdownRenderer extends MarkdownRenderer {
@ILanguageService languageService: ILanguageService,
@IOpenerService openerService: IOpenerService,
@ITrustedDomainService private readonly trustedDomainService: ITrustedDomainService,
@IHoverService private readonly hoverService: IHoverService,
) {
super(options ?? {}, languageService, openerService);
}
Expand All @@ -79,6 +83,25 @@ export class ChatMarkdownRenderer extends MarkdownRenderer {
value: `<body>\n\n${markdown.value}</body>`,
}
: markdown;
return super.render(mdWithBody, options, markedOptions);
const result = super.render(mdWithBody, options, markedOptions);
return this.attachCustomHover(result);
}

private attachCustomHover(result: IMarkdownRenderResult): IMarkdownRenderResult {
const store = new DisposableStore();
result.element.querySelectorAll('a').forEach((element) => {
if (element.title) {
store.add(this.hoverService.setupManagedHover(getDefaultHoverDelegate('element'), element, element.title));
element.title = '';
}
});

return {
element: result.element,
dispose: () => {
result.dispose();
store.dispose();
}
};
}
}

0 comments on commit 927f728

Please sign in to comment.