Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/test/common/modes/linkComputer.test.ts
3296 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
import assert from 'assert';
6
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
7
import { ILink } from '../../../common/languages.js';
8
import { ILinkComputerTarget, computeLinks } from '../../../common/languages/linkComputer.js';
9
10
class SimpleLinkComputerTarget implements ILinkComputerTarget {
11
12
constructor(private _lines: string[]) {
13
// Intentional Empty
14
}
15
16
public getLineCount(): number {
17
return this._lines.length;
18
}
19
20
public getLineContent(lineNumber: number): string {
21
return this._lines[lineNumber - 1];
22
}
23
}
24
25
function myComputeLinks(lines: string[]): ILink[] {
26
const target = new SimpleLinkComputerTarget(lines);
27
return computeLinks(target);
28
}
29
30
function assertLink(text: string, extractedLink: string): void {
31
let startColumn = 0,
32
endColumn = 0,
33
chr: string,
34
i = 0;
35
36
for (i = 0; i < extractedLink.length; i++) {
37
chr = extractedLink.charAt(i);
38
if (chr !== ' ' && chr !== '\t') {
39
startColumn = i + 1;
40
break;
41
}
42
}
43
44
for (i = extractedLink.length - 1; i >= 0; i--) {
45
chr = extractedLink.charAt(i);
46
if (chr !== ' ' && chr !== '\t') {
47
endColumn = i + 2;
48
break;
49
}
50
}
51
52
const r = myComputeLinks([text]);
53
assert.deepStrictEqual(r, [{
54
range: {
55
startLineNumber: 1,
56
startColumn: startColumn,
57
endLineNumber: 1,
58
endColumn: endColumn
59
},
60
url: extractedLink.substring(startColumn - 1, endColumn - 1)
61
}]);
62
}
63
64
suite('Editor Modes - Link Computer', () => {
65
66
ensureNoDisposablesAreLeakedInTestSuite();
67
68
test('Null model', () => {
69
const r = computeLinks(null);
70
assert.deepStrictEqual(r, []);
71
});
72
73
test('Parsing', () => {
74
75
assertLink(
76
'x = "http://foo.bar";',
77
' http://foo.bar '
78
);
79
80
assertLink(
81
'x = (http://foo.bar);',
82
' http://foo.bar '
83
);
84
85
assertLink(
86
'x = [http://foo.bar];',
87
' http://foo.bar '
88
);
89
90
assertLink(
91
'x = \'http://foo.bar\';',
92
' http://foo.bar '
93
);
94
95
assertLink(
96
'x = http://foo.bar ;',
97
' http://foo.bar '
98
);
99
100
assertLink(
101
'x = <http://foo.bar>;',
102
' http://foo.bar '
103
);
104
105
assertLink(
106
'x = {http://foo.bar};',
107
' http://foo.bar '
108
);
109
110
assertLink(
111
'(see http://foo.bar)',
112
' http://foo.bar '
113
);
114
assertLink(
115
'[see http://foo.bar]',
116
' http://foo.bar '
117
);
118
assertLink(
119
'{see http://foo.bar}',
120
' http://foo.bar '
121
);
122
assertLink(
123
'<see http://foo.bar>',
124
' http://foo.bar '
125
);
126
assertLink(
127
'<url>http://mylink.com</url>',
128
' http://mylink.com '
129
);
130
assertLink(
131
'// Click here to learn more. https://go.microsoft.com/fwlink/?LinkID=513275&clcid=0x409',
132
' https://go.microsoft.com/fwlink/?LinkID=513275&clcid=0x409'
133
);
134
assertLink(
135
'// Click here to learn more. https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx',
136
' https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx'
137
);
138
assertLink(
139
'// https://github.com/projectkudu/kudu/blob/master/Kudu.Core/Scripts/selectNodeVersion.js',
140
' https://github.com/projectkudu/kudu/blob/master/Kudu.Core/Scripts/selectNodeVersion.js'
141
);
142
assertLink(
143
'<!-- !!! Do not remove !!! WebContentRef(link:https://go.microsoft.com/fwlink/?LinkId=166007, area:Admin, updated:2015, nextUpdate:2016, tags:SqlServer) !!! Do not remove !!! -->',
144
' https://go.microsoft.com/fwlink/?LinkId=166007 '
145
);
146
assertLink(
147
'For instructions, see https://go.microsoft.com/fwlink/?LinkId=166007.</value>',
148
' https://go.microsoft.com/fwlink/?LinkId=166007 '
149
);
150
assertLink(
151
'For instructions, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx.</value>',
152
' https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx '
153
);
154
assertLink(
155
'x = "https://en.wikipedia.org/wiki/Zürich";',
156
' https://en.wikipedia.org/wiki/Zürich '
157
);
158
assertLink(
159
'請參閱 http://go.microsoft.com/fwlink/?LinkId=761051。',
160
' http://go.microsoft.com/fwlink/?LinkId=761051 '
161
);
162
assertLink(
163
'(請參閱 http://go.microsoft.com/fwlink/?LinkId=761051)',
164
' http://go.microsoft.com/fwlink/?LinkId=761051 '
165
);
166
167
assertLink(
168
'x = "file:///foo.bar";',
169
' file:///foo.bar '
170
);
171
assertLink(
172
'x = "file://c:/foo.bar";',
173
' file://c:/foo.bar '
174
);
175
176
assertLink(
177
'x = "file://shares/foo.bar";',
178
' file://shares/foo.bar '
179
);
180
181
assertLink(
182
'x = "file://shäres/foo.bar";',
183
' file://shäres/foo.bar '
184
);
185
assertLink(
186
'Some text, then http://www.bing.com.',
187
' http://www.bing.com '
188
);
189
assertLink(
190
'let url = `http://***/_api/web/lists/GetByTitle(\'Teambuildingaanvragen\')/items`;',
191
' http://***/_api/web/lists/GetByTitle(\'Teambuildingaanvragen\')/items '
192
);
193
});
194
195
test('issue #7855', () => {
196
assertLink(
197
'7. At this point, ServiceMain has been called. There is no functionality presently in ServiceMain, but you can consult the [MSDN documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms687414(v=vs.85).aspx) to add functionality as desired!',
198
' https://msdn.microsoft.com/en-us/library/windows/desktop/ms687414(v=vs.85).aspx '
199
);
200
});
201
202
test('issue #62278: "Ctrl + click to follow link" for IPv6 URLs', () => {
203
assertLink(
204
'let x = "http://[::1]:5000/connect/token"',
205
' http://[::1]:5000/connect/token '
206
);
207
});
208
209
test('issue #70254: bold links dont open in markdown file using editor mode with ctrl + click', () => {
210
assertLink(
211
'2. Navigate to **https://portal.azure.com**',
212
' https://portal.azure.com '
213
);
214
});
215
216
test('issue #86358: URL wrong recognition pattern', () => {
217
assertLink(
218
'POST|https://portal.azure.com|2019-12-05|',
219
' https://portal.azure.com '
220
);
221
});
222
223
test('issue #67022: Space as end of hyperlink isn\'t always good idea', () => {
224
assertLink(
225
'aa https://foo.bar/[this is foo site] aa',
226
' https://foo.bar/[this is foo site] '
227
);
228
});
229
230
test('issue #100353: Link detection stops at &(double-byte)', () => {
231
assertLink(
232
'aa http://tree-mark.chips.jp/レーズン&ベリーミックス aa',
233
' http://tree-mark.chips.jp/レーズン&ベリーミックス '
234
);
235
});
236
237
test('issue #121438: Link detection stops at【...】', () => {
238
assertLink(
239
'aa https://zh.wikipedia.org/wiki/【我推的孩子】 aa',
240
' https://zh.wikipedia.org/wiki/【我推的孩子】 '
241
);
242
});
243
244
test('issue #121438: Link detection stops at《...》', () => {
245
assertLink(
246
'aa https://zh.wikipedia.org/wiki/《新青年》编辑部旧址 aa',
247
' https://zh.wikipedia.org/wiki/《新青年》编辑部旧址 '
248
);
249
});
250
251
test('issue #121438: Link detection stops at “...”', () => {
252
assertLink(
253
'aa https://zh.wikipedia.org/wiki/“常凯申”误译事件 aa',
254
' https://zh.wikipedia.org/wiki/“常凯申”误译事件 '
255
);
256
});
257
258
test('issue #150905: Colon after bare hyperlink is treated as its part', () => {
259
assertLink(
260
'https://site.web/page.html: blah blah blah',
261
'https://site.web/page.html '
262
);
263
});
264
265
// Removed because of #156875
266
// test('issue #151631: Link parsing stoped where comments include a single quote ', () => {
267
// assertLink(
268
// `aa https://regexper.com/#%2F''%2F aa`,
269
// ` https://regexper.com/#%2F''%2F `,
270
// );
271
// });
272
273
test('issue #156875: Links include quotes ', () => {
274
assertLink(
275
`"This file has been converted from https://github.com/jeff-hykin/better-c-syntax/blob/master/autogenerated/c.tmLanguage.json",`,
276
` https://github.com/jeff-hykin/better-c-syntax/blob/master/autogenerated/c.tmLanguage.json `,
277
);
278
});
279
280
test('issue #225513: Cmd-Click doesn\'t work on JSDoc {@link URL|LinkText} format ', () => {
281
assertLink(
282
` * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers|Promise.withResolvers}`,
283
` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers `,
284
);
285
});
286
});
287
288