Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/markdown-language-features/src/test/documentLink.test.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 assert from 'assert';
7
import 'mocha';
8
import * as vscode from 'vscode';
9
import { joinLines } from './util';
10
11
const testFileA = workspaceFile('a.md');
12
13
const debug = false;
14
15
function debugLog(...args: any[]) {
16
if (debug) {
17
console.log(...args);
18
}
19
}
20
21
function workspaceFile(...segments: string[]) {
22
return vscode.Uri.joinPath(vscode.workspace.workspaceFolders![0].uri, ...segments);
23
}
24
25
async function getLinksForFile(file: vscode.Uri): Promise<vscode.DocumentLink[]> {
26
debugLog('getting links', file.toString(), Date.now());
27
const r = (await vscode.commands.executeCommand<vscode.DocumentLink[]>('vscode.executeLinkProvider', file, /*linkResolveCount*/ 100))!;
28
debugLog('got links', file.toString(), Date.now());
29
return r;
30
}
31
32
(vscode.env.uiKind === vscode.UIKind.Web ? suite.skip : suite)('Markdown Document links', () => {
33
34
setup(async () => {
35
// the tests make the assumption that link providers are already registered
36
await vscode.extensions.getExtension('vscode.markdown-language-features')!.activate();
37
});
38
39
teardown(async () => {
40
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
41
});
42
43
test('Should navigate to markdown file', async () => {
44
await withFileContents(testFileA, '[b](b.md)');
45
46
const [link] = await getLinksForFile(testFileA);
47
await executeLink(link);
48
49
assertActiveDocumentUri(workspaceFile('b.md'));
50
});
51
52
test('Should navigate to markdown file with leading ./', async () => {
53
await withFileContents(testFileA, '[b](./b.md)');
54
55
const [link] = await getLinksForFile(testFileA);
56
await executeLink(link);
57
58
assertActiveDocumentUri(workspaceFile('b.md'));
59
});
60
61
test('Should navigate to markdown file with leading /', async () => {
62
await withFileContents(testFileA, '[b](./b.md)');
63
64
const [link] = await getLinksForFile(testFileA);
65
await executeLink(link);
66
67
assertActiveDocumentUri(workspaceFile('b.md'));
68
});
69
70
test('Should navigate to markdown file without file extension', async () => {
71
await withFileContents(testFileA, '[b](b)');
72
73
const [link] = await getLinksForFile(testFileA);
74
await executeLink(link);
75
76
assertActiveDocumentUri(workspaceFile('b.md'));
77
});
78
79
test('Should navigate to markdown file in directory', async () => {
80
await withFileContents(testFileA, '[b](sub/c)');
81
82
const [link] = await getLinksForFile(testFileA);
83
await executeLink(link);
84
85
assertActiveDocumentUri(workspaceFile('sub', 'c.md'));
86
});
87
88
test('Should navigate to fragment by title in file', async () => {
89
await withFileContents(testFileA, '[b](sub/c#second)');
90
91
const [link] = await getLinksForFile(testFileA);
92
await executeLink(link);
93
94
assertActiveDocumentUri(workspaceFile('sub', 'c.md'));
95
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 1);
96
});
97
98
test('Should navigate to fragment by line', async () => {
99
await withFileContents(testFileA, '[b](sub/c#L2)');
100
101
const [link] = await getLinksForFile(testFileA);
102
await executeLink(link);
103
104
assertActiveDocumentUri(workspaceFile('sub', 'c.md'));
105
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 1);
106
});
107
108
test('Should navigate to line number within non-md file', async () => {
109
await withFileContents(testFileA, '[b](sub/foo.txt#L3)');
110
111
const [link] = await getLinksForFile(testFileA);
112
await executeLink(link);
113
114
assertActiveDocumentUri(workspaceFile('sub', 'foo.txt'));
115
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 2);
116
});
117
118
test('Should navigate to fragment within current file', async () => {
119
await withFileContents(testFileA, joinLines(
120
'[](a#header)',
121
'[](#header)',
122
'# Header'));
123
124
const links = await getLinksForFile(testFileA);
125
{
126
await executeLink(links[0]);
127
assertActiveDocumentUri(workspaceFile('a.md'));
128
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 2);
129
}
130
{
131
await executeLink(links[1]);
132
assertActiveDocumentUri(workspaceFile('a.md'));
133
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 2);
134
}
135
});
136
137
test.skip('Should navigate to fragment within current untitled file', async () => { // TODO: skip for now for ls migration
138
const testFile = workspaceFile('x.md').with({ scheme: 'untitled' });
139
await withFileContents(testFile, joinLines(
140
'[](#second)',
141
'# Second'));
142
143
const [link] = await getLinksForFile(testFile);
144
await executeLink(link);
145
146
assertActiveDocumentUri(testFile);
147
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 1);
148
});
149
});
150
151
152
function assertActiveDocumentUri(expectedUri: vscode.Uri) {
153
assert.strictEqual(
154
vscode.window.activeTextEditor!.document.uri.fsPath,
155
expectedUri.fsPath
156
);
157
}
158
159
async function withFileContents(file: vscode.Uri, contents: string): Promise<void> {
160
debugLog('openTextDocument', file.toString(), Date.now());
161
const document = await vscode.workspace.openTextDocument(file);
162
debugLog('showTextDocument', file.toString(), Date.now());
163
const editor = await vscode.window.showTextDocument(document);
164
debugLog('editTextDocument', file.toString(), Date.now());
165
await editor.edit(edit => {
166
edit.replace(new vscode.Range(0, 0, 1000, 0), contents);
167
});
168
debugLog('opened done', vscode.window.activeTextEditor?.document.toString(), Date.now());
169
}
170
171
async function executeLink(link: vscode.DocumentLink) {
172
debugLog('executingLink', link.target?.toString(), Date.now());
173
174
await vscode.commands.executeCommand('vscode.open', link.target!);
175
debugLog('executedLink', vscode.window.activeTextEditor?.document.toString(), Date.now());
176
}
177
178