|
1 | 1 | import { expect, test } from 'rstack/test'; |
2 | 2 | import { TextDocument } from 'vscode-languageserver-textdocument'; |
3 | | -import { computeMinimalEdit } from '../../../src/fmt/lsp/minimalEdit.ts'; |
| 3 | +import { computeMinimalEdit, computeMinimalTextEdit } from '../../../src/fmt/lsp/minimalEdit.ts'; |
4 | 4 |
|
5 | 5 | /** Applies an edit the way an editor does, to prove it rewrites the document. */ |
6 | 6 | const applyMinimalEdit = (source: string, formatted: string): string => { |
@@ -165,12 +165,31 @@ test('addresses every combination of line terminators', () => { |
165 | 165 | if (applied !== formatted) { |
166 | 166 | failures.push(`${JSON.stringify(source)} -> ${JSON.stringify(formatted)}`); |
167 | 167 | } |
| 168 | + |
| 169 | + // The hand-rolled position mapping must agree with the reference |
| 170 | + // implementation on every terminator combination. |
| 171 | + const range = computeMinimalTextEdit(source, formatted)?.range; |
| 172 | + const expected = { |
| 173 | + start: document.positionAt(edit.start), |
| 174 | + end: document.positionAt(edit.end), |
| 175 | + }; |
| 176 | + if (JSON.stringify(range) !== JSON.stringify(expected)) { |
| 177 | + failures.push(`positions ${JSON.stringify(source)} -> ${JSON.stringify(formatted)}`); |
| 178 | + } |
168 | 179 | } |
169 | 180 | } |
170 | 181 |
|
171 | 182 | expect(failures).toEqual([]); |
172 | 183 | }); |
173 | 184 |
|
| 185 | +// The protocol counts a lone `\r` as a line break, so the mapping must too. |
| 186 | +test('maps positions across lone carriage returns', () => { |
| 187 | + expect(computeMinimalTextEdit('a\rb=2;\r', 'a\rb = 2;\r')).toEqual({ |
| 188 | + range: { start: { line: 1, character: 1 }, end: { line: 1, character: 2 } }, |
| 189 | + newText: ' = ', |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
174 | 193 | // U+2028 ends a line for JavaScript but never for the protocol. |
175 | 194 | test('treats a line separator as ordinary content', () => { |
176 | 195 | const source = 'const a = "\u2028";\nconst b=2;\n'; |
|
0 commit comments