robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
10.7 KiB359 linesraw
1-- Test for joining lines
2
3local t = require('test.testutil')
4local n = require('test.functional.testnvim')()
5
6local describe, it, before_each = t.describe, t.it, t.before_each
7local clear, eq = n.clear, t.eq
8local eval, command = n.eval, n.command
9local feed = n.feed
10local insert = n.insert
11local expect = n.expect
12local feed_command = n.feed_command
13
14describe('joining lines', function()
15  before_each(clear)
16
17  it('is working', function()
18    command('new')
19    command([[call setline(1, ['one', 'two', 'three', 'four'])]])
20    command('normal J')
21    eq('one two', eval('getline(1)'))
22    command('%del')
23    command([[call setline(1, ['one', 'two', 'three', 'four'])]])
24    command('normal 10J')
25    eq('one two three four', eval('getline(1)'))
26  end)
27
28  -- luacheck: ignore 613 (Trailing whitespaces in a string)
29  -- luacheck: ignore 611 (Line contains only whitespaces)
30  it("keeps marks with different 'joinspaces' settings", function()
31    insert([[
32      firstline
33      asdfasdf.
34      asdf
35      asdfasdf. 
36      asdf
37      asdfasdf.  
38      asdf
39      asdfasdf.	
40      asdf
41      asdfasdf. 	
42      asdf
43      asdfasdf.	 
44      asdf
45      asdfasdf.		
46      asdf
47      asdfasdf
48      asdf
49      asdfasdf 
50      asdf
51      asdfasdf  
52      asdf
53      asdfasdf	
54      asdf
55      asdfasdf	 
56      asdf
57      asdfasdf 	
58      asdf
59      asdfasdf		
60      asdf
61      zx cvn.
62      as dfg?
63      hjkl iop!
64      ert
65      ]])
66
67    -- Switch off 'joinspaces', then join some lines in the buffer using "J".
68    -- Also set a few marks and record their movement when joining lines.
69    feed_command('set nojoinspaces')
70    feed_command('/firstline/')
71    feed('j"td/^$/<cr>')
72    feed('PJjJjJjJjJjJjJjJjJjJjJjJjJjJ')
73    feed('j05lmx2j06lmy2k4Jy3l$p`xyl$p`yy2l$p')
74
75    -- Do the same with 'joinspaces' on.
76    feed_command('set joinspaces')
77    feed('j"tp')
78    feed('JjJjJjJjJjJjJjJjJjJjJjJjJjJ')
79    feed('j05lmx2j06lmy2k4Jy3l$p`xyl$p`yy2l$po<esc>')
80
81    feed_command('1d')
82
83    expect([[
84      asdfasdf. asdf
85      asdfasdf. asdf
86      asdfasdf.  asdf
87      asdfasdf.	asdf
88      asdfasdf. 	asdf
89      asdfasdf.	 asdf
90      asdfasdf.		asdf
91      asdfasdf asdf
92      asdfasdf asdf
93      asdfasdf  asdf
94      asdfasdf	asdf
95      asdfasdf	 asdf
96      asdfasdf 	asdf
97      asdfasdf		asdf
98      zx cvn. as dfg? hjkl iop! ert ernop
99      
100      asdfasdf.  asdf
101      asdfasdf.  asdf
102      asdfasdf.  asdf
103      asdfasdf.	asdf
104      asdfasdf. 	asdf
105      asdfasdf.	 asdf
106      asdfasdf.		asdf
107      asdfasdf asdf
108      asdfasdf asdf
109      asdfasdf  asdf
110      asdfasdf	asdf
111      asdfasdf	 asdf
112      asdfasdf 	asdf
113      asdfasdf		asdf
114      zx cvn.  as dfg?  hjkl iop!  ert  enop
115      ]])
116  end)
117
118  it("removes comment leaders with 'joinspaces' off", function()
119    insert([[
120      {
121      
122      /*
123       * Make sure the previous comment leader is not removed.
124       */
125      
126      /*
127       * Make sure the previous comment leader is not removed.
128       */
129      
130      // Should the next comment leader be left alone?
131      // Yes.
132      
133      // Should the next comment leader be left alone?
134      // Yes.
135      
136      /* Here the comment leader should be left intact. */
137      // And so should this one.
138      
139      /* Here the comment leader should be left intact. */
140      // And so should this one.
141      
142      if (condition) // Remove the next comment leader!
143                     // OK, I will.
144          action();
145      
146      if (condition) // Remove the next comment leader!
147                     // OK, I will.
148          action();
149      }
150      ]])
151
152    feed_command('/^{/+1')
153    feed_command('set comments=s1:/*,mb:*,ex:*/,://')
154    feed_command('set nojoinspaces')
155    feed_command('set backspace=eol,start')
156
157    -- With 'joinspaces' switched off, join lines using both "J" and :join and
158    -- verify that comment leaders are stripped or kept as appropriate.
159    feed_command('.,+3join')
160    feed('j4J<cr>')
161    feed_command('.,+2join')
162    feed('j3J<cr>')
163    feed_command('.,+2join')
164    feed('j3J<cr>')
165    feed_command('.,+2join')
166    feed('jj3J<cr>')
167
168    expect([[
169      {
170      /* Make sure the previous comment leader is not removed. */
171      /* Make sure the previous comment leader is not removed. */
172      // Should the next comment leader be left alone? Yes.
173      // Should the next comment leader be left alone? Yes.
174      /* Here the comment leader should be left intact. */ // And so should this one.
175      /* Here the comment leader should be left intact. */ // And so should this one.
176      if (condition) // Remove the next comment leader! OK, I will.
177          action();
178      if (condition) // Remove the next comment leader! OK, I will.
179          action();
180      }
181      ]])
182  end)
183
184  -- This test case has nothing to do with joining lines.
185  it("Ctrl-u and 'backspace' compatibility", function()
186    -- Notice that the buffer text, which is intended to helpfully hint at
187    -- what's being done in the test, is off by one line. (For example, "this
188    -- should be deleted" should not be deleted, but the line below it should,
189    -- and is.) This is likely a mistake, but was kept here for consistency.
190    insert([[
191      1 this shouldn't be deleted
192      2 this shouldn't be deleted
193      3 this shouldn't be deleted
194      4 this should be deleted
195      5 this shouldn't be deleted
196      6 this shouldn't be deleted
197      7 this shouldn't be deleted
198      8 this shouldn't be deleted (not touched yet)
199      ]])
200
201    -- As mentioned above, we mimic the wrong initial cursor position in the old
202    -- test by advancing one line further.
203    feed_command([[/^\d\+ this]], '+1')
204
205    -- Test with the default 'backspace' setting.
206    feed('Avim1<c-u><esc><cr>')
207    feed('Avim2<c-g>u<c-u><esc><cr>')
208    feed_command('set cpo-=<')
209    feed_command('inoremap <c-u> <left><c-u>')
210    feed('Avim3<c-u><esc><cr>')
211    feed_command('iunmap <c-u>')
212    feed('Avim4<c-u><c-u><esc><cr>')
213
214    -- Test with 'backspace' set to the compatible setting.
215    feed_command('set backspace=')
216    feed('A vim5<esc>A<c-u><c-u><esc><cr>')
217    feed('A vim6<esc>Azwei<c-g>u<c-u><esc><cr>')
218    feed_command('inoremap <c-u> <left><c-u>')
219    feed('A vim7<c-u><c-u><esc><cr>')
220
221    expect([[
222      1 this shouldn't be deleted
223      2 this shouldn't be deleted
224      3 this shouldn't be deleted
225      4 this should be deleted3
226      
227      6 this shouldn't be deleted vim5
228      7 this shouldn't be deleted vim6
229      8 this shouldn't be deleted (not touched yet) vim7
230      ]])
231  end)
232
233  it("removes comment leaders with 'joinspaces' on", function()
234    insert([[
235      {
236      
237      /*
238       * Make sure the previous comment leader is not removed.
239       */
240      
241      /*
242       * Make sure the previous comment leader is not removed.
243       */
244      
245      /* List:
246       * - item1
247       *   foo bar baz
248       *   foo bar baz
249       * - item2
250       *   foo bar baz
251       *   foo bar baz
252       */
253      
254      /* List:
255       * - item1
256       *   foo bar baz
257       *   foo bar baz
258       * - item2
259       *   foo bar baz
260       *   foo bar baz
261       */
262      
263      // Should the next comment leader be left alone?
264      // Yes.
265      
266      // Should the next comment leader be left alone?
267      // Yes.
268      
269      /* Here the comment leader should be left intact. */
270      // And so should this one.
271      
272      /* Here the comment leader should be left intact. */
273      // And so should this one.
274      
275      if (condition) // Remove the next comment leader!
276                     // OK, I will.
277          action();
278      
279      if (condition) // Remove the next comment leader!
280                     // OK, I will.
281          action();
282      
283      int i = 7 /* foo *// 3
284       // comment
285       ;
286      
287      int i = 7 /* foo *// 3
288       // comment
289       ;
290      
291      ># Note that the last character of the ending comment leader (left angle
292       # bracket) is a comment leader itself. Make sure that this comment leader is
293       # not removed from the next line #<
294      < On this line a new comment is opened which spans 2 lines. This comment should
295      < retain its comment leader.
296      
297      ># Note that the last character of the ending comment leader (left angle
298       # bracket) is a comment leader itself. Make sure that this comment leader is
299       # not removed from the next line #<
300      < On this line a new comment is opened which spans 2 lines. This comment should
301      < retain its comment leader.
302      
303      }
304      ]])
305
306    feed_command('/^{/+1')
307    feed_command([[set comments=sO:*\ -,mO:*\ \ ,exO:*/]])
308    feed_command('set comments+=s1:/*,mb:*,ex:*/,://')
309    feed_command('set comments+=s1:>#,mb:#,ex:#<,:<')
310    feed_command('set backspace=eol,start')
311
312    -- With 'joinspaces' on (the default setting), again join lines and verify
313    -- that comment leaders are stripped or kept as appropriate.
314    feed_command('.,+3join')
315    feed('j4J<cr>')
316    feed_command('.,+8join')
317    feed('j9J<cr>')
318    feed_command('.,+2join')
319    feed('j3J<cr>')
320    feed_command('.,+2join')
321    feed('j3J<cr>')
322    feed_command('.,+2join')
323    feed('jj3J<cr>')
324    feed('j')
325    feed_command('.,+2join')
326    feed('jj3J<cr>')
327    feed('j')
328    feed_command('.,+5join')
329    feed('j6J<cr>')
330    feed(
331      'oSome code!<cr>// Make sure backspacing does not remove this comment leader.<esc>0i<bs><esc>'
332    )
333
334    expect([[
335      {
336      /* Make sure the previous comment leader is not removed.  */
337      /* Make sure the previous comment leader is not removed.  */
338      /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */
339      /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */
340      // Should the next comment leader be left alone?  Yes.
341      // Should the next comment leader be left alone?  Yes.
342      /* Here the comment leader should be left intact. */ // And so should this one.
343      /* Here the comment leader should be left intact. */ // And so should this one.
344      if (condition) // Remove the next comment leader!  OK, I will.
345          action();
346      if (condition) // Remove the next comment leader!  OK, I will.
347          action();
348      int i = 7 /* foo *// 3 // comment
349       ;
350      int i = 7 /* foo *// 3 // comment
351       ;
352      ># Note that the last character of the ending comment leader (left angle bracket) is a comment leader itself. Make sure that this comment leader is not removed from the next line #< < On this line a new comment is opened which spans 2 lines. This comment should retain its comment leader.
353      ># Note that the last character of the ending comment leader (left angle bracket) is a comment leader itself. Make sure that this comment leader is not removed from the next line #< < On this line a new comment is opened which spans 2 lines. This comment should retain its comment leader.
354      
355      Some code!// Make sure backspacing does not remove this comment leader.
356      }
357      ]])
358  end)
359end)