-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoptimization.test.ts
More file actions
61 lines (52 loc) · 1.97 KB
/
optimization.test.ts
File metadata and controls
61 lines (52 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { CodeActionParams, Diagnostic, DiagnosticSeverity, Range } from 'vscode-languageserver';
import { CodeActionBuilder } from '../../builder';
import { onCodeAction } from './optimization';
function positionAt(text: string, offset: number) {
const lines = text.slice(0, offset).split('\n');
return { line: lines.length - 1, character: lines[lines.length - 1].length };
}
function rangeFromOffsets(text: string, startOffset: number, endOffset: number): Range {
return {
start: positionAt(text, startOffset),
end: positionAt(text, endOffset),
};
}
describe('Molang optimization code action', () => {
it('uses explicit offsets from diagnostic data for replacement range', () => {
const text = 'v.test=5*3*math.pi;';
const uri = 'file:///test.json';
const correctStart = text.indexOf('5*3*math.pi');
const correctEnd = correctStart + '5*3*math.pi'.length;
const badStart = text.indexOf('*math');
const badEnd = badStart + '*math'.length;
const diag: Diagnostic = {
range: rangeFromOffsets(text, badStart, badEnd),
message: 'Can rewrite the operation to...',
severity: DiagnosticSeverity.Information,
code: 'molang.optimization.constant-folding',
data: {
replacement: '15*math.pi',
startOffset: correctStart,
endOffset: correctEnd,
},
source: 'mc',
};
const params: CodeActionParams = {
textDocument: { uri },
range: diag.range,
context: { diagnostics: [diag] },
};
const context = {
document: {
uri,
positionAt: (offset: number) => positionAt(text, offset),
},
} as any;
const builder = new CodeActionBuilder(params, context);
onCodeAction(builder, diag);
expect(builder.out).toHaveLength(1);
const action = builder.out[0] as any;
expect(action.edit.changes[uri][0].range).toEqual(rangeFromOffsets(text, correctStart, correctEnd));
expect(action.edit.changes[uri][0].newText).toBe('15*math.pi');
});
});