Path: blob/main/extensions/markdown-language-features/src/util/openDocumentLink.ts
3292 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as vscode from 'vscode';6import { MdLanguageClient } from '../client/client';7import * as proto from '../client/protocol';89enum OpenMarkdownLinks {10beside = 'beside',11currentGroup = 'currentGroup',12}1314export class MdLinkOpener {1516constructor(17private readonly _client: MdLanguageClient,18) { }1920public async resolveDocumentLink(linkText: string, fromResource: vscode.Uri): Promise<proto.ResolvedDocumentLinkTarget> {21return this._client.resolveLinkTarget(linkText, fromResource);22}2324public async openDocumentLink(linkText: string, fromResource: vscode.Uri, viewColumn?: vscode.ViewColumn): Promise<void> {25const resolved = await this._client.resolveLinkTarget(linkText, fromResource);26if (!resolved) {27return;28}2930const uri = vscode.Uri.from(resolved.uri);31switch (resolved.kind) {32case 'external':33return vscode.commands.executeCommand('vscode.open', uri);3435case 'folder':36return vscode.commands.executeCommand('revealInExplorer', uri);3738case 'file': {39// If no explicit viewColumn is given, check if the editor is already open in a tab40if (typeof viewColumn === 'undefined') {41for (const tab of vscode.window.tabGroups.all.flatMap(x => x.tabs)) {42if (tab.input instanceof vscode.TabInputText) {43if (tab.input.uri.fsPath === uri.fsPath) {44viewColumn = tab.group.viewColumn;45break;46}47}48}49}5051return vscode.commands.executeCommand('vscode.open', uri, {52selection: resolved.position ? new vscode.Range(resolved.position.line, resolved.position.character, resolved.position.line, resolved.position.character) : undefined,53viewColumn: viewColumn ?? getViewColumn(fromResource),54} satisfies vscode.TextDocumentShowOptions);55}56}57}58}5960function getViewColumn(resource: vscode.Uri): vscode.ViewColumn {61const config = vscode.workspace.getConfiguration('markdown', resource);62const openLinks = config.get<OpenMarkdownLinks>('links.openLocation', OpenMarkdownLinks.currentGroup);63switch (openLinks) {64case OpenMarkdownLinks.beside:65return vscode.ViewColumn.Beside;66case OpenMarkdownLinks.currentGroup:67default:68return vscode.ViewColumn.Active;69}70}71727374