test_comments.vim 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. " Tests for the various flags in the 'comments' option
  2. " Test for the 'n' flag in 'comments'
  3. func Test_comment_nested()
  4. new
  5. setlocal comments=n:> fo+=ro
  6. exe "normal i> B\nD\<C-C>ggOA\<C-C>joC\<C-C>Go\<BS>>>> F\nH"
  7. exe "normal 5GOE\<C-C>6GoG"
  8. let expected =<< trim END
  9. > A
  10. > B
  11. > C
  12. > D
  13. >>>> E
  14. >>>> F
  15. >>>> G
  16. >>>> H
  17. END
  18. call assert_equal(expected, getline(1, '$'))
  19. close!
  20. endfunc
  21. " Test for the 'b' flag in 'comments'
  22. func Test_comment_blank()
  23. new
  24. setlocal comments=b:* fo+=ro
  25. exe "normal i* E\nF\n\<BS>G\nH\<C-C>ggOC\<C-C>O\<BS>B\<C-C>OA\<C-C>2joD"
  26. let expected =<< trim END
  27. A
  28. *B
  29. * C
  30. * D
  31. * E
  32. * F
  33. *G
  34. H
  35. END
  36. call assert_equal(expected, getline(1, '$'))
  37. close!
  38. endfunc
  39. " Test for the 'f' flag in 'comments' (only the first line has a comment
  40. " string)
  41. func Test_comment_firstline()
  42. new
  43. setlocal comments=f:- fo+=ro
  44. exe "normal i- B\nD\<C-C>ggoC\<C-C>ggOA\<C-C>"
  45. call assert_equal(['A', '- B', ' C', ' D'], getline(1, '$'))
  46. %d
  47. setlocal comments=:-
  48. exe "normal i- B\nD\<C-C>ggoC\<C-C>ggOA\<C-C>"
  49. call assert_equal(['- A', '- B', '- C', '- D'], getline(1, '$'))
  50. close!
  51. endfunc
  52. " Test for the 's', 'm' and 'e' flags in 'comments'
  53. " Test for automatically adding comment leaders in insert mode
  54. func Test_comment_threepiece()
  55. new
  56. setlocal expandtab
  57. call setline(1, ["\t/*"])
  58. setlocal formatoptions=croql
  59. call cursor(1, 3)
  60. call feedkeys("A\<cr>\<cr>/", 'tnix')
  61. call assert_equal(["\t/*", " *", " */"], getline(1, '$'))
  62. " If a comment ends in a single line, then don't add it in the next line
  63. %d
  64. call setline(1, '/* line1 */')
  65. call feedkeys("A\<CR>next line", 'xt')
  66. call assert_equal(['/* line1 */', 'next line'], getline(1, '$'))
  67. %d
  68. " Copy the trailing indentation from the leader comment to a new line
  69. setlocal autoindent noexpandtab
  70. call feedkeys("a\t/*\tone\ntwo\n/", 'xt')
  71. call assert_equal(["\t/*\tone", "\t *\ttwo", "\t */"], getline(1, '$'))
  72. close!
  73. endfunc
  74. " Test for the 'r' flag in 'comments' (right align comment)
  75. func Test_comment_rightalign()
  76. new
  77. setlocal comments=sr:/***,m:**,ex-2:******/ fo+=ro
  78. exe "normal i=\<C-C>o\t /***\nD\n/"
  79. exe "normal 2GOA\<C-C>joB\<C-C>jOC\<C-C>joE\<C-C>GOF\<C-C>joG"
  80. let expected =<< trim END
  81. =
  82. A
  83. /***
  84. ** B
  85. ** C
  86. ** D
  87. ** E
  88. ** F
  89. ******/
  90. G
  91. END
  92. call assert_equal(expected, getline(1, '$'))
  93. close!
  94. endfunc
  95. " Test for the 'O' flag in 'comments'
  96. func Test_comment_O()
  97. new
  98. setlocal comments=Ob:* fo+=ro
  99. exe "normal i* B\nD\<C-C>kOA\<C-C>joC"
  100. let expected =<< trim END
  101. A
  102. * B
  103. * C
  104. * D
  105. END
  106. call assert_equal(expected, getline(1, '$'))
  107. close!
  108. endfunc
  109. " Test for using a multibyte character as a comment leader
  110. func Test_comment_multibyte_leader()
  111. new
  112. let t =<< trim END
  113. {
  114. Xa
  115. XaY
  116. XY
  117. XYZ
  118. X Y
  119. X YZ
  120. XX
  121. XXa
  122. XXY
  123. }
  124. END
  125. call setline(1, t)
  126. call cursor(2, 1)
  127. set tw=2 fo=cqm comments=n:X
  128. exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
  129. let t =<< trim END
  130. Xa
  131. XaY
  132. XY
  133. XYZ
  134. X Y
  135. X YZ
  136. XX
  137. XXa
  138. XXY
  139. END
  140. exe "normal o\n" . join(t, "\n")
  141. let expected =<< trim END
  142. {
  143. Xa
  144. Xa
  145. XY
  146. XY
  147. XY
  148. XZ
  149. X Y
  150. X Y
  151. X Z
  152. XX
  153. XXa
  154. XXY
  155. Xa
  156. Xa
  157. XY
  158. XY
  159. XY
  160. XZ
  161. X Y
  162. X Y
  163. X Z
  164. XX
  165. XXa
  166. XXY
  167. }
  168. END
  169. call assert_equal(expected, getline(1, '$'))
  170. set tw& fo& comments&
  171. close!
  172. endfunc
  173. " Test for a space character in 'comments' setting
  174. func Test_comment_space()
  175. new
  176. setlocal comments=b:\ > fo+=ro
  177. exe "normal i> B\nD\<C-C>ggOA\<C-C>joC"
  178. exe "normal Go > F\nH\<C-C>kOE\<C-C>joG"
  179. let expected =<< trim END
  180. A
  181. > B
  182. C
  183. D
  184. > E
  185. > F
  186. > G
  187. > H
  188. END
  189. call assert_equal(expected, getline(1, '$'))
  190. close!
  191. endfunc
  192. " Test for formatting lines with and without comments
  193. func Test_comment_format_lines()
  194. new
  195. call setline(1, ['one', '/* two */', 'three'])
  196. normal gggqG
  197. call assert_equal(['one', '/* two */', 'three'], getline(1, '$'))
  198. close!
  199. endfunc
  200. " Test for using 'a' in 'formatoptions' with comments
  201. func Test_comment_autoformat()
  202. new
  203. setlocal formatoptions+=a
  204. call feedkeys("a- one\n- two\n", 'xt')
  205. call assert_equal(['- one', '- two', ''], getline(1, '$'))
  206. %d
  207. call feedkeys("a\none\n", 'xt')
  208. call assert_equal(['', 'one', ''], getline(1, '$'))
  209. setlocal formatoptions+=aw
  210. %d
  211. call feedkeys("aone \ntwo\n", 'xt')
  212. call assert_equal(['one two', ''], getline(1, '$'))
  213. %d
  214. call feedkeys("aone\ntwo\n", 'xt')
  215. call assert_equal(['one', 'two', ''], getline(1, '$'))
  216. set backspace=indent,eol,start
  217. %d
  218. call feedkeys("aone \n\<BS>", 'xt')
  219. call assert_equal(['one'], getline(1, '$'))
  220. set backspace&
  221. close!
  222. endfunc
  223. " Test for joining lines with comments ('j' flag in 'formatoptions')
  224. func Test_comment_join_lines_fo_j()
  225. new
  226. setlocal fo+=j comments=://
  227. call setline(1, ['i++; // comment1', ' // comment2'])
  228. normal J
  229. call assert_equal('i++; // comment1 comment2', getline(1))
  230. setlocal fo-=j
  231. call setline(1, ['i++; // comment1', ' // comment2'])
  232. normal J
  233. call assert_equal('i++; // comment1 // comment2', getline(1))
  234. " Test with nested comments
  235. setlocal fo+=j comments=n:>,n:)
  236. call setline(1, ['i++; > ) > ) comment1', ' > ) comment2'])
  237. normal J
  238. call assert_equal('i++; > ) > ) comment1 comment2', getline(1))
  239. close!
  240. endfunc
  241. " Test for formatting lines where only the first line has a comment.
  242. func Test_comment_format_firstline_comment()
  243. new
  244. setlocal formatoptions=tcq
  245. call setline(1, ['- one two', 'three'])
  246. normal gggqG
  247. call assert_equal(['- one two three'], getline(1, '$'))
  248. %d
  249. call setline(1, ['- one', '- two'])
  250. normal gggqG
  251. call assert_equal(['- one', '- two'], getline(1, '$'))
  252. close!
  253. endfunc
  254. " vim: shiftwidth=2 sts=2 expandtab