1" Test for breakindent
2"
3" Note: if you get strange failures when adding new tests, it might be that
4" while the test is run, the breakindent cacheing gets in its way.
5" It helps to change the tabstop setting and force a redraw (e.g. see
6" Test_breakindent08())
7if !exists('+breakindent')
8  finish
9endif
10
11source view_util.vim
12
13let s:input ="\tabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP"
14
15function s:screen_lines(lnum, width) abort
16  return ScreenLines([a:lnum, a:lnum + 2], a:width)
17endfunction
18
19function! s:compare_lines(expect, actual)
20  call assert_equal(join(a:expect, "\n"), join(a:actual, "\n"))
21endfunction
22
23function s:test_windows(...)
24  call NewWindow(10, 20)
25  setl ts=4 sw=4 sts=4 breakindent
26  put =s:input
27  exe get(a:000, 0, '')
28endfunction
29
30function s:close_windows(...)
31  call CloseWindow()
32  exe get(a:000, 0, '')
33endfunction
34
35function Test_breakindent01()
36  " simple breakindent test
37  call s:test_windows('setl briopt=min:0')
38  let lines=s:screen_lines(line('.'),8)
39  let expect=[
40\ "    abcd",
41\ "    qrst",
42\ "    GHIJ",
43\ ]
44  call s:compare_lines(expect, lines)
45  call s:close_windows()
46endfunction
47
48function Test_breakindent02()
49  " simple breakindent test with showbreak set
50  call s:test_windows('setl briopt=min:0 sbr=>>')
51  let lines=s:screen_lines(line('.'),8)
52  let expect=[
53\ "    abcd",
54\ "    >>qr",
55\ "    >>EF",
56\ ]
57  call s:compare_lines(expect, lines)
58  call s:close_windows('set sbr=')
59endfunction
60
61function Test_breakindent03()
62  " simple breakindent test with showbreak set and briopt including sbr
63  call s:test_windows('setl briopt=sbr,min:0 sbr=++')
64  let lines=s:screen_lines(line('.'),8)
65  let expect=[
66\ "    abcd",
67\ "++  qrst",
68\ "++  GHIJ",
69\ ]
70  call s:compare_lines(expect, lines)
71  " clean up
72  call s:close_windows('set sbr=')
73endfunction
74
75function Test_breakindent04()
76  " breakindent set with min width 18
77  call s:test_windows('setl sbr= briopt=min:18')
78  let lines=s:screen_lines(line('.'),8)
79  let expect=[
80\ "    abcd",
81\ "  qrstuv",
82\ "  IJKLMN",
83\ ]
84  call s:compare_lines(expect, lines)
85  " clean up
86  call s:close_windows('set sbr=')
87endfunction
88
89function Test_breakindent05()
90  " breakindent set and shift by 2
91  call s:test_windows('setl briopt=shift:2,min:0')
92  let lines=s:screen_lines(line('.'),8)
93  let expect=[
94\ "    abcd",
95\ "      qr",
96\ "      EF",
97\ ]
98  call s:compare_lines(expect, lines)
99  call s:close_windows()
100endfunction
101
102function Test_breakindent06()
103  " breakindent set and shift by -1
104  call s:test_windows('setl briopt=shift:-1,min:0')
105  let lines=s:screen_lines(line('.'),8)
106  let expect=[
107\ "    abcd",
108\ "   qrstu",
109\ "   HIJKL",
110\ ]
111  call s:compare_lines(expect, lines)
112  call s:close_windows()
113endfunction
114
115function Test_breakindent07()
116  " breakindent set and shift by 1, Number  set sbr=? and briopt:sbr
117  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu sbr=? nuw=4 cpo+=n')
118  let lines=s:screen_lines(line('.'),10)
119  let expect=[
120\ "  2     ab",
121\ "?        m",
122\ "?        x",
123\ ]
124  call s:compare_lines(expect, lines)
125  " clean up
126  call s:close_windows('set sbr= cpo-=n')
127endfunction
128
129function Test_breakindent07a()
130  " breakindent set and shift by 1, Number  set sbr=? and briopt:sbr
131  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu sbr=? nuw=4')
132  let lines=s:screen_lines(line('.'),10)
133  let expect=[
134\ "  2     ab",
135\ "    ?    m",
136\ "    ?    x",
137\ ]
138  call s:compare_lines(expect, lines)
139  " clean up
140  call s:close_windows('set sbr=')
141endfunction
142
143function Test_breakindent08()
144  " breakindent set and shift by 1, Number and list set sbr=# and briopt:sbr
145  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu nuw=4 sbr=# list cpo+=n ts=4')
146  " make sure, cache is invalidated!
147  set ts=8
148  redraw!
149  set ts=4
150  redraw!
151  let lines=s:screen_lines(line('.'),10)
152  let expect=[
153\ "  2 ^Iabcd",
154\ "#      opq",
155\ "#      BCD",
156\ ]
157  call s:compare_lines(expect, lines)
158  call s:close_windows('set sbr= cpo-=n')
159endfunction
160
161function Test_breakindent08a()
162  " breakindent set and shift by 1, Number and list set sbr=# and briopt:sbr
163  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu nuw=4 sbr=# list')
164  let lines=s:screen_lines(line('.'),10)
165  let expect=[
166\ "  2 ^Iabcd",
167\ "    #  opq",
168\ "    #  BCD",
169\ ]
170  call s:compare_lines(expect, lines)
171  call s:close_windows('set sbr=')
172endfunction
173
174function Test_breakindent09()
175  " breakindent set and shift by 1, Number and list set sbr=#
176  call s:test_windows('setl briopt=shift:1,min:0 nu nuw=4 sbr=# list')
177  let lines=s:screen_lines(line('.'),10)
178  let expect=[
179\ "  2 ^Iabcd",
180\ "       #op",
181\ "       #AB",
182\ ]
183  call s:compare_lines(expect, lines)
184  call s:close_windows('set sbr=')
185endfunction
186
187function Test_breakindent10()
188  " breakindent set, Number set sbr=~
189  call s:test_windows('setl cpo+=n sbr=~ nu nuw=4 nolist briopt=sbr,min:0')
190  " make sure, cache is invalidated!
191  set ts=8
192  redraw!
193  set ts=4
194  redraw!
195  let lines=s:screen_lines(line('.'),10)
196  let expect=[
197\ "  2     ab",
198\ "~       mn",
199\ "~       yz",
200\ ]
201  call s:compare_lines(expect, lines)
202  call s:close_windows('set sbr= cpo-=n')
203endfunction
204
205function Test_breakindent11()
206  " test strdisplaywidth()
207  call s:test_windows('setl cpo-=n sbr=>> nu nuw=4 nolist briopt= ts=4')
208  let text=getline(2)
209  let width = strlen(text[1:])+indent(2)+strlen(&sbr)*3 " text wraps 3 times
210  call assert_equal(width, strdisplaywidth(text))
211  call s:close_windows('set sbr=')
212endfunction
213
214function Test_breakindent12()
215  " test breakindent with long indent
216  let s:input="\t\t\t\t\t{"
217  call s:test_windows('setl breakindent linebreak briopt=min:10 nu numberwidth=3 ts=4 list listchars=tab:>-')
218  let lines=s:screen_lines(2,16)
219  let expect=[
220\ " 2 >--->--->--->",
221\ "          ---{  ",
222\ "~               ",
223\ ]
224  call s:compare_lines(expect, lines)
225  call s:close_windows('set nuw=4 listchars=')
226endfunction
227
228function Test_breakindent13()
229  let s:input=""
230  call s:test_windows('setl breakindent briopt=min:10 ts=8')
231  vert resize 20
232  call setline(1, ["    a\tb\tc\td\te", "    z   y       x       w       v"])
233  1
234  norm! fbgj"ayl
235  2
236  norm! fygj"byl
237  call assert_equal('d', @a)
238  call assert_equal('w', @b)
239  call s:close_windows()
240endfunction
241
242function Test_breakindent14()
243  let s:input=""
244  call s:test_windows('setl breakindent briopt= ts=8')
245  vert resize 30
246  norm! 3a1234567890
247  norm! a    abcde
248  exec "norm! 0\<C-V>tex"
249  let lines=s:screen_lines(line('.'),8)
250  let expect=[
251\ "e       ",
252\ "~       ",
253\ "~       ",
254\ ]
255  call s:compare_lines(expect, lines)
256  call s:close_windows()
257endfunction
258
259function Test_breakindent15()
260  let s:input=""
261  call s:test_windows('setl breakindent briopt= ts=8 sw=8')
262  vert resize 30
263  norm! 4a1234567890
264  exe "normal! >>\<C-V>3f0x"
265  let lines=s:screen_lines(line('.'),20)
266  let expect=[
267\ "        1234567890  ",
268\ "~                   ",
269\ "~                   ",
270\ ]
271  call s:compare_lines(expect, lines)
272  call s:close_windows()
273endfunction
274
275function Test_breakindent16()
276  " Check that overlong lines are indented correctly.
277  let s:input=""
278  call s:test_windows('setl breakindent briopt=min:0 ts=4')
279  call setline(1, "\t".repeat("1234567890", 10))
280  resize 6
281  norm! 1gg$
282  redraw!
283  let lines=s:screen_lines(1,10)
284  let expect=[
285\ "    789012",
286\ "    345678",
287\ "    901234",
288\ ]
289  call s:compare_lines(expect, lines)
290  let lines=s:screen_lines(4,10)
291  let expect=[
292\ "    567890",
293\ "    123456",
294\ "    7890  ",
295\ ]
296  call s:compare_lines(expect, lines)
297  call s:close_windows()
298endfunction
299