@@ -270,6 +270,153 @@ function! s:numericHighlightGroupInSliceSlicing(testname, from, to)
270
270
endtry
271
271
endfunction
272
272
273
+ function ! Test_diagnostic_after_fmt () abort
274
+ let g: go_fmt_command = ' gofmt'
275
+ try
276
+ call s: diagnostic_after_write ( [
277
+ \ ' package main' ,
278
+ \ ' import "fmt"' ,
279
+ \ ' ' ,
280
+ \ ' func main() {' ,
281
+ \ ' ' ,
282
+ \ " \t fmt.Println(\x1f hello)" ,
283
+ \ ' }' ,
284
+ \ ], [])
285
+ finally
286
+ unlet g: go_fmt_command
287
+ endtry
288
+ endfunction
289
+
290
+ function ! Test_diagnostic_after_fmt_change () abort
291
+ " craft a file that will be changed when its written (gofmt will change it).
292
+ let g: go_fmt_command = ' gofmt'
293
+ try
294
+ call s: diagnostic_after_write ( [
295
+ \ ' package main' ,
296
+ \ ' import "fmt"' ,
297
+ \ ' ' ,
298
+ \ ' func main() {' ,
299
+ \ ' ' ,
300
+ \ " fmt.Println(\x1f hello)" ,
301
+ \ ' }' ,
302
+ \ ], [])
303
+ finally
304
+ unlet g: go_fmt_command
305
+ endtry
306
+ endfunction
307
+
308
+ function ! Test_diagnostic_after_fmt_cleared () abort
309
+ " craft a file that will be fixed when it is written.
310
+ let g: go_fmt_command = ' gofmt'
311
+ try
312
+ call s: diagnostic_after_write ( [
313
+ \ ' package main' ,
314
+ \ ' import "fmt"' ,
315
+ \ ' ' ,
316
+ \ ' func main() {' ,
317
+ \ ' ' ,
318
+ \ " fmt.Println(\x1f hello)" ,
319
+ \ ' }' ,
320
+ \ ], [' hello := "hello, vim-go"' ])
321
+ finally
322
+ unlet g: go_fmt_command
323
+ endtry
324
+ endfunction
325
+
326
+ function ! Test_diagnostic_after_reload () abort
327
+ let l: dir = gotest#write_file (' diagnostic/after-reload.go' , [
328
+ \ ' package main' ,
329
+ \ ' import "fmt"' ,
330
+ \ ' ' ,
331
+ \ ' func main() {' ,
332
+ \ ' ' ,
333
+ \ " \t fmt.Println(\x1f hello)" ,
334
+ \ ' }' ,
335
+ \ ])
336
+ try
337
+ call s: check_diagnostics (' ' , ' goDiagnosticError' , ' initial' )
338
+ let l: pos = getcurpos ()
339
+ edit
340
+ call setpos (' .' , l: pos )
341
+ call s: check_diagnostics (' ' , ' goDiagnosticError' , ' after-reload' )
342
+ finally
343
+ call delete (l: dir , ' rf' )
344
+ endtry
345
+ endfunction
346
+
347
+ function ! s: diagnostic_after_write (contents, changes ) abort
348
+ syntax on
349
+
350
+ let l: dir = gotest#write_file (' diagnostic/after-write.go' , a: contents )
351
+
352
+ try
353
+ let l: pos = getcurpos ()
354
+ call s: check_diagnostics (' ' , ' goDiagnosticError' , ' initial' )
355
+
356
+ " write a:changes to the previous line and make sure l:actual and
357
+ " l:expected are set so that they won't accidentally match on the next
358
+ " check.
359
+ if len (a: changes ) > 0
360
+ call append (l: pos [1 ]-1 , a: changes )
361
+ let l: actual = ' goDiagnosticError'
362
+ let l: expected = ' '
363
+ else
364
+ let l: actual = ' '
365
+ let l: expected = ' goDiagnosticError'
366
+ endif
367
+
368
+ write
369
+
370
+ call s: check_diagnostics (l: actual , l: expected , ' after-write' )
371
+ finally
372
+ call delete (l: dir , ' rf' )
373
+ endtry
374
+ endfunction
375
+
376
+ function ! s: check_diagnostics (actual, expected, when)
377
+ let l: actual = a: actual
378
+ let l: start = reltime ()
379
+
380
+ while l: actual != a: expected && reltimefloat (reltime (l: start )) < 10
381
+ " Get the cursor position on each iteration, because the cursor postion
382
+ " may change between iterations when go#fmt#GoFmt formats, reloads the
383
+ " file, and moves the cursor to try to keep it where the user expects it
384
+ " to be when gofmt modifies the files.
385
+ let l: pos = getcurpos ()
386
+ if ! has (' textprop' )
387
+ let l: matches = getmatches ()
388
+ if len (l: matches ) == 0
389
+ let l: actual = ' '
390
+ endif
391
+
392
+ for l: m in l: matches
393
+ let l: matchline = l: m .pos1[0 ]
394
+ if len (l: m .pos1) < 2
395
+ continue
396
+ endif
397
+ let l: matchcol = get (l: m .pos1, 1 , 1 )
398
+ if l: pos [1 ] == l: matchline && l: pos [2 ] >= l: matchcol && l: pos [2 ] <= l: matchcol + l: m .pos1[2 ]
399
+ " Ideally, we'd check that the cursor is within the match, but when a
400
+ " tab is added on the current line, the cursor position within the
401
+ " line will stay constant while the line itself is shifted over by a
402
+ " column, so just check the line itself instead of checking a precise
403
+ " cursor location.
404
+ " if l:pos[1] == l:matchline
405
+ let l: actual = l: m .group
406
+ break
407
+ endif
408
+ endfor
409
+
410
+ sleep 100 m
411
+ continue
412
+ endif
413
+
414
+ let l: actual = get (prop_list (l: pos [1 ]), 0 , {' type' : ' ' }).type
415
+ sleep 100 m
416
+ endwhile
417
+
418
+ call assert_equal (a: expected , l: actual , a: when )
419
+ endfunction
273
420
" restore Vi compatibility settings
274
421
let &cpo = s: cpo_save
275
422
unlet s: cpo_save
0 commit comments