|
| 1 | +import type { Disposable, Event, TextDocumentContentProvider } from 'vscode'; |
| 2 | +import { EventEmitter, Uri, workspace } from 'vscode'; |
| 3 | +import { Schemes } from '../constants'; |
| 4 | +import type { GlCommands } from '../constants.commands'; |
| 5 | +import { decodeGitLensRevisionUriAuthority, encodeGitLensRevisionUriAuthority } from '../git/gitUri.authority'; |
| 6 | +import { createMarkdownCommandLink } from '../system/commands'; |
| 7 | + |
| 8 | +// gitlens-markdown:{explain}/{entity}/{entityID}/{model}[{/friendlyName}].md |
| 9 | + |
| 10 | +export interface MarkdownContentMetadata { |
| 11 | + header: { |
| 12 | + title: string; |
| 13 | + subtitle?: string; |
| 14 | + aiModel?: string; |
| 15 | + }; |
| 16 | + command?: { |
| 17 | + label: string; |
| 18 | + name: GlCommands; |
| 19 | + args?: Record<string, unknown>; |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +export class MarkdownContentProvider implements TextDocumentContentProvider { |
| 24 | + private contents = new Map<string, string>(); |
| 25 | + private registration: Disposable; |
| 26 | + |
| 27 | + private _onDidChange = new EventEmitter<Uri>(); |
| 28 | + get onDidChange(): Event<Uri> { |
| 29 | + return this._onDidChange.event; |
| 30 | + } |
| 31 | + |
| 32 | + constructor() { |
| 33 | + this.registration = workspace.registerTextDocumentContentProvider(Schemes.GitLensMarkdown, this); |
| 34 | + |
| 35 | + workspace.onDidCloseTextDocument(document => { |
| 36 | + if (document.uri.scheme === Schemes.GitLensMarkdown) { |
| 37 | + this.contents.delete(document.uri.toString()); |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + provideTextDocumentContent(uri: Uri): string | undefined { |
| 43 | + let contents = this.contents.get(uri.toString()); |
| 44 | + if (contents != null) return contents; |
| 45 | + |
| 46 | + contents = getContentFromMarkdownUri(uri); |
| 47 | + if (contents != null) return contents; |
| 48 | + |
| 49 | + return `# ${uri.path}\n\nNo content available.`; |
| 50 | + } |
| 51 | + |
| 52 | + openDocument(content: string, path: string, label: string, metadata?: Record<string, unknown>): Uri { |
| 53 | + const uri = Uri.from({ |
| 54 | + scheme: Schemes.GitLensMarkdown, |
| 55 | + authority: metadata ? encodeGitLensRevisionUriAuthority(metadata) : undefined, |
| 56 | + path: `${path}.md`, |
| 57 | + query: JSON.stringify({ label: label }), |
| 58 | + }); |
| 59 | + this.contents.set(uri.toString(), content); |
| 60 | + return uri; |
| 61 | + } |
| 62 | + |
| 63 | + updateDocument(uri: Uri, content: string): void { |
| 64 | + this.contents.set(uri.toString(), content); |
| 65 | + this._onDidChange.fire(uri); |
| 66 | + } |
| 67 | + |
| 68 | + closeDocument(uri: Uri): void { |
| 69 | + this.contents.delete(uri.toString()); |
| 70 | + } |
| 71 | + |
| 72 | + dispose(): void { |
| 73 | + this.contents.clear(); |
| 74 | + this.registration.dispose(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function getContentFromMarkdownUri(uri: Uri): string | undefined { |
| 79 | + if (!uri.path.startsWith('/explain')) return undefined; |
| 80 | + |
| 81 | + const authority = uri.authority; |
| 82 | + if (authority == null || authority.length === 0) return undefined; |
| 83 | + |
| 84 | + const metadata = decodeGitLensRevisionUriAuthority<MarkdownContentMetadata>(authority); |
| 85 | + |
| 86 | + if (metadata.header == null) return undefined; |
| 87 | + |
| 88 | + let headerContent = `# ${metadata.header.title}`; |
| 89 | + if (metadata.header.aiModel != null) { |
| 90 | + headerContent += `\n\n> Generated by ${metadata.header.aiModel}`; |
| 91 | + } |
| 92 | + if (metadata.header.subtitle != null) { |
| 93 | + headerContent += `\n\n## ${metadata.header.subtitle}`; |
| 94 | + } |
| 95 | + |
| 96 | + if (metadata.command == null) return `${headerContent}\n\nNo content available.`; |
| 97 | + |
| 98 | + const commandContent = `\n\n\n\n[${metadata.command.label}](${createMarkdownCommandLink( |
| 99 | + metadata.command.name, |
| 100 | + metadata.command.args, |
| 101 | + )})`; |
| 102 | + |
| 103 | + return `${headerContent}\n\n${commandContent}`; |
| 104 | +} |
0 commit comments