summaryrefslogtreecommitdiff
path: root/test/test_code_action.vader
blob: 80e2b1d8813d12bb7f714fb366eeeec5cc7253ce (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
Before:
  let g:notified_changes = []

  runtime autoload/ale/lsp.vim

  function! ale#lsp#NotifyForChanges(conn_id, buffer) abort
    call add(g:notified_changes, {
    \ 'conn_id': a:conn_id,
    \ 'buffer': a:buffer
    \})
  endfunction

  Save g:ale_enabled
  let g:ale_enabled = 0

  let g:file1 = tempname()
  let g:file2 = tempname()
  let g:test = {}

  let g:test.create_change = {line, offset, end_line, end_offset, value ->
  \{
  \   'changes': [{
  \      'fileName': g:file1,
  \      'textChanges': [{
  \        'start': {
  \          'line': line,
  \          'offset': offset,
  \        },
  \        'end': {
  \          'line': end_line,
  \          'offset': end_offset,
  \        },
  \        'newText': value,
  \      }],
  \   }]
  \}}

  function! WriteFileAndEdit() abort
    let g:test.text = [
    \ 'class Name {',
    \ '  value: string',
    \ '}',
    \]
    call writefile(g:test.text, g:file1, 'S')
    execute 'edit ' . g:file1
  endfunction!

After:
  " Close the extra buffers if we opened it.
  if bufnr(g:file1) != -1 && buflisted(bufnr(g:file1))
    execute ':bp! | :bd! ' . bufnr(g:file1)
  endif
  if bufnr(g:file2) != -1 && buflisted(bufnr(g:file2))
    execute ':bp! | :bd! ' . bufnr(g:file2)
  endif

  if filereadable(g:file1)
    call delete(g:file1)
  endif
  if filereadable(g:file2)
    call delete(g:file2)
  endif

  unlet! g:notified_changes
  " unlet! g:expected_notified_changes
  unlet! g:file1
  unlet! g:file2
  unlet! g:test
  unlet! g:changes
  delfunction WriteFileAndEdit

  runtime autoload/ale/lsp.vim

  Restore


Execute(It should modify and save multiple files):
  call writefile([
  \ 'class Name {',
  \ '  value: string',
  \ '}',
  \ '',
  \ 'class B {',
  \ '  constructor(readonly a: Name) {}',
  \ '}'
  \], g:file1, 'S')
  call writefile([
  \ 'import A from "A"',
  \ 'import {',
  \ '  B,',
  \ '  C,',
  \ '} from "module"',
  \ 'import D from "D"',
  \], g:file2, 'S')

  call ale#code_action#HandleCodeAction(
  \ {
  \   'changes': [{
  \      'fileName': g:file1,
  \      'textChanges': [{
  \        'start': {
  \          'line': 1,
  \          'offset': 7,
  \        },
  \        'end': {
  \          'line': 1,
  \          'offset': 11,
  \        },
  \        'newText': 'Value',
  \      }, {
  \        'start': {
  \          'line': 6,
  \          'offset': 27,
  \        },
  \        'end': {
  \          'line': 6,
  \          'offset': 31,
  \        },
  \        'newText': 'Value',
  \      }],
  \   }, {
  \     'fileName': g:file2,
  \     'textChanges': [{
  \       'start': {
  \         'line': 2,
  \         'offset': 1,
  \       },
  \       'end': {
  \         'line': 6,
  \         'offset': 1,
  \       },
  \       'newText': "import {A, B} from 'module'\n\n",
  \     }]
  \   }],
  \ },
  \ {'should_save': 1, 'conn_id': 'test_conn'},
  \)

  AssertEqual [
  \ 'class Value {',
  \ '  value: string',
  \ '}',
  \ '',
  \ 'class B {',
  \ '  constructor(readonly a: Value) {}',
  \ '}',
  \ '',
  \], readfile(g:file1, 'b')

  AssertEqual [
  \ 'import A from "A"',
  \ 'import {A, B} from ''module''',
  \ '',
  \ 'import D from "D"',
  \ '',
  \], readfile(g:file2, 'b')

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(g:file1),
  \}, {
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(g:file2),
  \}], g:notified_changes

Execute(Beginning of file can be modified):
  let g:test.text = [
  \ 'class Name {',
  \ '  value: string',
  \ '}',
  \]
  call writefile(g:test.text, g:file1, 'S')

  call ale#code_action#HandleCodeAction(
  \ {
  \   'changes': [{
  \      'fileName': g:file1,
  \      'textChanges': [{
  \        'start': {
  \          'line': 1,
  \          'offset': 1,
  \        },
  \        'end': {
  \          'line': 1,
  \          'offset': 1,
  \        },
  \        'newText': "type A: string\ntype B: number\n",
  \      }],
  \   }]
  \ },
  \ {'should_save': 1, 'conn_id': 'test_conn'},
  \)

  AssertEqual [
  \  'type A: string',
  \  'type B: number',
  \] + g:test.text + [''], readfile(g:file1, 'b')

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(g:file1),
  \}], g:notified_changes


Execute(End of file can be modified):
  let g:test.text = [
  \ 'class Name {',
  \ '  value: string',
  \ '}',
  \]
  call writefile(g:test.text, g:file1, 'S')

  call ale#code_action#HandleCodeAction(
  \ {
  \   'changes': [{
  \    'fileName': g:file1,
  \    'textChanges': [{
  \      'start': {
  \        'line': 4,
  \        'offset': 1,
  \      },
  \      'end': {
  \        'line': 4,
  \        'offset': 1,
  \      },
  \      'newText': "type A: string\ntype B: number\n",
  \     }],
  \   }]
  \ },
  \ {'should_save': 1, 'conn_id': 'test_conn'},
  \)

  AssertEqual g:test.text + [
  \  'type A: string',
  \  'type B: number',
  \  '',
  \], readfile(g:file1, 'b')

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(g:file1),
  \}], g:notified_changes


Execute(Current buffer contents will be reloaded):
  let g:test.text = [
  \ 'class Name {',
  \ '  value: string',
  \ '}',
  \]
  call writefile(g:test.text, g:file1, 'S')

  execute 'edit ' . g:file1
  let g:test.buffer = bufnr(g:file1)

  call ale#code_action#HandleCodeAction(
  \ {
  \   'changes': [{
  \      'fileName': g:file1,
  \      'textChanges': [{
  \        'start': {
  \          'line': 1,
  \          'offset': 1,
  \        },
  \        'end': {
  \          'line': 1,
  \          'offset': 1,
  \        },
  \        'newText': "type A: string\ntype B: number\n",
  \      }],
  \   }]
  \ },
  \ {'should_save': 1, 'conn_id': 'test_conn'},
  \)

  AssertEqual [
  \  'type A: string',
  \  'type B: number',
  \] + g:test.text + [''], readfile(g:file1, 'b')

  AssertEqual [
  \  'type A: string',
  \  'type B: number',
  \] + g:test.text, getbufline(g:test.buffer, 1, '$')

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(g:file1),
  \}], g:notified_changes


Execute(Unlisted buffer contents will be modified correctly):
  let g:test.text = [
  \ 'class Name {',
  \ '  value: string',
  \ '}',
  \]
  call writefile(g:test.text, g:file1, 'S')

  execute 'edit ' . g:file1
  let g:test.buffer = bufnr(g:file1)

  execute 'bd'
  AssertEqual bufnr(g:file1), g:test.buffer

  call ale#code_action#HandleCodeAction(
  \ {
  \   'changes': [{
  \      'fileName': g:file1,
  \      'textChanges': [{
  \        'start': {
  \          'line': 1,
  \          'offset': 1,
  \        },
  \        'end': {
  \          'line': 1,
  \          'offset': 1,
  \        },
  \        'newText': "type A: string\ntype B: number\n",
  \      }],
  \   }]
  \ },
  \ {'should_save': 1, 'conn_id': 'test_conn'},
  \)

  AssertEqual [
  \  'type A: string',
  \  'type B: number',
  \] + g:test.text + [''], readfile(g:file1, 'b')

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(g:file1),
  \}], g:notified_changes

# Tests for cursor repositioning. In comments `=` designates change range, and
# `C` cursor position

#     C ===
Execute(Cursor will not move when it is before text change):
  call WriteFileAndEdit()
  let g:test.changes = g:test.create_change(2, 3, 2, 8, 'value2')

  call setpos('.', [0, 1, 1, 0])
  call ale#code_action#HandleCodeAction(g:test.changes, {
  \ 'should_save': 1,
  \ 'conn_id': 'test_conn',
  \})
  AssertEqual [1, 1], getpos('.')[1:2]

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes

  call setpos('.', [0, 2, 2, 0])
  call ale#code_action#HandleCodeAction(g:test.changes, {
  \ 'should_save': 1,
  \ 'conn_id': 'test_conn',
  \})
  AssertEqual [2, 2], getpos('.')[1:2]

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}, {
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes

# ====C====
Execute(Cursor column will move to the change end when cursor between start/end):
  let g:test.changes = g:test.create_change(2, 3, 2, 8, 'value2')

  for r in range(3, 8)
    call WriteFileAndEdit()
    call setpos('.', [0, 2, r, 0])
    AssertEqual '  value: string', getline('.')
    call ale#code_action#HandleCodeAction(g:test.changes, {
    \ 'should_save': 1,
    \ 'conn_id': 'test_conn',
    \})
    AssertEqual '  value2: string', getline('.')
    AssertEqual [2, 9], getpos('.')[1:2]
  endfor

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}, {
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}, {
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}, {
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}, {
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}, {
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes


# ====C
Execute(Cursor column will move back when new text is shorter):
  call WriteFileAndEdit()
  call setpos('.', [0, 2, 8, 0])
  AssertEqual '  value: string', getline('.')
  call ale#code_action#HandleCodeAction(
  \ g:test.create_change(2, 3, 2, 8, 'val'),
  \ {
  \   'should_save': 1,
  \   'conn_id': 'test_conn',
  \ })
  AssertEqual '  val: string', getline('.')
  AssertEqual [2, 6], getpos('.')[1:2]

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes


# ==== C
Execute(Cursor column will move forward when new text is longer):
  call WriteFileAndEdit()

  call setpos('.', [0, 2, 8, 0])
  AssertEqual '  value: string', getline('.')
  call ale#code_action#HandleCodeAction(
  \ g:test.create_change(2, 3, 2, 8, 'longValue'),
  \ {
  \   'should_save': 1,
  \   'conn_id': 'test_conn',
  \ })
  AssertEqual '  longValue: string', getline('.')
  AssertEqual [2, 12], getpos('.')[1:2]

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes

# =========
# =
# C
Execute(Cursor line will move when updates are happening on lines above):
  call WriteFileAndEdit()
  call setpos('.', [0, 3, 1, 0])
  AssertEqual '}', getline('.')
  call ale#code_action#HandleCodeAction(
  \ g:test.create_change(1, 1, 2, 1, "test\ntest\n"),
  \ {
  \   'should_save': 1,
  \   'conn_id': 'test_conn',
  \ })
  AssertEqual '}', getline('.')
  AssertEqual [4, 1], getpos('.')[1:2]

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes


# =========
# =C
Execute(Cursor line and column will move when change on lines above and just before cursor column):
  call WriteFileAndEdit()
  call setpos('.', [0, 2, 2, 0])
  AssertEqual '  value: string', getline('.')
  call ale#code_action#HandleCodeAction(
  \ g:test.create_change(1, 1, 2, 1, "test\ntest\n123"),
  \ {
  \   'should_save': 1,
  \   'conn_id': 'test_conn',
  \ })
  AssertEqual '123  value: string', getline('.')
  AssertEqual [3, 5], getpos('.')[1:2]

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes

# =========
# ======C==
# =
Execute(Cursor line and column will move at the end of changes):
  call WriteFileAndEdit()
  call setpos('.', [0, 2, 10, 0])
  AssertEqual '  value: string', getline('.')
  call ale#code_action#HandleCodeAction(
  \ g:test.create_change(1, 1, 3, 1, "test\n"),
  \ {
  \   'should_save': 1,
  \   'conn_id': 'test_conn',
  \ })
  AssertEqual '}', getline('.')
  AssertEqual [2, 1], getpos('.')[1:2]

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes

#      C ==
# ===
Execute(Cursor will not move when changes happening on lines >= cursor, but after cursor):
  call WriteFileAndEdit()
  call setpos('.', [0, 2, 3, 0])
  AssertEqual '  value: string', getline('.')
  call ale#code_action#HandleCodeAction(
  \ g:test.create_change(2, 10, 3, 1, "number\n"),
  \ {
  \   'should_save': 1,
  \   'conn_id': 'test_conn',
  \ })
  AssertEqual '  value: number', getline('.')
  AssertEqual [2, 3], getpos('.')[1:2]

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes

Execute(Cursor will not move when change covers entire file):
  call WriteFileAndEdit()
  call setpos('.', [0, 2, 3, 0])
  call ale#code_action#HandleCodeAction(
  \ g:test.create_change(1, 1, len(g:test.text) + 1, 1,
  \   join(g:test.text + ['x'], "\n")),
  \ {
  \   'should_save': 1,
  \   'conn_id': 'test_conn',
  \ })
  AssertEqual [2, 3], getpos('.')[1:2]

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes

Execute(It should just modify file when should_save is set to v:false):
  call WriteFileAndEdit()
  let g:test.change = g:test.create_change(1, 1, 1, 1, "import { writeFile } from 'fs';\n")
  call ale#code_action#HandleCodeAction(g:test.change, {
  \ 'conn_id': 'test_conn',
  \})
  AssertEqual 1, getbufvar(bufnr(''), '&modified')
  AssertEqual [
  \ 'import { writeFile } from ''fs'';',
  \ 'class Name {',
  \ '  value: string',
  \ '}',
  \], getline(1, '$')

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes

Given typescript(An example TypeScript file):
  type Foo = {}

  export interface ISomething {
    fooLongName: Foo | null
  }

  export class SomethingElse implements ISomething {
    // Bindings
    fooLongName!: ISomething['fooLongName']
  }

Execute():
  let g:changes = [
  \ {'end': {'offset': 14, 'line': 4}, 'newText': 'foo', 'start': {'offset': 3, 'line': 4}},
  \ {'end': {'offset': 40, 'line': 9}, 'newText': 'foo', 'start': {'offset': 29, 'line': 9}},
  \ {'end': {'offset': 14, 'line': 9}, 'newText': 'foo', 'start': {'offset': 3, 'line': 9}},
  \]

  call ale#code_action#ApplyChanges(expand('%:p'), g:changes, {
  \ 'conn_id': 'test_conn',
  \})

  AssertEqual [{
  \ 'conn_id': 'test_conn',
  \ 'buffer': bufnr(''),
  \}], g:notified_changes

Expect(The changes should be applied correctly):
  type Foo = {}

  export interface ISomething {
    foo: Foo | null
  }

  export class SomethingElse implements ISomething {
    // Bindings
    foo!: ISomething['foo']
  }