029_join_spec.lua 10 KB

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