|
| 1 | +import type { TextEditor, Uri } from 'vscode'; |
| 2 | +import { window } from 'vscode'; |
| 3 | +import { Schemes } from '../constants'; |
| 4 | +import type { MarkdownContentMetadata } from '../documents/markdown'; |
| 5 | +import { decodeGitLensRevisionUriAuthority } from '../git/gitUri.authority'; |
| 6 | +import { command, executeCommand } from '../system/-webview/command'; |
| 7 | +import { Logger } from '../system/logger'; |
| 8 | +import { ActiveEditorCommand } from './commandBase'; |
| 9 | +import { getCommandUri } from './commandBase.utils'; |
| 10 | + |
| 11 | +@command() |
| 12 | +export class RegenerateMarkdownDocumentCommand extends ActiveEditorCommand { |
| 13 | + constructor() { |
| 14 | + super('gitlens.regenerateMarkdownDocument'); |
| 15 | + } |
| 16 | + |
| 17 | + async execute(editor?: TextEditor, uri?: Uri): Promise<void> { |
| 18 | + uri = getCommandUri(uri, editor); |
| 19 | + if (uri == null) return; |
| 20 | + |
| 21 | + // Only work with gitlens-markdown scheme documents |
| 22 | + if (uri.scheme !== Schemes.GitLensMarkdown) { |
| 23 | + void window.showErrorMessage('This action can only be used on GitLens markdown documents.'); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // Extract the command from the authority |
| 28 | + const authority = uri.authority; |
| 29 | + if (authority == null || authority.length === 0) { |
| 30 | + void window.showErrorMessage('No regeneration command found for this document.'); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + try { |
| 35 | + const metadata = decodeGitLensRevisionUriAuthority<MarkdownContentMetadata>(authority); |
| 36 | + |
| 37 | + if (metadata.command == null) { |
| 38 | + void window.showErrorMessage('No regeneration command found for this document.'); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Execute the command that was encoded in the authority |
| 43 | + // The openDocument method in the regeneration command will automatically |
| 44 | + // detect content changes and fire the _onDidChange event to refresh the preview |
| 45 | + await executeCommand(metadata.command.name, metadata.command.args); |
| 46 | + } catch (ex) { |
| 47 | + Logger.error(ex, 'RegenerateMarkdownDocumentCommand'); |
| 48 | + void window.showErrorMessage('Failed to regenerate document. See output for more details.'); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments