From 2fe62e7dfec6fcff6e037e5fbc12082d43d98c0e Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Tue, 10 Sep 2019 16:30:49 -0700 Subject: [PATCH] Use the document that contains the md link as the resource for getting config, not the target document --- .../src/commands/openDocumentLink.ts | 31 ++++++++++++------- .../src/features/documentLinkProvider.ts | 4 +-- .../src/features/preview.ts | 2 +- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/extensions/markdown-language-features/src/commands/openDocumentLink.ts b/extensions/markdown-language-features/src/commands/openDocumentLink.ts index 90162dee432..ef173b60854 100644 --- a/extensions/markdown-language-features/src/commands/openDocumentLink.ts +++ b/extensions/markdown-language-features/src/commands/openDocumentLink.ts @@ -13,8 +13,9 @@ import { isMarkdownFile } from '../util/file'; export interface OpenDocumentLinkArgs { - path: string; - fragment: string; + readonly path: string; + readonly fragment: string; + readonly fromResource: any; } enum OpenMarkdownLinks { @@ -27,10 +28,15 @@ export class OpenDocumentLinkCommand implements Command { public readonly id = OpenDocumentLinkCommand.id; public static createCommandUri( + fromResource: vscode.Uri, path: string, - fragment: string + fragment: string, ): vscode.Uri { - return vscode.Uri.parse(`command:${OpenDocumentLinkCommand.id}?${encodeURIComponent(JSON.stringify({ path: encodeURIComponent(path), fragment }))}`); + return vscode.Uri.parse(`command:${OpenDocumentLinkCommand.id}?${encodeURIComponent(JSON.stringify({ + path: encodeURIComponent(path), + fragment, + fromResource: encodeURIComponent(fromResource.toString(true)), + }))}`); } public constructor( @@ -38,19 +44,21 @@ export class OpenDocumentLinkCommand implements Command { ) { } public execute(args: OpenDocumentLinkArgs) { - const p = decodeURIComponent(args.path); - return this.tryOpen(p, args).catch(() => { - if (p && extname(p) === '') { - return this.tryOpen(p + '.md', args); + const fromResource = vscode.Uri.parse(decodeURIComponent(args.fromResource)); + const targetPath = decodeURIComponent(args.path); + const column = this.getViewColumn(fromResource); + return this.tryOpen(targetPath, args, column).catch(() => { + if (targetPath && extname(targetPath) === '') { + return this.tryOpen(targetPath + '.md', args, column); } - const resource = vscode.Uri.file(p); + const targetResource = vscode.Uri.file(targetPath); return Promise.resolve(undefined) - .then(() => vscode.commands.executeCommand('vscode.open', resource)) + .then(() => vscode.commands.executeCommand('vscode.open', targetResource, column)) .then(() => undefined); }); } - private async tryOpen(path: string, args: OpenDocumentLinkArgs) { + private async tryOpen(path: string, args: OpenDocumentLinkArgs, column: vscode.ViewColumn) { const resource = vscode.Uri.file(path); if (vscode.window.activeTextEditor && isMarkdownFile(vscode.window.activeTextEditor.document)) { if (!path || vscode.window.activeTextEditor.document.uri.fsPath === resource.fsPath) { @@ -58,7 +66,6 @@ export class OpenDocumentLinkCommand implements Command { } } - const column = this.getViewColumn(resource); return vscode.workspace.openTextDocument(resource) .then(document => vscode.window.showTextDocument(document, column)) .then(editor => this.tryRevealLine(editor, args.fragment)); diff --git a/extensions/markdown-language-features/src/features/documentLinkProvider.ts b/extensions/markdown-language-features/src/features/documentLinkProvider.ts index c4344a632bd..23364c4d69b 100644 --- a/extensions/markdown-language-features/src/features/documentLinkProvider.ts +++ b/extensions/markdown-language-features/src/features/documentLinkProvider.ts @@ -38,7 +38,7 @@ function parseLink( } return { - uri: OpenDocumentLinkCommand.createCommandUri(resourcePath, tempUri.fragment), + uri: OpenDocumentLinkCommand.createCommandUri(document.uri, resourcePath, tempUri.fragment), tooltip: localize('documentLink.tooltip', 'Follow link') }; } @@ -183,4 +183,4 @@ export default class LinkProvider implements vscode.DocumentLinkProvider { } return out; } -} \ No newline at end of file +} diff --git a/extensions/markdown-language-features/src/features/preview.ts b/extensions/markdown-language-features/src/features/preview.ts index e3de041cf58..0ee7deafff2 100644 --- a/extensions/markdown-language-features/src/features/preview.ts +++ b/extensions/markdown-language-features/src/features/preview.ts @@ -540,7 +540,7 @@ export class MarkdownPreview extends Disposable { } } - vscode.commands.executeCommand('_markdown.openDocumentLink', { path, fragment }); + vscode.commands.executeCommand('_markdown.openDocumentLink', { path, fragment, fromResource: this.resource }); } private async onCacheImageSizes(imageInfo: { id: string, width: number, height: number }[]) {