diff --git a/extensions/markdown-language-features/src/features/preview.ts b/extensions/markdown-language-features/src/features/preview.ts index d9bcd0bb00d..fe3290eceea 100644 --- a/extensions/markdown-language-features/src/features/preview.ts +++ b/extensions/markdown-language-features/src/features/preview.ts @@ -381,22 +381,27 @@ export class MarkdownPreview extends Disposable { clearTimeout(this.throttleTimer); this.throttleTimer = undefined; - const document = await vscode.workspace.openTextDocument(resource); - if (!this.forceUpdate && this.currentVersion && this.currentVersion.resource.fsPath === resource.fsPath && this.currentVersion.version === document.version) { - if (this.line) { - this.updateForView(resource, this.line); + try { + const document = await vscode.workspace.openTextDocument(resource); + if (!this.forceUpdate && this.currentVersion && this.currentVersion.resource.fsPath === resource.fsPath && this.currentVersion.version === document.version) { + if (this.line) { + this.updateForView(resource, this.line); + } + return; } - return; - } - this.forceUpdate = false; + this.forceUpdate = false; - this.currentVersion = { resource, version: document.version }; - const content = await this._contentProvider.provideTextDocumentContent(document, this._previewConfigurations, this.line, this.state); - if (this._resource === resource) { - this.editor.title = MarkdownPreview.getPreviewTitle(this._resource, this._locked); - this.editor.iconPath = this.iconPath; - this.editor.webview.options = MarkdownPreview.getWebviewOptions(resource, this._contributionProvider.contributions); - this.editor.webview.html = content; + this.currentVersion = { resource, version: document.version }; + const content = await this._contentProvider.provideTextDocumentContent(document, this._previewConfigurations, this.line, this.state); + if (this._resource === resource) { + this.editor.title = MarkdownPreview.getPreviewTitle(this._resource, this._locked); + this.editor.iconPath = this.iconPath; + this.editor.webview.options = MarkdownPreview.getWebviewOptions(resource, this._contributionProvider.contributions); + this.editor.webview.html = content; + } + } + catch (e) { + await this.showFileNotFoundError(); } } @@ -456,7 +461,20 @@ export class MarkdownPreview extends Disposable { } } - vscode.workspace.openTextDocument(this._resource).then(vscode.window.showTextDocument); + try { + vscode.workspace.openTextDocument(this._resource).then(vscode.window.showTextDocument); + } + catch (e) { + await this.showFileNotFoundError(); + } + } + + private async showFileNotFoundError() { + const content = await this._contentProvider.provideFileNotFoundContent(this._resource); + this.editor.title = MarkdownPreview.getPreviewTitle(this._resource, this._locked); + this.editor.iconPath = this.iconPath; + this.editor.webview.options = MarkdownPreview.getWebviewOptions(this._resource, this._contributionProvider.contributions); + this.editor.webview.html = content; } private async onDidClickPreviewLink(path: string, fragment: string | undefined) { diff --git a/extensions/markdown-language-features/src/features/previewContentProvider.ts b/extensions/markdown-language-features/src/features/previewContentProvider.ts index 88e9cea04f9..71ad313d7fc 100644 --- a/extensions/markdown-language-features/src/features/previewContentProvider.ts +++ b/extensions/markdown-language-features/src/features/previewContentProvider.ts @@ -90,6 +90,18 @@ export class MarkdownContentProvider { `; } + public async provideFileNotFoundContent( + resource: vscode.Uri, + ): Promise { + const resourcePath = path.basename(resource.fsPath); + return ` + + + ${resourcePath} cannot be found. + + `; + } + private extensionResourcePath(mediaFile: string): string { return vscode.Uri.file(this.context.asAbsolutePath(path.join('media', mediaFile))) .with({ scheme: 'vscode-resource' })