fold_spec.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. -- Tests for folding.
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local feed, insert, feed_command, expect_any = n.feed, n.insert, n.feed_command, n.expect_any
  5. local command = n.command
  6. local exec = n.exec
  7. describe('folding', function()
  8. local screen
  9. before_each(function()
  10. n.clear()
  11. screen = Screen.new(45, 8)
  12. end)
  13. it('creation, opening, moving (to the end) and closing', function()
  14. insert([[
  15. 1 aa
  16. 2 bb
  17. 3 cc
  18. last
  19. ]])
  20. -- Basic test if a fold can be created, opened, moving to the end and
  21. -- closed.
  22. feed_command('1')
  23. feed('zf2j')
  24. feed_command('call append("$", "manual " . getline(foldclosed(".")))')
  25. feed('zo')
  26. feed_command('call append("$", foldclosed("."))')
  27. feed(']z')
  28. feed_command('call append("$", getline("."))')
  29. feed('zc')
  30. feed_command('call append("$", getline(foldclosed(".")))')
  31. expect_any([[
  32. manual 1 aa
  33. -1
  34. 3 cc
  35. 1 aa]])
  36. end)
  37. it('foldmethod=marker', function()
  38. screen:try_resize(20, 10)
  39. insert([[
  40. dd {{{
  41. ee {{{ }}}
  42. ff }}}
  43. ]])
  44. feed_command('set fdm=marker fdl=1')
  45. feed_command('2')
  46. feed_command('call append("$", "line 2 foldlevel=" . foldlevel("."))')
  47. feed('[z')
  48. feed_command('call append("$", foldlevel("."))')
  49. feed('jo{{ <esc>r{jj') -- writes '{{{' and moves 2 lines bot
  50. feed_command('call append("$", foldlevel("."))')
  51. feed('kYpj')
  52. feed_command('call append("$", foldlevel("."))')
  53. n.poke_eventloop()
  54. screen:expect([[
  55. dd {{{ |
  56. ee {{{ }}} |
  57. {{{ |
  58. ff }}} |*2
  59. ^ |
  60. line 2 foldlevel=2 |
  61. 1 |*2
  62. |
  63. ]])
  64. end)
  65. it('foldmethod=indent', function()
  66. screen:try_resize(20, 8)
  67. feed_command('set fdm=indent sw=2')
  68. insert([[
  69. aa
  70. bb
  71. cc
  72. last
  73. ]])
  74. feed_command('call append("$", "foldlevel line3=" . foldlevel(3))')
  75. feed_command('call append("$", foldlevel(2))')
  76. feed('zR')
  77. n.poke_eventloop()
  78. screen:expect([[
  79. aa |
  80. bb |
  81. cc |
  82. last |
  83. ^ |
  84. foldlevel line3=2 |
  85. 1 |
  86. |
  87. ]])
  88. end)
  89. it('foldmethod=syntax', function()
  90. screen:try_resize(35, 15)
  91. insert([[
  92. 1 aa
  93. 2 bb
  94. 3 cc
  95. 4 dd {{{
  96. 5 ee {{{ }}}
  97. 6 ff }}}
  98. 7 gg
  99. 8 hh
  100. 9 ii
  101. a jj
  102. b kk
  103. last]])
  104. feed_command('set fdm=syntax fdl=0')
  105. feed_command('syn region Hup start="dd" end="ii" fold contains=Fd1,Fd2,Fd3')
  106. feed_command('syn region Fd1 start="ee" end="ff" fold contained')
  107. feed_command('syn region Fd2 start="gg" end="hh" fold contained')
  108. feed_command('syn region Fd3 start="commentstart" end="commentend" fold contained')
  109. feed('Gzk')
  110. feed_command('call append("$", "folding " . getline("."))')
  111. feed('k')
  112. feed_command('call append("$", getline("."))')
  113. feed('jAcommentstart <esc>Acommentend<esc>')
  114. feed_command('set fdl=1')
  115. feed('3j')
  116. feed_command('call append("$", getline("."))')
  117. feed_command('set fdl=0')
  118. feed('zO<C-L>j') -- <C-L> redraws screen
  119. feed_command('call append("$", getline("."))')
  120. feed_command('set fdl=0')
  121. expect_any([[
  122. folding 9 ii
  123. 3 cc
  124. 9 ii
  125. a jj]])
  126. end)
  127. it('foldmethod=expression', function()
  128. insert([[
  129. 1 aa
  130. 2 bb
  131. 3 cc
  132. 4 dd {{{
  133. 5 ee {{{ }}}
  134. 6 ff }}}
  135. 7 gg
  136. 8 hh
  137. 9 ii
  138. a jj
  139. b kk
  140. last ]])
  141. feed_command([[
  142. fun Flvl()
  143. let l = getline(v:lnum)
  144. if l =~ "bb$"
  145. return 2
  146. elseif l =~ "gg$"
  147. return "s1"
  148. elseif l =~ "ii$"
  149. return ">2"
  150. elseif l =~ "kk$"
  151. return "0"
  152. endif
  153. return "="
  154. endfun
  155. ]])
  156. feed_command('set fdm=expr fde=Flvl()')
  157. feed_command('/bb$')
  158. feed_command('call append("$", "expr " . foldlevel("."))')
  159. feed_command('/hh$')
  160. feed_command('call append("$", foldlevel("."))')
  161. feed_command('/ii$')
  162. feed_command('call append("$", foldlevel("."))')
  163. feed_command('/kk$')
  164. feed_command('call append("$", foldlevel("."))')
  165. expect_any([[
  166. expr 2
  167. 1
  168. 2
  169. 0]])
  170. end)
  171. it('can be opened after :move', function()
  172. -- luacheck: ignore
  173. screen:try_resize(35, 8)
  174. insert([[
  175. Test fdm=indent and :move bug END
  176. line2
  177. Test fdm=indent START
  178. line3
  179. line4]])
  180. feed_command('set noai nosta ')
  181. feed_command('set fdm=indent')
  182. feed_command('1m1')
  183. feed('2jzc')
  184. feed_command('m0')
  185. feed('zR')
  186. expect_any([[
  187. Test fdm=indent START
  188. line3
  189. line4
  190. Test fdm=indent and :move bug END
  191. line2]])
  192. end)
  193. -- oldtest: Test_folds_with_rnu()
  194. it('with relative line numbers', function()
  195. command('set fdm=marker rnu foldcolumn=2')
  196. command('call setline(1, ["{{{1", "nline 1", "{{{1", "line 2"])')
  197. screen:expect([[
  198. {7:+ }{8: 0 }{13:^+-- 2 lines: ·························}|
  199. {7:+ }{8: 1 }{13:+-- 2 lines: ·························}|
  200. {1:~ }|*5
  201. |
  202. ]])
  203. feed('j')
  204. screen:expect([[
  205. {7:+ }{8: 1 }{13:+-- 2 lines: ·························}|
  206. {7:+ }{8: 0 }{13:^+-- 2 lines: ·························}|
  207. {1:~ }|*5
  208. |
  209. ]])
  210. end)
  211. -- oldtest: Test_foldclose_opt()
  212. it('foldclose=all', function()
  213. exec([[
  214. set foldmethod=manual foldclose=all foldopen=all
  215. call setline(1, ['one', 'two', 'three', 'four'])
  216. 2,3fold
  217. ]])
  218. screen:expect([[
  219. ^one |
  220. {13:+-- 2 lines: two····························}|
  221. four |
  222. {1:~ }|*4
  223. |
  224. ]])
  225. feed('2G')
  226. screen:expect([[
  227. one |
  228. ^two |
  229. three |
  230. four |
  231. {1:~ }|*3
  232. |
  233. ]])
  234. feed('4G')
  235. screen:expect([[
  236. one |
  237. {13:+-- 2 lines: two····························}|
  238. ^four |
  239. {1:~ }|*4
  240. |
  241. ]])
  242. feed('3G')
  243. screen:expect([[
  244. one |
  245. two |
  246. ^three |
  247. four |
  248. {1:~ }|*3
  249. |
  250. ]])
  251. feed('1G')
  252. screen:expect([[
  253. ^one |
  254. {13:+-- 2 lines: two····························}|
  255. four |
  256. {1:~ }|*4
  257. |
  258. ]])
  259. feed('2G')
  260. screen:expect([[
  261. one |
  262. ^two |
  263. three |
  264. four |
  265. {1:~ }|*3
  266. |
  267. ]])
  268. feed('k')
  269. screen:expect([[
  270. ^one |
  271. {13:+-- 2 lines: two····························}|
  272. four |
  273. {1:~ }|*4
  274. |
  275. ]])
  276. end)
  277. end)