069_multibyte_formatting_spec.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. -- Test for multibyte text formatting.
  2. -- Also test, that 'mps' with multibyte chars works.
  3. -- And test "ra" on multibyte characters.
  4. -- Also test byteidx() and byteidxcomp()
  5. local t = require('test.testutil')
  6. local n = require('test.functional.testnvim')()
  7. local feed, insert, eq, eval, clear, feed_command, expect =
  8. n.feed, n.insert, t.eq, n.eval, n.clear, n.feed_command, n.expect
  9. describe('multibyte text', function()
  10. before_each(clear)
  11. it('formatting with "set fo=t"', function()
  12. insert([[
  13. {
  14. XYZ
  15. abc XYZ
  16. }]])
  17. feed_command('/^{/+1')
  18. feed_command('set tw=2 fo=t')
  19. feed('gqgqjgqgqo<cr>')
  20. feed('XYZ<cr>')
  21. feed('abc XYZ<esc><esc>')
  22. expect([[
  23. {
  24. XYZ
  25. abc
  26. XYZ
  27. XYZ
  28. abc
  29. XYZ
  30. }]])
  31. end)
  32. it('formatting with "set fo=tm"', function()
  33. insert([[
  34. {
  35. Xa
  36. X a
  37. XY
  38. X Y
  39. }]])
  40. feed_command('/^{/+1')
  41. feed_command('set tw=1 fo=tm')
  42. feed('gqgqjgqgqjgqgqjgqgqjgqgqo<cr>')
  43. feed('X<cr>')
  44. feed('Xa<cr>')
  45. feed('X a<cr>')
  46. feed('XY<cr>')
  47. feed('X Y<esc><esc>')
  48. expect([[
  49. {
  50. a
  51. a
  52. a
  53. a
  54. }]])
  55. end)
  56. it('formatting with "set fo=tm" (part 2)', function()
  57. insert([[
  58. {
  59. Xa
  60. X a
  61. XY
  62. X Y
  63. aX
  64. abX
  65. abcX
  66. abX c
  67. abXY
  68. }]])
  69. feed_command('/^{/+1')
  70. feed_command('set tw=2 fo=tm')
  71. feed('gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqo<cr>')
  72. feed('X<cr>')
  73. feed('Xa<cr>')
  74. feed('X a<cr>')
  75. feed('XY<cr>')
  76. feed('X Y<cr>')
  77. feed('aX<cr>')
  78. feed('abX<cr>')
  79. feed('abcX<cr>')
  80. feed('abX c<cr>')
  81. feed('abXY<esc><esc>')
  82. expect([[
  83. {
  84. a
  85. a
  86. a
  87. ab
  88. abc
  89. ab
  90. c
  91. ab
  92. a
  93. a
  94. a
  95. ab
  96. abc
  97. ab
  98. c
  99. ab
  100. }]])
  101. end)
  102. it('formatting with "set ai fo=tm"', function()
  103. insert([[
  104. {
  105. Xa
  106. }]])
  107. feed_command('/^{/+1')
  108. feed_command('set ai tw=2 fo=tm')
  109. feed('gqgqjgqgqo<cr>')
  110. feed('X<cr>')
  111. feed('Xa<esc>')
  112. expect([[
  113. {
  114. a
  115. a
  116. }]])
  117. end)
  118. it('formatting with "set ai fo=tm" (part 2)', function()
  119. insert([[
  120. {
  121. Xa
  122. }]])
  123. feed_command('/^{/+1')
  124. feed_command('set noai tw=2 fo=tm')
  125. feed('gqgqjgqgqo<cr>')
  126. -- Literal spaces will be trimmed from the by feed().
  127. feed('<space><space>X<cr>')
  128. feed('<space><space>Xa<esc>')
  129. expect([[
  130. {
  131. a
  132. a
  133. }]])
  134. end)
  135. it('formatting with "set fo=cqm" and multibyte comments', function()
  136. insert([[
  137. {
  138. Xa
  139. XaY
  140. XY
  141. XYZ
  142. X Y
  143. X YZ
  144. XX
  145. XXa
  146. XXY
  147. }]])
  148. feed_command('/^{/+1')
  149. feed_command('set tw=2 fo=cqm comments=n:X')
  150. feed('gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqo<cr>')
  151. feed('X<cr>')
  152. feed('Xa<cr>')
  153. feed('XaY<cr>')
  154. feed('XY<cr>')
  155. feed('XYZ<cr>')
  156. feed('X Y<cr>')
  157. feed('X YZ<cr>')
  158. feed('XX<cr>')
  159. feed('XXa<cr>')
  160. feed('XXY<esc><esc>')
  161. expect([[
  162. {
  163. Xa
  164. Xa
  165. XY
  166. XY
  167. XY
  168. XZ
  169. X Y
  170. X Y
  171. X Z
  172. XX
  173. XXa
  174. XXY
  175. Xa
  176. Xa
  177. XY
  178. XY
  179. XY
  180. XZ
  181. X Y
  182. X Y
  183. X Z
  184. XX
  185. XXa
  186. XXY
  187. }]])
  188. end)
  189. it('formatting in replace mode', function()
  190. insert([[
  191. {
  192. }]])
  193. feed_command('/^{/+1')
  194. feed_command('set tw=2 fo=tm')
  195. feed('RXa<esc>')
  196. expect([[
  197. {
  198. a
  199. }]])
  200. end)
  201. it("as values of 'mps'", function()
  202. insert([[
  203. {
  204. ‘ two three ’ four
  205. }]])
  206. feed_command('/^{/+1')
  207. feed_command('set mps+=‘:’')
  208. feed('d%<cr>')
  209. expect([[
  210. {
  211. four
  212. }]])
  213. end)
  214. it('can be replaced with r', function()
  215. insert([[
  216. abba
  217. aab]])
  218. feed('gg0Vjra<cr>')
  219. expect([[
  220. aaaa
  221. aaa]])
  222. end)
  223. it("doesn't interfere with 'whichwrap'", function()
  224. insert([[
  225. á
  226. x]])
  227. feed_command('set whichwrap+=h')
  228. feed_command('/^x')
  229. feed('dh')
  230. expect([[
  231. áx]])
  232. end)
  233. it('can be queried with byteidx() and byteidxcomp()', function()
  234. -- One char of two bytes.
  235. feed_command("let a = '.é.'")
  236. -- Normal e with composing char.
  237. feed_command("let b = '.é.'")
  238. eq(0, eval('byteidx(a, 0)'))
  239. eq(1, eval('byteidx(a, 1)'))
  240. eq(3, eval('byteidx(a, 2)'))
  241. eq(4, eval('byteidx(a, 3)'))
  242. eq(-1, eval('byteidx(a, 4)'))
  243. eq(0, eval('byteidx(b, 0)'))
  244. eq(1, eval('byteidx(b, 1)'))
  245. eq(4, eval('byteidx(b, 2)'))
  246. eq(5, eval('byteidx(b, 3)'))
  247. eq(-1, eval('byteidx(b, 4)'))
  248. eq(0, eval('byteidxcomp(a, 0)'))
  249. eq(1, eval('byteidxcomp(a, 1)'))
  250. eq(3, eval('byteidxcomp(a, 2)'))
  251. eq(4, eval('byteidxcomp(a, 3)'))
  252. eq(-1, eval('byteidxcomp(a, 4)'))
  253. eq(0, eval('byteidxcomp(b, 0)'))
  254. eq(1, eval('byteidxcomp(b, 1)'))
  255. eq(2, eval('byteidxcomp(b, 2)'))
  256. eq(4, eval('byteidxcomp(b, 3)'))
  257. eq(5, eval('byteidxcomp(b, 4)'))
  258. eq(-1, eval('byteidxcomp(b, 5)'))
  259. end)
  260. it('correctly interact with the \zs pattern', function()
  261. eq('a1a2a3a', eval([[substitute('123', '\zs', 'a', 'g')]]))
  262. end)
  263. end)