Skip to content

Commit

Permalink
Treat commas as link terminators (#168752)
Browse files Browse the repository at this point in the history
Fixes #119696: Treat commas as link terminators
  • Loading branch information
alexdima authored Dec 11, 2022
1 parent 99ba576 commit f730897
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/vs/editor/common/languages/linkComputer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ function getClassifier(): CharacterClassifier<CharacterClass> {
_classifier = new CharacterClassifier<CharacterClass>(CharacterClass.None);

// allow-any-unicode-next-line
const FORCE_TERMINATION_CHARACTERS = ' \t<>\'\"、。。、,.:;‘〈「『〔([{「」}])〕』」〉’`~…';
const FORCE_TERMINATION_CHARACTERS = ', \t<>\'\"、。。、,.:;‘〈「『〔([{「」}])〕』」〉’`~…';
for (let i = 0; i < FORCE_TERMINATION_CHARACTERS.length; i++) {
_classifier.set(FORCE_TERMINATION_CHARACTERS.charCodeAt(i), CharacterClass.ForceTermination);
}

const CANNOT_END_WITH_CHARACTERS = '.,;:';
const CANNOT_END_WITH_CHARACTERS = '.;:';
for (let i = 0; i < CANNOT_END_WITH_CHARACTERS.length; i++) {
_classifier.set(CANNOT_END_WITH_CHARACTERS.charCodeAt(i), CharacterClass.CannotEndIn);
}
Expand Down
66 changes: 34 additions & 32 deletions src/vs/editor/test/common/modes/linkComputer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,29 @@ function myComputeLinks(lines: string[]): ILink[] {
return computeLinks(target);
}

function assertLink(text: string, extractedLink: string): void {
let startColumn = 0,
endColumn = 0,
chr: string,
i = 0;

for (i = 0; i < extractedLink.length; i++) {
chr = extractedLink.charAt(i);
if (chr !== ' ' && chr !== '\t') {
startColumn = i + 1;
break;
function extractLinks(text: string): string {
const keep: boolean[] = [];
const links = myComputeLinks([text]);
for (const link of links) {
const startChar = link.range.startColumn - 1;
const endChar = link.range.endColumn - 1;
for (let char = startChar; char < endChar; char++) {
keep[char] = true;
}
}

for (i = extractedLink.length - 1; i >= 0; i--) {
chr = extractedLink.charAt(i);
if (chr !== ' ' && chr !== '\t') {
endColumn = i + 2;
break;
const result: string[] = [];
for (let i = 0; i < text.length; i++) {
if (keep[i]) {
result.push(text.charAt(i));
} else {
result.push(' ');
}
}
return result.join('');
}

const r = myComputeLinks([text]);
assert.deepStrictEqual(r, [{
range: {
startLineNumber: 1,
startColumn: startColumn,
endLineNumber: 1,
endColumn: endColumn
},
url: extractedLink.substring(startColumn - 1, endColumn - 1)
}]);
function assertLink(text: string, expectedLinks: string): void {
assert.deepStrictEqual(extractLinks(text), expectedLinks);
}

suite('Editor Modes - Link Computer', () => {
Expand Down Expand Up @@ -106,19 +97,19 @@ suite('Editor Modes - Link Computer', () => {

assertLink(
'(see http://foo.bar)',
' http://foo.bar '
' http://foo.bar '
);
assertLink(
'[see http://foo.bar]',
' http://foo.bar '
' http://foo.bar '
);
assertLink(
'{see http://foo.bar}',
' http://foo.bar '
' http://foo.bar '
);
assertLink(
'<see http://foo.bar>',
' http://foo.bar '
' http://foo.bar '
);
assertLink(
'<url>http://mylink.com</url>',
Expand Down Expand Up @@ -199,7 +190,7 @@ suite('Editor Modes - Link Computer', () => {
test('issue #62278: "Ctrl + click to follow link" for IPv6 URLs', () => {
assertLink(
'let x = "http://[::1]:5000/connect/token"',
' http://[::1]:5000/connect/token '
' http://[::1]:5000/connect/token '
);
});

Expand Down Expand Up @@ -273,4 +264,15 @@ suite('Editor Modes - Link Computer', () => {
` https://github.com/jeff-hykin/better-c-syntax/blob/master/autogenerated/c.tmLanguage.json `,
);
});

test('issue #119696: Links shouldn\'t include commas', () => {
assertLink(
`https://apod.nasa.gov/apod/ap170720.html,IC 1396: Emission Nebula in Cepheus,https://apod.nasa.gov/apod/image/1707/MOSAIC_IC1396_HaSHO_blanco1024.jpg,https://apod.nasa.gov/apod/image/1707/MOSAIC_IC1396_HaSHO_blanco.jpg`,
`https://apod.nasa.gov/apod/ap170720.html https://apod.nasa.gov/apod/image/1707/MOSAIC_IC1396_HaSHO_blanco1024.jpg https://apod.nasa.gov/apod/image/1707/MOSAIC_IC1396_HaSHO_blanco.jpg`
);
assertLink(
`https://apod.nasa.gov/apod/ap180402.html,"Moons, Rings, Shadows, Clouds: Saturn (Cassini)",https://apod.nasa.gov/apod/image/1804/SaturnRingsMoons_Cassini_967.jpg,https://apod.nasa.gov/apod/image/1804/SaturnRingsMoons_Cassini_967.jpg`,
`https://apod.nasa.gov/apod/ap180402.html https://apod.nasa.gov/apod/image/1804/SaturnRingsMoons_Cassini_967.jpg https://apod.nasa.gov/apod/image/1804/SaturnRingsMoons_Cassini_967.jpg`,
);
});
});

0 comments on commit f730897

Please sign in to comment.