aboutsummaryrefslogtreecommitdiff
path: root/test/functional/ui/bufhl_spec.lua
blob: ba3e44b6772e4c72d2b0327b53c07ca26e7edd3a (plain) (blame)
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
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')

local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert
local command, neq = helpers.command, helpers.neq
local curbufmeths = helpers.curbufmeths

describe('Buffer highlighting', function()
  local screen

  before_each(function()
    clear()
    command('syntax on')
    screen = Screen.new(40, 8)
    screen:attach()
    screen:set_default_attr_ids({
      [1] = {bold=true, foreground=Screen.colors.Blue},
      [2] = {foreground = Screen.colors.Fuchsia}, -- String
      [3] = {foreground = Screen.colors.Brown, bold = true}, -- Statement
      [4] = {foreground = Screen.colors.SlateBlue}, -- Special
      [5] = {bold = true, foreground = Screen.colors.SlateBlue},
      [6] = {foreground = Screen.colors.DarkCyan}, -- Identifier
      [7] = {bold = true},
      [8] = {underline = true, bold = true, foreground = Screen.colors.SlateBlue},
      [9] = {foreground = Screen.colors.SlateBlue, underline = true},
      [10] = {foreground = Screen.colors.Red},
      [11] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red},
      [12] = {foreground = Screen.colors.Blue1},
      [13] = {background = Screen.colors.LightGrey},
    })
  end)

  after_each(function()
    screen:detach()
  end)

  local add_hl = curbufmeths.add_highlight
  local clear_hl = curbufmeths.clear_highlight

  it('works', function()
    insert([[
      these are some lines
      with colorful text]])
    feed('+')

    screen:expect([[
      these are some lines                    |
      with colorful tex^t                      |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    add_hl(-1, "String", 0 , 10, 14)
    add_hl(-1, "Statement", 1 , 5, -1)

    screen:expect([[
      these are {2:some} lines                    |
      with {3:colorful tex^t}                      |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    feed("ggo<esc>")
    screen:expect([[
      these are {2:some} lines                    |
      ^                                        |
      with {3:colorful text}                      |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    clear_hl(-1, 0, -1)
    screen:expect([[
      these are some lines                    |
      ^                                        |
      with colorful text                      |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])
  end)

  describe('support adding multiple sources', function()
    local id1, id2
    before_each(function()
      insert([[
        a longer example
        in order to demonstrate
        combining highlights
        from different sources]])

      command("hi ImportantWord gui=bold cterm=bold")
      id1 = add_hl(0, "ImportantWord", 0, 2, 8)
      add_hl(id1, "ImportantWord", 1, 12, -1)
      add_hl(id1, "ImportantWord", 2, 0, 9)
      add_hl(id1, "ImportantWord", 3, 5, 14)

      -- add_highlight can be called like this to get a new source
      -- without adding any highlight
      id2 = add_hl(0, "", 0, 0, 0)
      neq(id1, id2)

      add_hl(id2, "Special", 0, 2, 8)
      add_hl(id2, "Identifier", 1, 3, 8)
      add_hl(id2, "Special", 1, 14, 20)
      add_hl(id2, "Underlined", 2, 6, 12)
      add_hl(id2, "Underlined", 3, 0, 9)

      screen:expect([[
        a {5:longer} example                        |
        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
        {7:combin}{8:ing}{9: hi}ghlights                    |
        {9:from }{8:diff}{7:erent} source^s                  |
        {1:~                                       }|
        {1:~                                       }|
        {1:~                                       }|
                                                |
      ]])
    end)

    it('and clearing the first added', function()
      clear_hl(id1, 0, -1)
      screen:expect([[
        a {4:longer} example                        |
        in {6:order} to de{4:monstr}ate                 |
        combin{9:ing hi}ghlights                    |
        {9:from diff}erent source^s                  |
        {1:~                                       }|
        {1:~                                       }|
        {1:~                                       }|
                                                |
      ]])
    end)

    it('and clearing the second added', function()
      clear_hl(id2, 0, -1)
      screen:expect([[
        a {7:longer} example                        |
        in order to {7:demonstrate}                 |
        {7:combining} highlights                    |
        from {7:different} source^s                  |
        {1:~                                       }|
        {1:~                                       }|
        {1:~                                       }|
                                                |
      ]])
    end)

    it('and clearing line ranges', function()
      clear_hl(-1, 0, 1)
      clear_hl(id1, 1, 2)
      clear_hl(id2, 2, -1)
      screen:expect([[
        a longer example                        |
        in {6:order} to de{4:monstr}ate                 |
        {7:combining} highlights                    |
        from {7:different} source^s                  |
        {1:~                                       }|
        {1:~                                       }|
        {1:~                                       }|
                                                |
      ]])
    end)

    it('and renumbering lines', function()
      feed('3Gddggo<esc>')
      screen:expect([[
        a {5:longer} example                        |
        ^                                        |
        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
        {9:from }{8:diff}{7:erent} sources                  |
        {1:~                                       }|
        {1:~                                       }|
        {1:~                                       }|
                                                |
      ]])

      command(':3move 4')
      screen:expect([[
        a {5:longer} example                        |
                                                |
        {9:from }{8:diff}{7:erent} sources                  |
        ^in {6:order} to {7:de}{5:monstr}{7:ate}                 |
        {1:~                                       }|
        {1:~                                       }|
        {1:~                                       }|
                                                |
      ]])
    end)
  end)

  it('prioritizes latest added highlight', function()
    insert([[
      three overlapping colors]])
    add_hl(0, "Identifier", 0, 6, 17)
    add_hl(0, "String", 0, 14, 23)
    local id = add_hl(0, "Special", 0, 0, 9)

    screen:expect([[
      {4:three ove}{6:rlapp}{2:ing color}^s                |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    clear_hl(id, 0, 1)
    screen:expect([[
      three {6:overlapp}{2:ing color}^s                |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])
  end)

  it('works with multibyte text', function()
    insert([[
      Ta båten över sjön!]])
    add_hl(-1, "Identifier", 0, 3, 9)
    add_hl(-1, "String", 0, 16, 21)

    screen:expect([[
      Ta {6:båten} över {2:sjön}^!                     |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])
  end)

  it('works with new syntax groups', function()
    insert([[
      fancy code in a new fancy language]])
    add_hl(-1, "FancyLangItem", 0, 0, 5)
    screen:expect([[
      fancy code in a new fancy languag^e      |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    command('hi FancyLangItem guifg=red')
    screen:expect([[
      {10:fancy} code in a new fancy languag^e      |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])
  end)

  it('supports virtual text annotations', function()
    local set_virtual_text = curbufmeths.set_virtual_text
    insert([[
      1 + 2
      3 +
      x = 4]])
    feed('O<esc>20A5, <esc>gg')
    screen:expect([[
      ^1 + 2                                   |
      3 +                                     |
      5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
      , 5, 5, 5, 5, 5, 5,                     |
      x = 4                                   |
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    local id1 = set_virtual_text(0, 0, {{"=", "Statement"}, {" 3", "Number"}}, {})
    set_virtual_text(id1, 1, {{"ERROR:", "ErrorMsg"}, {" invalid syntax"}}, {})
    local id2 = set_virtual_text(0, 2, {{"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."}}, {})
    neq(id2, id1)

    screen:expect([[
      ^1 + 2 {3:=}{2: 3}                               |
      3 + {11:ERROR:} invalid syntax               |
      5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
      , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
      x = 4                                   |
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    clear_hl(id1, 0, -1)
    screen:expect([[
      ^1 + 2                                   |
      3 +                                     |
      5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
      , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
      x = 4                                   |
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    -- Handles doublewidth chars, leaving a space if truncating
    -- in the middle of a char
    set_virtual_text(id1, 1, {{"暗x事zz速野谷質結育副住新覚丸活解終事", "Comment"}}, {})
    screen:expect([[
      ^1 + 2                                   |
      3 + {12:暗x事zz速野谷質結育副住新覚丸活解終 }|
      5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
      , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
      x = 4                                   |
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    feed("2Gx")
    screen:expect([[
      1 + 2                                   |
      ^ + {12:暗x事zz速野谷質結育副住新覚丸活解終事}|
      5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
      , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
      x = 4                                   |
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    -- visual selection doesn't highlight virtual text
    feed("ggVG")
    screen:expect([[
      {13:1 + 2}                                   |
      {13: +} {12:暗x事zz速野谷質結育副住新覚丸活解終事}|
      {13:5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}|
      {13:, 5, 5, 5, 5, 5, 5, } Lorem ipsum dolor s|
      ^x{13: = 4}                                   |
      {1:~                                       }|
      {1:~                                       }|
      {7:-- VISUAL LINE --}                       |
    ]])

    feed("<esc>")
    screen:expect([[
      1 + 2                                   |
       + {12:暗x事zz速野谷質結育副住新覚丸活解終事}|
      5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
      , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
      ^x = 4                                   |
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    feed("2Gdd")
    screen:expect([[
      1 + 2                                   |
      ^5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
      , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
      x = 4                                   |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    -- listchars=eol:- works, and doesn't shift virtual text
    command("set list")
    screen:expect([[
      1 + 2                                   |
      ^5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
      , 5, 5, 5, 5, 5, 5,{1:-} Lorem ipsum dolor s|
      x = 4                                   |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

    clear_hl(-1, 0, -1)
    screen:expect([[
      1 + 2                                   |
      ^5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
      , 5, 5, 5, 5, 5, 5,{1:-}                    |
      x = 4                                   |
      {1:~                                       }|
      {1:~                                       }|
      {1:~                                       }|
                                              |
    ]])

  end)
end)