robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
28.0 KiB567 linesraw
1-- Test for scenarios involving 'spell'
2
3local t = require('test.testutil')
4local n = require('test.functional.testnvim')()
5local Screen = require('test.functional.ui.screen')
6
7local describe, it, before_each, pending = t.describe, t.it, t.before_each, t.pending
8local clear = n.clear
9local exec = n.exec
10local feed = n.feed
11local insert = n.insert
12local api = n.api
13local is_os = t.is_os
14
15describe("'spell'", function()
16  local screen
17
18  before_each(function()
19    clear()
20    screen = Screen.new(80, 8)
21    screen:add_extra_attr_ids {
22      [100] = { special = Screen.colors.Red, undercurl = true },
23      [101] = { special = Screen.colors.Blue, undercurl = true },
24      [102] = { foreground = Screen.colors.Blue, special = Screen.colors.Red, undercurl = true },
25      [103] = { foreground = tonumber('0x6a0dad') },
26      [104] = { foreground = Screen.colors.Red, underline = true },
27      [105] = { foreground = Screen.colors.Blue, underline = true },
28    }
29  end)
30
31  it('joins long lines #7937', function()
32    if is_os('openbsd') then
33      pending('FIXME #12104', function() end)
34      return
35    end
36    exec('set spell')
37    insert([[
38    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
39    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
40    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
41    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
42    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
43    non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
44    ]])
45    feed('ggJJJJJJ0')
46    screen:expect([[
47    {100:^Lorem} {100:ipsum} dolor sit {100:amet}, {100:consectetur} {100:adipiscing} {100:elit}, {100:sed} do {100:eiusmod} {100:tempor} {100:i}|
48    {100:ncididunt} {100:ut} {100:labore} et {100:dolore} {100:magna} {100:aliqua}. {100:Ut} {100:enim} ad minim {100:veniam}, {100:quis} {100:nostru}|
49    {100:d} {100:exercitation} {100:ullamco} {100:laboris} {100:nisi} {100:ut} {100:aliquip} ex ea {100:commodo} {100:consequat}. {100:Duis} {100:aut}|
50    {100:e} {100:irure} dolor in {100:reprehenderit} in {100:voluptate} {100:velit} {100:esse} {100:cillum} {100:dolore} {100:eu} {100:fugiat} {100:n}|
51    {100:ulla} {100:pariatur}. {100:Excepteur} {100:sint} {100:occaecat} {100:cupidatat} non {100:proident}, {100:sunt} in culpa {100:qui}|
52     {100:officia} {100:deserunt} {100:mollit} {100:anim} id est {100:laborum}.                                   |
53    {1:~                                                                               }|
54                                                                                    |
55    ]])
56  end)
57
58  -- oldtest: Test_spell_screendump()
59  it('has correct highlight at start of line', function()
60    exec([=[
61      call setline(1, [
62        \"This is some text without any spell errors.  Everything",
63        \"should just be black, nothing wrong here.",
64        \"",
65        \"This line has a sepll error. and missing caps.",
66        \"And and this is the the duplication.",
67        \"with missing caps here.",
68      \])
69      set spell spelllang=en_nz
70    ]=])
71    screen:expect([[
72      ^This is some text without any spell errors.  Everything                         |
73      should just be black, nothing wrong here.                                       |
74                                                                                      |
75      This line has a {100:sepll} error. {101:and} missing caps.                                  |
76      {100:And and} this is {100:the the} duplication.                                            |
77      {101:with} missing caps here.                                                         |
78      {1:~                                                                               }|
79                                                                                      |
80    ]])
81  end)
82
83  -- oldtest: Test_spell_screendump_spellcap()
84  it('SpellCap highlight at start of line', function()
85    exec([=[
86      call setline(1, [
87        \"   This line has a sepll error. and missing caps and trailing spaces.   ",
88        \"another missing cap here.",
89        \"",
90        \"and here.",
91        \"    ",
92        \"and here."
93      \])
94      set spell spelllang=en
95    ]=])
96    screen:expect([[
97      ^   This line has a {100:sepll} error. {101:and} missing caps and trailing spaces.           |
98      {101:another} missing cap here.                                                       |
99                                                                                      |
100      {101:and} here.                                                                       |
101                                                                                      |
102      {101:and} here.                                                                       |
103      {1:~                                                                               }|
104                                                                                      |
105    ]])
106    -- After adding word missing Cap in next line is updated
107    feed('3GANot<Esc>')
108    screen:expect([[
109         This line has a {100:sepll} error. {101:and} missing caps and trailing spaces.           |
110      {101:another} missing cap here.                                                       |
111      No^t                                                                             |
112      and here.                                                                       |
113                                                                                      |
114      {101:and} here.                                                                       |
115      {1:~                                                                               }|
116                                                                                      |
117    ]])
118    -- Deleting a full stop removes missing Cap in next line
119    feed('5Gdd<C-L>k$x')
120    screen:expect([[
121         This line has a {100:sepll} error. {101:and} missing caps and trailing spaces.           |
122      {101:another} missing cap here.                                                       |
123      Not                                                                             |
124      and her^e                                                                        |
125      and here.                                                                       |
126      {1:~                                                                               }|*2
127                                                                                      |
128    ]])
129    -- Undo also updates the next line (go to command line to remove message)
130    feed('u:<Esc>')
131    screen:expect([[
132         This line has a {100:sepll} error. {101:and} missing caps and trailing spaces.           |
133      {101:another} missing cap here.                                                       |
134      Not                                                                             |
135      and here^.                                                                       |
136      {101:and} here.                                                                       |
137      {1:~                                                                               }|*2
138                                                                                      |
139    ]])
140    -- Folding an empty line does not remove Cap in next line
141    feed('uzfk:<Esc>')
142    screen:expect([[
143         This line has a {100:sepll} error. {101:and} missing caps and trailing spaces.           |
144      {101:another} missing cap here.                                                       |
145      Not                                                                             |
146      {13:^+--  2 lines: and here.·························································}|
147      {101:and} here.                                                                       |
148      {1:~                                                                               }|*2
149                                                                                      |
150    ]])
151    -- Folding the end of a sentence does not remove Cap in next line
152    -- and editing a line does not remove Cap in current line
153    feed('Jzfkk$x')
154    screen:expect([[
155         This line has a {100:sepll} error. {101:and} missing caps and trailing spaces.           |
156      {101:another} missing cap her^e                                                        |
157      {13:+--  2 lines: Not·······························································}|
158      {101:and} here.                                                                       |
159      {1:~                                                                               }|*3
160                                                                                      |
161    ]])
162    -- Cap is correctly applied in the first row of a window
163    feed('<C-E><C-L>')
164    screen:expect([[
165      {101:another} missing cap her^e                                                        |
166      {13:+--  2 lines: Not·······························································}|
167      {101:and} here.                                                                       |
168      {1:~                                                                               }|*4
169                                                                                      |
170    ]])
171    -- Adding an empty line does not remove Cap in "mod_bot" area
172    feed('zbO<Esc>')
173    screen:expect([[
174         This line has a {100:sepll} error. {101:and} missing caps and trailing spaces.           |
175      ^                                                                                |
176      {101:another} missing cap here                                                        |
177      {13:+--  2 lines: Not·······························································}|
178      {101:and} here.                                                                       |
179      {1:~                                                                               }|*2
180                                                                                      |
181    ]])
182    -- Multiple empty lines does not remove Cap in the line after
183    feed('O<Esc><C-L>')
184    screen:expect([[
185         This line has a {100:sepll} error. {101:and} missing caps and trailing spaces.           |
186      ^                                                                                |
187                                                                                      |
188      {101:another} missing cap here                                                        |
189      {13:+--  2 lines: Not·······························································}|
190      {101:and} here.                                                                       |
191      {1:~                                                                               }|
192                                                                                      |
193    ]])
194  end)
195
196  -- oldtest: Test_spell_compatible()
197  it([[redraws properly when using "C" and "$" is in 'cpo']], function()
198    exec([=[
199      call setline(1, [
200        \ "test "->repeat(20),
201        \ "",
202        \ "end",
203      \ ])
204      set spell cpo+=$
205    ]=])
206    feed('51|C')
207    screen:expect([[
208      {101:test} test test test test test test test test test ^test test test test test test |
209      test test test test$                                                            |
210                                                                                      |
211      {101:end}                                                                             |
212      {1:~                                                                               }|*3
213      {5:-- INSERT --}                                                                    |
214    ]])
215    feed('x')
216    screen:expect([[
217      {101:test} test test test test test test test test test x^est test test test test test |
218      test test test test$                                                            |
219                                                                                      |
220      {101:end}                                                                             |
221      {1:~                                                                               }|*3
222      {5:-- INSERT --}                                                                    |
223    ]])
224  end)
225
226  it('extmarks, "noplainbuffer" and syntax #20385 #23398', function()
227    exec('set filetype=c')
228    exec('syntax on')
229    exec('set spell')
230    insert([[
231      #include <stdbool.h>
232      bool func(void);
233      // I am a speling mistakke]])
234    feed('ge')
235    screen:expect([[
236      {103:#include }{26:<stdbool.h>}                                                            |
237      {6:bool} func({6:void});                                                                |
238      {18:// I am a }{102:spelin^g}{18: }{102:mistakke}                                                      |
239      {1:~                                                                               }|*4
240                                                                                      |
241    ]])
242    feed(']s')
243    screen:expect([[
244      {103:#include }{26:<stdbool.h>}                                                            |
245      {6:bool} func({6:void});                                                                |
246      {18:// I am a }{102:speling}{18: }{102:^mistakke}                                                      |
247      {1:~                                                                               }|*4
248                                                                                      |
249    ]])
250    feed(']s')
251    screen:expect([[
252      {103:#include }{26:<stdbool.h>}                                                            |
253      {6:bool} func({6:void});                                                                |
254      {18:// I am a }{102:^speling}{18: }{102:mistakke}                                                      |
255      {1:~                                                                               }|*4
256      {19:search hit BOTTOM, continuing at TOP}                                            |
257    ]])
258    exec('echo ""')
259    local ns = api.nvim_create_namespace('spell')
260    -- extmark with spell=true enables spell
261    local id = api.nvim_buf_set_extmark(0, ns, 1, 4, { end_row = 1, end_col = 10, spell = true })
262    screen:expect([[
263      {103:#include }{26:<stdbool.h>}                                                            |
264      {6:bool} {100:func}({6:void});                                                                |
265      {18:// I am a }{102:^speling}{18: }{102:mistakke}                                                      |
266      {1:~                                                                               }|*4
267                                                                                      |
268    ]])
269    feed('[s')
270    screen:expect([[
271      {103:#include }{26:<stdbool.h>}                                                            |
272      {6:bool} {100:^func}({6:void});                                                                |
273      {18:// I am a }{102:speling}{18: }{102:mistakke}                                                      |
274      {1:~                                                                               }|*4
275                                                                                      |
276    ]])
277    api.nvim_buf_del_extmark(0, ns, id)
278    -- extmark with spell=false disables spell
279    id = api.nvim_buf_set_extmark(0, ns, 2, 18, { end_row = 2, end_col = 26, spell = false })
280    screen:expect([[
281      {103:#include }{26:<stdbool.h>}                                                            |
282      {6:bool} ^func({6:void});                                                                |
283      {18:// I am a }{102:speling}{18: mistakke}                                                      |
284      {1:~                                                                               }|*4
285                                                                                      |
286    ]])
287    feed('[s')
288    screen:expect([[
289      {103:#include }{26:<stdbool.h>}                                                            |
290      {6:bool} func({6:void});                                                                |
291      {18:// I am a }{102:^speling}{18: mistakke}                                                      |
292      {1:~                                                                               }|*4
293      {19:search hit TOP, continuing at BOTTOM}                                            |
294    ]])
295    exec('echo ""')
296    api.nvim_buf_del_extmark(0, ns, id)
297    screen:expect([[
298      {103:#include }{26:<stdbool.h>}                                                            |
299      {6:bool} func({6:void});                                                                |
300      {18:// I am a }{102:^speling}{18: }{102:mistakke}                                                      |
301      {1:~                                                                               }|*4
302                                                                                      |
303    ]])
304    feed(']s')
305    screen:expect([[
306      {103:#include }{26:<stdbool.h>}                                                            |
307      {6:bool} func({6:void});                                                                |
308      {18:// I am a }{102:speling}{18: }{102:^mistakke}                                                      |
309      {1:~                                                                               }|*4
310                                                                                      |
311    ]])
312    -- "noplainbuffer" shouldn't change spellchecking behavior with syntax enabled
313    exec('set spelloptions+=noplainbuffer')
314    screen:expect_unchanged()
315    feed('[s')
316    screen:expect([[
317      {103:#include }{26:<stdbool.h>}                                                            |
318      {6:bool} func({6:void});                                                                |
319      {18:// I am a }{102:^speling}{18: }{102:mistakke}                                                      |
320      {1:~                                                                               }|*4
321                                                                                      |
322    ]])
323    -- no spellchecking with "noplainbuffer" and syntax disabled
324    exec('syntax off')
325    screen:expect([[
326      #include <stdbool.h>                                                            |
327      bool func(void);                                                                |
328      // I am a ^speling mistakke                                                      |
329      {1:~                                                                               }|*4
330                                                                                      |
331    ]])
332    feed(']s')
333    screen:expect([[
334      #include <stdbool.h>                                                            |
335      bool func(void);                                                                |
336      // I am a ^speling mistakke                                                      |
337      {1:~                                                                               }|*4
338      {19:search hit BOTTOM, continuing at TOP}                                            |
339    ]])
340    exec('echo ""')
341    -- everything is spellchecked without "noplainbuffer" with syntax disabled
342    exec('set spelloptions&')
343    screen:expect([[
344      #include <{100:stdbool}.h>                                                            |
345      {100:bool} {100:func}(void);                                                                |
346      // I am a {100:^speling} {100:mistakke}                                                      |
347      {1:~                                                                               }|*4
348                                                                                      |
349    ]])
350    feed('[s')
351    screen:expect([[
352      #include <{100:stdbool}.h>                                                            |
353      {100:bool} {100:^func}(void);                                                                |
354      // I am a {100:speling} {100:mistakke}                                                      |
355      {1:~                                                                               }|*4
356                                                                                      |
357    ]])
358  end)
359
360  it('and syntax does not clear extmark highlighting at the start of a word', function()
361    screen:try_resize(43, 3)
362    exec([[
363      set spell
364      syntax match Constant "^.*$"
365      call setline(1, "This is some text without any spell errors.")
366    ]])
367    local ns = api.nvim_create_namespace('spell')
368    api.nvim_buf_set_extmark(0, ns, 0, 0, { hl_group = 'WarningMsg', end_col = 43 })
369    screen:expect([[
370      {19:^This is some text without any spell errors.}|
371      {1:~                                          }|
372                                                 |
373    ]])
374  end)
375
376  it('overrides syntax when Visual selection is active', function()
377    screen:try_resize(43, 3)
378    exec([[
379      hi! Comment guibg=NONE guifg=Blue gui=NONE guisp=NONE
380      hi! SpellBad guibg=NONE guifg=Red gui=NONE guisp=NONE
381      hi! Visual guibg=NONE guifg=NONE gui=underline guisp=NONE
382      syn match Comment "//.*"
383      call setline(1, '// Here is a misspeld word.')
384      set spell
385    ]])
386    screen:expect([[
387      {18:^// Here is a }{19:misspeld}{18: word.}                |
388      {1:~                                          }|
389                                                 |
390    ]])
391    feed('V')
392    screen:expect([[
393      {105:^// Here is a }{104:misspeld}{105: word.}                |
394      {1:~                                          }|
395      {5:-- VISUAL LINE --}                          |
396    ]])
397  end)
398
399  it("global value works properly for 'spelloptions'", function()
400    screen:try_resize(43, 3)
401    exec('set spell')
402    -- :setglobal applies to future buffers but not current buffer
403    exec('setglobal spelloptions=camel')
404    insert('Here is TheCamelWord being spellchecked')
405    screen:expect([[
406      Here is {100:TheCamelWord} being spellchecke^d    |
407      {1:~                                          }|
408                                                 |
409    ]])
410    exec('enew')
411    insert('There is TheCamelWord being spellchecked')
412    screen:expect([[
413      There is TheCamelWord being spellchecke^d   |
414      {1:~                                          }|
415                                                 |
416    ]])
417    -- :setlocal applies to current buffer but not future buffers
418    exec('setlocal spelloptions=')
419    screen:expect([[
420      There is {100:TheCamelWord} being spellchecke^d   |
421      {1:~                                          }|
422                                                 |
423    ]])
424    exec('enew')
425    insert('What is TheCamelWord being spellchecked')
426    screen:expect([[
427      What is TheCamelWord being spellchecke^d    |
428      {1:~                                          }|
429                                                 |
430    ]])
431    -- :set applies to both current buffer and future buffers
432    exec('set spelloptions=')
433    screen:expect([[
434      What is {100:TheCamelWord} being spellchecke^d    |
435      {1:~                                          }|
436                                                 |
437    ]])
438    exec('enew')
439    insert('Where is TheCamelWord being spellchecked')
440    screen:expect([[
441      Where is {100:TheCamelWord} being spellchecke^d   |
442      {1:~                                          }|
443                                                 |
444    ]])
445  end)
446
447  local function test_spell_false_nav(ephemeral)
448    screen:try_resize(50, 8)
449    insert('Splel\nSplle\nSepll\nSpele\nSpeel')
450    feed('gg0')
451    exec('set shortmess+=s spell spelloptions=noplainbuffer')
452
453    n.exec_lua(function()
454      local ns = vim.api.nvim_create_namespace('spell')
455      if ephemeral then
456        local decors = {} --- @type [integer,integer,vim.api.keyset.set_extmark][]
457        local function on_do()
458          for _, decor in ipairs(decors) do
459            vim.api.nvim_buf_set_extmark(
460              0,
461              ns,
462              decor[1],
463              decor[2],
464              vim.tbl_deep_extend('error', decor[3], { ephemeral = true })
465            )
466          end
467        end
468        vim.api.nvim_set_decoration_provider(ns, {
469          on_win = on_do,
470          _on_spell_nav = on_do,
471        })
472        function _G.Update(new_decors)
473          decors = new_decors
474          vim.cmd('redraw!')
475        end
476      else
477        --- @param decors [integer,integer,vim.api.keyset.set_extmark][]
478        function _G.Update(decors)
479          vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
480          for _, decor in ipairs(decors) do
481            vim.api.nvim_buf_set_extmark(0, ns, decor[1], decor[2], decor[3])
482          end
483          vim.cmd('redraw!')
484        end
485      end
486    end)
487
488    n.exec_lua(function()
489      _G.Update({
490        { 0, 0, { end_row = 5, spell = true, priority = 0 } },
491        { 2, 0, { end_row = 3, spell = false, priority = 1 } },
492      })
493    end)
494    screen:expect([[
495      {100:^Splel}                                             |
496      {100:Splle}                                             |
497      Sepll                                             |
498      {100:Spele}                                             |
499      {100:Speel}                                             |
500      {1:~                                                 }|*2
501                                                        |
502    ]])
503
504    t.eq({ 1, 0 }, api.nvim_win_get_cursor(0))
505    feed(']s')
506    t.eq({ 2, 0 }, api.nvim_win_get_cursor(0))
507    feed(']s')
508    t.eq({ 4, 0 }, api.nvim_win_get_cursor(0))
509    feed(']s')
510    t.eq({ 5, 0 }, api.nvim_win_get_cursor(0))
511    feed(']s')
512    t.eq({ 1, 0 }, api.nvim_win_get_cursor(0))
513    feed('[s')
514    t.eq({ 5, 0 }, api.nvim_win_get_cursor(0))
515    feed('[s')
516    t.eq({ 4, 0 }, api.nvim_win_get_cursor(0))
517    feed('[s')
518    t.eq({ 2, 0 }, api.nvim_win_get_cursor(0))
519    feed('[s')
520    t.eq({ 1, 0 }, api.nvim_win_get_cursor(0))
521
522    n.exec_lua(function()
523      _G.Update({
524        { 0, 0, { end_row = 5, spell = true, priority = 0 } },
525        { 3, 0, { end_row = 5, spell = false, priority = 1 } },
526        { 3, 0, { end_row = 4, spell = true, priority = 2 } },
527      })
528    end)
529    screen:expect([[
530      {100:^Splel}                                             |
531      {100:Splle}                                             |
532      {100:Sepll}                                             |
533      {100:Spele}                                             |
534      Speel                                             |
535      {1:~                                                 }|*2
536                                                        |
537    ]])
538
539    t.eq({ 1, 0 }, api.nvim_win_get_cursor(0))
540    feed(']s')
541    t.eq({ 2, 0 }, api.nvim_win_get_cursor(0))
542    feed(']s')
543    t.eq({ 3, 0 }, api.nvim_win_get_cursor(0))
544    feed(']s')
545    t.eq({ 4, 0 }, api.nvim_win_get_cursor(0))
546    feed(']s')
547    t.eq({ 1, 0 }, api.nvim_win_get_cursor(0))
548    feed('[s')
549    t.eq({ 4, 0 }, api.nvim_win_get_cursor(0))
550    feed('[s')
551    t.eq({ 3, 0 }, api.nvim_win_get_cursor(0))
552    feed('[s')
553    t.eq({ 2, 0 }, api.nvim_win_get_cursor(0))
554    feed('[s')
555    t.eq({ 1, 0 }, api.nvim_win_get_cursor(0))
556  end
557
558  describe('spell=false decoration on line with spelling mistake #39441', function()
559    it('using ephemeral decorations', function()
560      test_spell_false_nav(true)
561    end)
562
563    it('using non-ephemeral extmarks', function()
564      test_spell_false_nav(false)
565    end)
566  end)
567end)