Skip to content

Commit e34baf6

Browse files
Only escape to   when needed (#309)
1 parent 05e4eed commit e34baf6

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/services/jsonHover.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ function toMarkdown(plain: string | undefined): string | undefined {
116116
return plain
117117
.trim()
118118
.replace(/[\\`*_{}[\]()<>#+\-.!]/g, '\\$&') // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
119-
.replace(/([ \t]+)/g, (_match, g1) => '&nbsp;'.repeat(g1.length)) // escape spaces tabs
119+
.replace(/(^ +)/mg, (_match, g1) => '&nbsp;'.repeat(g1.length)) // escape leading spaces on each line
120+
.replace(/( {2,})/g, (_match, g1) => ' ' + '&nbsp;'.repeat(g1.length - 1)) // escape consecutive spaces
121+
.replace(/(\t+)/g, (_match, g1) => '&nbsp;'.repeat(g1.length * 4)) // escape tabs
120122
.replace(/\n/g, '\\\n'); // escape new lines
121123
}
122124
return undefined;

src/test/hover.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ suite('JSON Hover', () => {
5757
}
5858
};
5959
await testComputeInfo(content, schema, { line: 0, character: 0 }).then((result) => {
60-
assert.deepEqual(result.contents, ['a&nbsp;very&nbsp;special&nbsp;object']);
60+
assert.deepEqual(result.contents, ['a very special object']);
6161
});
6262
await testComputeInfo(content, schema, { line: 0, character: 1 }).then((result) => {
6363
assert.deepEqual(result.contents, ['A']);
@@ -66,7 +66,7 @@ suite('JSON Hover', () => {
6666
assert.deepEqual(result.contents, ['C']);
6767
});
6868
await testComputeInfo(content, schema, { line: 0, character: 37 }).then((result) => {
69-
assert.deepEqual(result.contents, ['For&nbsp;example:\\\n\\\n\\<script\\>\\\n&nbsp;&nbsp;alert\\(1\\)\\\n\\</script\\>\\\n\\\n&nbsp;&nbsp;&nbsp;&nbsp;Test&nbsp;\\[1\\]']);
69+
assert.deepEqual(result.contents, ['For example:\\\n\\\n\\<script\\>\\\n&nbsp;&nbsp;alert\\(1\\)\\\n\\</script\\>\\\n\\\n&nbsp;&nbsp;&nbsp;&nbsp;Test \\[1\\]']);
7070
});
7171
await testComputeInfo(content, schema, { line: 0, character: 7 }).then((result) => {
7272
assert.deepEqual(result.contents, ['A']);
@@ -96,13 +96,13 @@ suite('JSON Hover', () => {
9696
}]
9797
};
9898
await testComputeInfo(content, schema, { line: 0, character: 0 }).then((result) => {
99-
assert.deepEqual(result.contents, ['a&nbsp;very&nbsp;special&nbsp;object']);
99+
assert.deepEqual(result.contents, ['a very special object']);
100100
});
101101
await testComputeInfo(content, schema, { line: 0, character: 1 }).then((result) => {
102102
assert.deepEqual(result.contents, ['A']);
103103
});
104104
await testComputeInfo(content, schema, { line: 0, character: 10 }).then((result) => {
105-
assert.deepEqual(result.contents, ['B\n\nIt\'s&nbsp;B']);
105+
assert.deepEqual(result.contents, ['B\n\nIt\'s B']);
106106
});
107107
});
108108

0 commit comments

Comments
 (0)