aboutsummaryrefslogtreecommitdiff
path: root/test/functional/legacy/069_multibyte_formatting_spec.lua
blob: 24a4692e6c5ea8b8d932544e784293a9ab61a0a6 (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
-- Test for multibyte text formatting.
-- Also test, that 'mps' with multibyte chars works.
-- And test "ra" on multibyte characters.
-- Also test byteidx() and byteidxcomp()

local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local feed, insert, eq, eval, clear, feed_command, expect =
  n.feed, n.insert, t.eq, n.eval, n.clear, n.feed_command, n.expect

describe('multibyte text', function()
  before_each(clear)

  it('formatting with "set fo=t"', function()
    insert([[
      {
      XYZ
      abc XYZ
      }]])
    feed_command('/^{/+1')
    feed_command('set tw=2 fo=t')
    feed('gqgqjgqgqo<cr>')
    feed('XYZ<cr>')
    feed('abc XYZ<esc><esc>')
    expect([[
      {
      XYZ
      abc
      XYZ

      XYZ
      abc
      XYZ
      }]])
  end)

  it('formatting with "set fo=tm"', function()
    insert([[
      {

      Xa
      X a
      XY
      X Y
      }]])
    feed_command('/^{/+1')
    feed_command('set tw=1 fo=tm')
    feed('gqgqjgqgqjgqgqjgqgqjgqgqo<cr>')
    feed('X<cr>')
    feed('Xa<cr>')
    feed('X a<cr>')
    feed('XY<cr>')
    feed('X Y<esc><esc>')
    expect([[
      {


      a

      a







      a

      a




      }]])
  end)

  it('formatting with "set fo=tm" (part 2)', function()
    insert([[
      {

      Xa
      X a
      XY
      X Y
      aX
      abX
      abcX
      abX c
      abXY
      }]])
    feed_command('/^{/+1')
    feed_command('set tw=2 fo=tm')
    feed('gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqo<cr>')
    feed('X<cr>')
    feed('Xa<cr>')
    feed('X a<cr>')
    feed('XY<cr>')
    feed('X Y<cr>')
    feed('aX<cr>')
    feed('abX<cr>')
    feed('abcX<cr>')
    feed('abX c<cr>')
    feed('abXY<esc><esc>')
    expect([[
      {


      a

      a




      a

      ab

      abc

      ab

      c
      ab





      a

      a




      a

      ab

      abc

      ab

      c
      ab


      }]])
  end)

  it('formatting with "set ai fo=tm"', function()
    insert([[
      {

        Xa
      }]])
    feed_command('/^{/+1')
    feed_command('set ai tw=2 fo=tm')
    feed('gqgqjgqgqo<cr>')
    feed('X<cr>')
    feed('Xa<esc>')
    expect([[
      {


        a



        a
      }]])
  end)

  it('formatting with "set ai fo=tm" (part 2)', function()
    insert([[
      {

        Xa
      }]])
    feed_command('/^{/+1')
    feed_command('set noai tw=2 fo=tm')
    feed('gqgqjgqgqo<cr>')
    -- Literal spaces will be trimmed from the by feed().
    feed('<space><space>X<cr>')
    feed('<space><space>Xa<esc>')
    expect([[
      {


      a



      a
      }]])
  end)

  it('formatting with "set fo=cqm" and multibyte comments', function()
    insert([[
      {

      Xa
      XaY
      XY
      XYZ
      X Y
      X YZ
      XX
      XXa
      XXY
      }]])
    feed_command('/^{/+1')
    feed_command('set tw=2 fo=cqm comments=n:X')
    feed('gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqo<cr>')
    feed('X<cr>')
    feed('Xa<cr>')
    feed('XaY<cr>')
    feed('XY<cr>')
    feed('XYZ<cr>')
    feed('X Y<cr>')
    feed('X YZ<cr>')
    feed('XX<cr>')
    feed('XXa<cr>')
    feed('XXY<esc><esc>')
    expect([[
      {

      Xa
      Xa
      XY
      XY
      XY
      XZ
      X Y
      X Y
      X Z
      XX
      XXa
      XXY


      Xa
      Xa
      XY
      XY
      XY
      XZ
      X Y
      X Y
      X Z
      XX
      XXa
      XXY
      }]])
  end)

  it('formatting in replace mode', function()
    insert([[
      {

      }]])
    feed_command('/^{/+1')
    feed_command('set tw=2 fo=tm')
    feed('RXa<esc>')
    expect([[
      {

      a
      }]])
  end)

  it("as values of 'mps'", function()
    insert([[
      {
      ‘ two three ’ four
      }]])
    feed_command('/^{/+1')
    feed_command('set mps+=‘:’')
    feed('d%<cr>')
    expect([[
      {
       four
      }]])
  end)

  it('can be replaced with r', function()
    insert([[
      abba
      aab]])
    feed('gg0Vjra<cr>')
    expect([[
      aaaa
      aaa]])
  end)

  it("doesn't interfere with 'whichwrap'", function()
    insert([[
      á
      x]])
    feed_command('set whichwrap+=h')
    feed_command('/^x')
    feed('dh')
    expect([[
      áx]])
  end)

  it('can be queried with byteidx() and byteidxcomp()', function()
    -- One char of two bytes.
    feed_command("let a = '.é.'")
    -- Normal e with composing char.
    feed_command("let b = '.é.'")
    eq(0, eval('byteidx(a, 0)'))
    eq(1, eval('byteidx(a, 1)'))
    eq(3, eval('byteidx(a, 2)'))
    eq(4, eval('byteidx(a, 3)'))
    eq(-1, eval('byteidx(a, 4)'))
    eq(0, eval('byteidx(b, 0)'))
    eq(1, eval('byteidx(b, 1)'))
    eq(4, eval('byteidx(b, 2)'))
    eq(5, eval('byteidx(b, 3)'))
    eq(-1, eval('byteidx(b, 4)'))
    eq(0, eval('byteidxcomp(a, 0)'))
    eq(1, eval('byteidxcomp(a, 1)'))
    eq(3, eval('byteidxcomp(a, 2)'))
    eq(4, eval('byteidxcomp(a, 3)'))
    eq(-1, eval('byteidxcomp(a, 4)'))
    eq(0, eval('byteidxcomp(b, 0)'))
    eq(1, eval('byteidxcomp(b, 1)'))
    eq(2, eval('byteidxcomp(b, 2)'))
    eq(4, eval('byteidxcomp(b, 3)'))
    eq(5, eval('byteidxcomp(b, 4)'))
    eq(-1, eval('byteidxcomp(b, 5)'))
  end)

  it('correctly interact with the \zs pattern', function()
    eq('a1a2a3a', eval([[substitute('123', '\zs', 'a', 'g')]]))
  end)
end)