Path: blob/main/src/vs/editor/test/common/modes/linkComputer.test.ts
3296 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*--------------------------------------------------------------------------------------------*/4import assert from 'assert';5import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';6import { ILink } from '../../../common/languages.js';7import { ILinkComputerTarget, computeLinks } from '../../../common/languages/linkComputer.js';89class SimpleLinkComputerTarget implements ILinkComputerTarget {1011constructor(private _lines: string[]) {12// Intentional Empty13}1415public getLineCount(): number {16return this._lines.length;17}1819public getLineContent(lineNumber: number): string {20return this._lines[lineNumber - 1];21}22}2324function myComputeLinks(lines: string[]): ILink[] {25const target = new SimpleLinkComputerTarget(lines);26return computeLinks(target);27}2829function assertLink(text: string, extractedLink: string): void {30let startColumn = 0,31endColumn = 0,32chr: string,33i = 0;3435for (i = 0; i < extractedLink.length; i++) {36chr = extractedLink.charAt(i);37if (chr !== ' ' && chr !== '\t') {38startColumn = i + 1;39break;40}41}4243for (i = extractedLink.length - 1; i >= 0; i--) {44chr = extractedLink.charAt(i);45if (chr !== ' ' && chr !== '\t') {46endColumn = i + 2;47break;48}49}5051const r = myComputeLinks([text]);52assert.deepStrictEqual(r, [{53range: {54startLineNumber: 1,55startColumn: startColumn,56endLineNumber: 1,57endColumn: endColumn58},59url: extractedLink.substring(startColumn - 1, endColumn - 1)60}]);61}6263suite('Editor Modes - Link Computer', () => {6465ensureNoDisposablesAreLeakedInTestSuite();6667test('Null model', () => {68const r = computeLinks(null);69assert.deepStrictEqual(r, []);70});7172test('Parsing', () => {7374assertLink(75'x = "http://foo.bar";',76' http://foo.bar '77);7879assertLink(80'x = (http://foo.bar);',81' http://foo.bar '82);8384assertLink(85'x = [http://foo.bar];',86' http://foo.bar '87);8889assertLink(90'x = \'http://foo.bar\';',91' http://foo.bar '92);9394assertLink(95'x = http://foo.bar ;',96' http://foo.bar '97);9899assertLink(100'x = <http://foo.bar>;',101' http://foo.bar '102);103104assertLink(105'x = {http://foo.bar};',106' http://foo.bar '107);108109assertLink(110'(see http://foo.bar)',111' http://foo.bar '112);113assertLink(114'[see http://foo.bar]',115' http://foo.bar '116);117assertLink(118'{see http://foo.bar}',119' http://foo.bar '120);121assertLink(122'<see http://foo.bar>',123' http://foo.bar '124);125assertLink(126'<url>http://mylink.com</url>',127' http://mylink.com '128);129assertLink(130'// Click here to learn more. https://go.microsoft.com/fwlink/?LinkID=513275&clcid=0x409',131' https://go.microsoft.com/fwlink/?LinkID=513275&clcid=0x409'132);133assertLink(134'// Click here to learn more. https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx',135' https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx'136);137assertLink(138'// https://github.com/projectkudu/kudu/blob/master/Kudu.Core/Scripts/selectNodeVersion.js',139' https://github.com/projectkudu/kudu/blob/master/Kudu.Core/Scripts/selectNodeVersion.js'140);141assertLink(142'<!-- !!! Do not remove !!! WebContentRef(link:https://go.microsoft.com/fwlink/?LinkId=166007, area:Admin, updated:2015, nextUpdate:2016, tags:SqlServer) !!! Do not remove !!! -->',143' https://go.microsoft.com/fwlink/?LinkId=166007 '144);145assertLink(146'For instructions, see https://go.microsoft.com/fwlink/?LinkId=166007.</value>',147' https://go.microsoft.com/fwlink/?LinkId=166007 '148);149assertLink(150'For instructions, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx.</value>',151' https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx '152);153assertLink(154'x = "https://en.wikipedia.org/wiki/Zürich";',155' https://en.wikipedia.org/wiki/Zürich '156);157assertLink(158'請參閱 http://go.microsoft.com/fwlink/?LinkId=761051。',159' http://go.microsoft.com/fwlink/?LinkId=761051 '160);161assertLink(162'(請參閱 http://go.microsoft.com/fwlink/?LinkId=761051)',163' http://go.microsoft.com/fwlink/?LinkId=761051 '164);165166assertLink(167'x = "file:///foo.bar";',168' file:///foo.bar '169);170assertLink(171'x = "file://c:/foo.bar";',172' file://c:/foo.bar '173);174175assertLink(176'x = "file://shares/foo.bar";',177' file://shares/foo.bar '178);179180assertLink(181'x = "file://shäres/foo.bar";',182' file://shäres/foo.bar '183);184assertLink(185'Some text, then http://www.bing.com.',186' http://www.bing.com '187);188assertLink(189'let url = `http://***/_api/web/lists/GetByTitle(\'Teambuildingaanvragen\')/items`;',190' http://***/_api/web/lists/GetByTitle(\'Teambuildingaanvragen\')/items '191);192});193194test('issue #7855', () => {195assertLink(196'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!',197' https://msdn.microsoft.com/en-us/library/windows/desktop/ms687414(v=vs.85).aspx '198);199});200201test('issue #62278: "Ctrl + click to follow link" for IPv6 URLs', () => {202assertLink(203'let x = "http://[::1]:5000/connect/token"',204' http://[::1]:5000/connect/token '205);206});207208test('issue #70254: bold links dont open in markdown file using editor mode with ctrl + click', () => {209assertLink(210'2. Navigate to **https://portal.azure.com**',211' https://portal.azure.com '212);213});214215test('issue #86358: URL wrong recognition pattern', () => {216assertLink(217'POST|https://portal.azure.com|2019-12-05|',218' https://portal.azure.com '219);220});221222test('issue #67022: Space as end of hyperlink isn\'t always good idea', () => {223assertLink(224'aa https://foo.bar/[this is foo site] aa',225' https://foo.bar/[this is foo site] '226);227});228229test('issue #100353: Link detection stops at &(double-byte)', () => {230assertLink(231'aa http://tree-mark.chips.jp/レーズン&ベリーミックス aa',232' http://tree-mark.chips.jp/レーズン&ベリーミックス '233);234});235236test('issue #121438: Link detection stops at【...】', () => {237assertLink(238'aa https://zh.wikipedia.org/wiki/【我推的孩子】 aa',239' https://zh.wikipedia.org/wiki/【我推的孩子】 '240);241});242243test('issue #121438: Link detection stops at《...》', () => {244assertLink(245'aa https://zh.wikipedia.org/wiki/《新青年》编辑部旧址 aa',246' https://zh.wikipedia.org/wiki/《新青年》编辑部旧址 '247);248});249250test('issue #121438: Link detection stops at “...”', () => {251assertLink(252'aa https://zh.wikipedia.org/wiki/“常凯申”误译事件 aa',253' https://zh.wikipedia.org/wiki/“常凯申”误译事件 '254);255});256257test('issue #150905: Colon after bare hyperlink is treated as its part', () => {258assertLink(259'https://site.web/page.html: blah blah blah',260'https://site.web/page.html '261);262});263264// Removed because of #156875265// test('issue #151631: Link parsing stoped where comments include a single quote ', () => {266// assertLink(267// `aa https://regexper.com/#%2F''%2F aa`,268// ` https://regexper.com/#%2F''%2F `,269// );270// });271272test('issue #156875: Links include quotes ', () => {273assertLink(274`"This file has been converted from https://github.com/jeff-hykin/better-c-syntax/blob/master/autogenerated/c.tmLanguage.json",`,275` https://github.com/jeff-hykin/better-c-syntax/blob/master/autogenerated/c.tmLanguage.json `,276);277});278279test('issue #225513: Cmd-Click doesn\'t work on JSDoc {@link URL|LinkText} format ', () => {280assertLink(281` * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers|Promise.withResolvers}`,282` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers `,283);284});285});286287288