Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/markdown-language-features/src/util/openDocumentLink.ts
3292 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as vscode from 'vscode';
7
import { MdLanguageClient } from '../client/client';
8
import * as proto from '../client/protocol';
9
10
enum OpenMarkdownLinks {
11
beside = 'beside',
12
currentGroup = 'currentGroup',
13
}
14
15
export class MdLinkOpener {
16
17
constructor(
18
private readonly _client: MdLanguageClient,
19
) { }
20
21
public async resolveDocumentLink(linkText: string, fromResource: vscode.Uri): Promise<proto.ResolvedDocumentLinkTarget> {
22
return this._client.resolveLinkTarget(linkText, fromResource);
23
}
24
25
public async openDocumentLink(linkText: string, fromResource: vscode.Uri, viewColumn?: vscode.ViewColumn): Promise<void> {
26
const resolved = await this._client.resolveLinkTarget(linkText, fromResource);
27
if (!resolved) {
28
return;
29
}
30
31
const uri = vscode.Uri.from(resolved.uri);
32
switch (resolved.kind) {
33
case 'external':
34
return vscode.commands.executeCommand('vscode.open', uri);
35
36
case 'folder':
37
return vscode.commands.executeCommand('revealInExplorer', uri);
38
39
case 'file': {
40
// If no explicit viewColumn is given, check if the editor is already open in a tab
41
if (typeof viewColumn === 'undefined') {
42
for (const tab of vscode.window.tabGroups.all.flatMap(x => x.tabs)) {
43
if (tab.input instanceof vscode.TabInputText) {
44
if (tab.input.uri.fsPath === uri.fsPath) {
45
viewColumn = tab.group.viewColumn;
46
break;
47
}
48
}
49
}
50
}
51
52
return vscode.commands.executeCommand('vscode.open', uri, {
53
selection: resolved.position ? new vscode.Range(resolved.position.line, resolved.position.character, resolved.position.line, resolved.position.character) : undefined,
54
viewColumn: viewColumn ?? getViewColumn(fromResource),
55
} satisfies vscode.TextDocumentShowOptions);
56
}
57
}
58
}
59
}
60
61
function getViewColumn(resource: vscode.Uri): vscode.ViewColumn {
62
const config = vscode.workspace.getConfiguration('markdown', resource);
63
const openLinks = config.get<OpenMarkdownLinks>('links.openLocation', OpenMarkdownLinks.currentGroup);
64
switch (openLinks) {
65
case OpenMarkdownLinks.beside:
66
return vscode.ViewColumn.Beside;
67
case OpenMarkdownLinks.currentGroup:
68
default:
69
return vscode.ViewColumn.Active;
70
}
71
}
72
73
74