buffer_spec.lua 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local Screen = require('test.functional.ui.screen')
  3. local clear, nvim, buffer = helpers.clear, helpers.nvim, helpers.buffer
  4. local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq
  5. local curbufmeths, ok = helpers.curbufmeths, helpers.ok
  6. local meths = helpers.meths
  7. local funcs = helpers.funcs
  8. local request = helpers.request
  9. local exc_exec = helpers.exc_exec
  10. local exec_lua = helpers.exec_lua
  11. local feed_command = helpers.feed_command
  12. local insert = helpers.insert
  13. local NIL = helpers.NIL
  14. local command = helpers.command
  15. local bufmeths = helpers.bufmeths
  16. local feed = helpers.feed
  17. local pcall_err = helpers.pcall_err
  18. describe('api/buf', function()
  19. before_each(clear)
  20. -- access deprecated functions
  21. local function curbuf_depr(method, ...)
  22. return request('buffer_'..method, 0, ...)
  23. end
  24. describe('nvim_buf_set_lines, nvim_buf_line_count', function()
  25. it('deprecated forms', function()
  26. eq(1, curbuf_depr('line_count'))
  27. curbuf_depr('insert', -1, {'line'})
  28. eq(2, curbuf_depr('line_count'))
  29. curbuf_depr('insert', -1, {'line'})
  30. eq(3, curbuf_depr('line_count'))
  31. curbuf_depr('del_line', -1)
  32. eq(2, curbuf_depr('line_count'))
  33. curbuf_depr('del_line', -1)
  34. curbuf_depr('del_line', -1)
  35. -- There's always at least one line
  36. eq(1, curbuf_depr('line_count'))
  37. end)
  38. it('cursor position is maintained after lines are inserted #9961', function()
  39. -- replace the buffer contents with these three lines.
  40. request('nvim_buf_set_lines', 0, 0, -1, 1, {"line1", "line2", "line3", "line4"})
  41. -- Set the current cursor to {3, 2}.
  42. curwin('set_cursor', {3, 2})
  43. -- add 2 lines and delete 1 line above the current cursor position.
  44. request('nvim_buf_set_lines', 0, 1, 2, 1, {"line5", "line6"})
  45. -- check the current set of lines in the buffer.
  46. eq({"line1", "line5", "line6", "line3", "line4"}, buffer('get_lines', 0, 0, -1, 1))
  47. -- cursor should be moved below by 1 line.
  48. eq({4, 2}, curwin('get_cursor'))
  49. -- add a line after the current cursor position.
  50. request('nvim_buf_set_lines', 0, 5, 5, 1, {"line7"})
  51. -- check the current set of lines in the buffer.
  52. eq({"line1", "line5", "line6", "line3", "line4", "line7"}, buffer('get_lines', 0, 0, -1, 1))
  53. -- cursor position is unchanged.
  54. eq({4, 2}, curwin('get_cursor'))
  55. -- overwrite current cursor line.
  56. request('nvim_buf_set_lines', 0, 3, 5, 1, {"line8", "line9"})
  57. -- check the current set of lines in the buffer.
  58. eq({"line1", "line5", "line6", "line8", "line9", "line7"}, buffer('get_lines', 0, 0, -1, 1))
  59. -- cursor position is unchanged.
  60. eq({4, 2}, curwin('get_cursor'))
  61. -- delete current cursor line.
  62. request('nvim_buf_set_lines', 0, 3, 5, 1, {})
  63. -- check the current set of lines in the buffer.
  64. eq({"line1", "line5", "line6", "line7"}, buffer('get_lines', 0, 0, -1, 1))
  65. -- cursor position is unchanged.
  66. eq({4, 2}, curwin('get_cursor'))
  67. end)
  68. it('line_count has defined behaviour for unloaded buffers', function()
  69. -- we'll need to know our bufnr for when it gets unloaded
  70. local bufnr = curbuf('get_number')
  71. -- replace the buffer contents with these three lines
  72. request('nvim_buf_set_lines', bufnr, 0, -1, 1, {"line1", "line2", "line3", "line4"})
  73. -- check the line count is correct
  74. eq(4, request('nvim_buf_line_count', bufnr))
  75. -- force unload the buffer (this will discard changes)
  76. command('new')
  77. command('bunload! '..bufnr)
  78. -- line count for an unloaded buffer should always be 0
  79. eq(0, request('nvim_buf_line_count', bufnr))
  80. end)
  81. it('get_lines has defined behaviour for unloaded buffers', function()
  82. -- we'll need to know our bufnr for when it gets unloaded
  83. local bufnr = curbuf('get_number')
  84. -- replace the buffer contents with these three lines
  85. buffer('set_lines', bufnr, 0, -1, 1, {"line1", "line2", "line3", "line4"})
  86. -- confirm that getting lines works
  87. eq({"line2", "line3"}, buffer('get_lines', bufnr, 1, 3, 1))
  88. -- force unload the buffer (this will discard changes)
  89. command('new')
  90. command('bunload! '..bufnr)
  91. -- attempting to get lines now always gives empty list
  92. eq({}, buffer('get_lines', bufnr, 1, 3, 1))
  93. -- it's impossible to get out-of-bounds errors for an unloaded buffer
  94. eq({}, buffer('get_lines', bufnr, 8888, 9999, 1))
  95. end)
  96. end)
  97. describe('deprecated: {get,set,del}_line', function()
  98. it('works', function()
  99. eq('', curbuf_depr('get_line', 0))
  100. curbuf_depr('set_line', 0, 'line1')
  101. eq('line1', curbuf_depr('get_line', 0))
  102. curbuf_depr('set_line', 0, 'line2')
  103. eq('line2', curbuf_depr('get_line', 0))
  104. curbuf_depr('del_line', 0)
  105. eq('', curbuf_depr('get_line', 0))
  106. end)
  107. it('get_line: out-of-bounds is an error', function()
  108. curbuf_depr('set_line', 0, 'line1.a')
  109. eq(1, curbuf_depr('line_count')) -- sanity
  110. eq(false, pcall(curbuf_depr, 'get_line', 1))
  111. eq(false, pcall(curbuf_depr, 'get_line', -2))
  112. end)
  113. it('set_line, del_line: out-of-bounds is an error', function()
  114. curbuf_depr('set_line', 0, 'line1.a')
  115. eq(false, pcall(curbuf_depr, 'set_line', 1, 'line1.b'))
  116. eq(false, pcall(curbuf_depr, 'set_line', -2, 'line1.b'))
  117. eq(false, pcall(curbuf_depr, 'del_line', 2))
  118. eq(false, pcall(curbuf_depr, 'del_line', -3))
  119. end)
  120. it('can handle NULs', function()
  121. curbuf_depr('set_line', 0, 'ab\0cd')
  122. eq('ab\0cd', curbuf_depr('get_line', 0))
  123. end)
  124. end)
  125. describe('deprecated: {get,set}_line_slice', function()
  126. it('get_line_slice: out-of-bounds returns empty array', function()
  127. curbuf_depr('set_line_slice', 0, 0, true, true, {'a', 'b', 'c'})
  128. eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, 2, true, true)) --sanity
  129. eq({}, curbuf_depr('get_line_slice', 2, 3, false, true))
  130. eq({}, curbuf_depr('get_line_slice', 3, 9, true, true))
  131. eq({}, curbuf_depr('get_line_slice', 3, -1, true, true))
  132. eq({}, curbuf_depr('get_line_slice', -3, -4, false, true))
  133. eq({}, curbuf_depr('get_line_slice', -4, -5, true, true))
  134. end)
  135. it('set_line_slice: out-of-bounds extends past end', function()
  136. curbuf_depr('set_line_slice', 0, 0, true, true, {'a', 'b', 'c'})
  137. eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, 2, true, true)) --sanity
  138. eq({'c'}, curbuf_depr('get_line_slice', -1, 4, true, true))
  139. eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, 5, true, true))
  140. curbuf_depr('set_line_slice', 4, 5, true, true, {'d'})
  141. eq({'a', 'b', 'c', 'd'}, curbuf_depr('get_line_slice', 0, 5, true, true))
  142. curbuf_depr('set_line_slice', -4, -5, true, true, {'e'})
  143. eq({'e', 'a', 'b', 'c', 'd'}, curbuf_depr('get_line_slice', 0, 5, true, true))
  144. end)
  145. it('works', function()
  146. eq({''}, curbuf_depr('get_line_slice', 0, -1, true, true))
  147. -- Replace buffer
  148. curbuf_depr('set_line_slice', 0, -1, true, true, {'a', 'b', 'c'})
  149. eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, -1, true, true))
  150. eq({'b', 'c'}, curbuf_depr('get_line_slice', 1, -1, true, true))
  151. eq({'b'}, curbuf_depr('get_line_slice', 1, 2, true, false))
  152. eq({}, curbuf_depr('get_line_slice', 1, 1, true, false))
  153. eq({'a', 'b'}, curbuf_depr('get_line_slice', 0, -1, true, false))
  154. eq({'b'}, curbuf_depr('get_line_slice', 1, -1, true, false))
  155. eq({'b', 'c'}, curbuf_depr('get_line_slice', -2, -1, true, true))
  156. curbuf_depr('set_line_slice', 1, 2, true, false, {'a', 'b', 'c'})
  157. eq({'a', 'a', 'b', 'c', 'c'}, curbuf_depr('get_line_slice', 0, -1, true, true))
  158. curbuf_depr('set_line_slice', -1, -1, true, true, {'a', 'b', 'c'})
  159. eq({'a', 'a', 'b', 'c', 'a', 'b', 'c'},
  160. curbuf_depr('get_line_slice', 0, -1, true, true))
  161. curbuf_depr('set_line_slice', 0, -3, true, false, {})
  162. eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, -1, true, true))
  163. curbuf_depr('set_line_slice', 0, -1, true, true, {})
  164. eq({''}, curbuf_depr('get_line_slice', 0, -1, true, true))
  165. end)
  166. end)
  167. describe('nvim_buf_get_lines, nvim_buf_set_lines', function()
  168. local get_lines, set_lines = curbufmeths.get_lines, curbufmeths.set_lines
  169. local line_count = curbufmeths.line_count
  170. it('fails correctly when input is not valid', function()
  171. eq(1, curbufmeths.get_number())
  172. eq([[String cannot contain newlines]],
  173. pcall_err(bufmeths.set_lines, 1, 1, 2, false, {'b\na'}))
  174. end)
  175. it("fails if 'nomodifiable'", function()
  176. command('set nomodifiable')
  177. eq([[Buffer is not 'modifiable']],
  178. pcall_err(bufmeths.set_lines, 1, 1, 2, false, {'a','b'}))
  179. end)
  180. it('has correct line_count when inserting and deleting', function()
  181. eq(1, line_count())
  182. set_lines(-1, -1, true, {'line'})
  183. eq(2, line_count())
  184. set_lines(-1, -1, true, {'line'})
  185. eq(3, line_count())
  186. set_lines(-2, -1, true, {})
  187. eq(2, line_count())
  188. set_lines(-2, -1, true, {})
  189. set_lines(-2, -1, true, {})
  190. -- There's always at least one line
  191. eq(1, line_count())
  192. end)
  193. it('can get, set and delete a single line', function()
  194. eq({''}, get_lines(0, 1, true))
  195. set_lines(0, 1, true, {'line1'})
  196. eq({'line1'}, get_lines(0, 1, true))
  197. set_lines(0, 1, true, {'line2'})
  198. eq({'line2'}, get_lines(0, 1, true))
  199. set_lines(0, 1, true, {})
  200. eq({''}, get_lines(0, 1, true))
  201. end)
  202. it('can get a single line with strict indexing', function()
  203. set_lines(0, 1, true, {'line1.a'})
  204. eq(1, line_count()) -- sanity
  205. eq('Index out of bounds', pcall_err(get_lines, 1, 2, true))
  206. eq('Index out of bounds', pcall_err(get_lines, -3, -2, true))
  207. end)
  208. it('can get a single line with non-strict indexing', function()
  209. set_lines(0, 1, true, {'line1.a'})
  210. eq(1, line_count()) -- sanity
  211. eq({}, get_lines(1, 2, false))
  212. eq({}, get_lines(-3, -2, false))
  213. end)
  214. it('can set and delete a single line with strict indexing', function()
  215. set_lines(0, 1, true, {'line1.a'})
  216. eq('Index out of bounds', pcall_err(set_lines, 1, 2, true, {'line1.b'}))
  217. eq('Index out of bounds', pcall_err(set_lines, -3, -2, true, {'line1.c'}))
  218. eq({'line1.a'}, get_lines(0, -1, true))
  219. eq('Index out of bounds', pcall_err(set_lines, 1, 2, true, {}))
  220. eq('Index out of bounds', pcall_err(set_lines, -3, -2, true, {}))
  221. eq({'line1.a'}, get_lines(0, -1, true))
  222. end)
  223. it('can set and delete a single line with non-strict indexing', function()
  224. set_lines(0, 1, true, {'line1.a'})
  225. set_lines(1, 2, false, {'line1.b'})
  226. set_lines(-4, -3, false, {'line1.c'})
  227. eq({'line1.c', 'line1.a', 'line1.b'}, get_lines(0, -1, true))
  228. set_lines(3, 4, false, {})
  229. set_lines(-5, -4, false, {})
  230. eq({'line1.c', 'line1.a', 'line1.b'}, get_lines(0, -1, true))
  231. end)
  232. it('can handle NULs', function()
  233. set_lines(0, 1, true, {'ab\0cd'})
  234. eq({'ab\0cd'}, get_lines(0, -1, true))
  235. end)
  236. it('works with multiple lines', function()
  237. eq({''}, get_lines(0, -1, true))
  238. -- Replace buffer
  239. for _, mode in pairs({false, true}) do
  240. set_lines(0, -1, mode, {'a', 'b', 'c'})
  241. eq({'a', 'b', 'c'}, get_lines(0, -1, mode))
  242. eq({'b', 'c'}, get_lines(1, -1, mode))
  243. eq({'b'}, get_lines(1, 2, mode))
  244. eq({}, get_lines(1, 1, mode))
  245. eq({'a', 'b'}, get_lines(0, -2, mode))
  246. eq({'b'}, get_lines(1, -2, mode))
  247. eq({'b', 'c'}, get_lines(-3, -1, mode))
  248. set_lines(1, 2, mode, {'a', 'b', 'c'})
  249. eq({'a', 'a', 'b', 'c', 'c'}, get_lines(0, -1, mode))
  250. set_lines(-2, -1, mode, {'a', 'b', 'c'})
  251. eq({'a', 'a', 'b', 'c', 'a', 'b', 'c'},
  252. get_lines(0, -1, mode))
  253. set_lines(0, -4, mode, {})
  254. eq({'a', 'b', 'c'}, get_lines(0, -1, mode))
  255. set_lines(0, -1, mode, {})
  256. eq({''}, get_lines(0, -1, mode))
  257. end
  258. end)
  259. it('can get line ranges with non-strict indexing', function()
  260. set_lines(0, -1, true, {'a', 'b', 'c'})
  261. eq({'a', 'b', 'c'}, get_lines(0, -1, true)) --sanity
  262. eq({}, get_lines(3, 4, false))
  263. eq({}, get_lines(3, 10, false))
  264. eq({}, get_lines(-5, -5, false))
  265. eq({}, get_lines(3, -1, false))
  266. eq({}, get_lines(-3, -4, false))
  267. end)
  268. it('can get line ranges with strict indexing', function()
  269. set_lines(0, -1, true, {'a', 'b', 'c'})
  270. eq({'a', 'b', 'c'}, get_lines(0, -1, true)) --sanity
  271. eq('Index out of bounds', pcall_err(get_lines, 3, 4, true))
  272. eq('Index out of bounds', pcall_err(get_lines, 3, 10, true))
  273. eq('Index out of bounds', pcall_err(get_lines, -5, -5, true))
  274. -- empty or inverted ranges are not errors
  275. eq({}, get_lines(3, -1, true))
  276. eq({}, get_lines(-3, -4, true))
  277. end)
  278. it('set_lines: out-of-bounds can extend past end', function()
  279. set_lines(0, -1, true, {'a', 'b', 'c'})
  280. eq({'a', 'b', 'c'}, get_lines(0, -1, true)) --sanity
  281. eq({'c'}, get_lines(-2, 5, false))
  282. eq({'a', 'b', 'c'}, get_lines(0, 6, false))
  283. eq('Index out of bounds', pcall_err(set_lines, 4, 6, true, {'d'}))
  284. set_lines(4, 6, false, {'d'})
  285. eq({'a', 'b', 'c', 'd'}, get_lines(0, -1, true))
  286. eq('Index out of bounds', pcall_err(set_lines, -6, -6, true, {'e'}))
  287. set_lines(-6, -6, false, {'e'})
  288. eq({'e', 'a', 'b', 'c', 'd'}, get_lines(0, -1, true))
  289. end)
  290. it("set_lines on alternate buffer does not access invalid line (E315)", function()
  291. feed_command('set hidden')
  292. insert('Initial file')
  293. command('enew')
  294. insert([[
  295. More
  296. Lines
  297. Than
  298. In
  299. The
  300. Other
  301. Buffer]])
  302. feed_command('$')
  303. local retval = exc_exec("call nvim_buf_set_lines(1, 0, 1, v:false, ['test'])")
  304. eq(0, retval)
  305. end)
  306. it("set_lines of invisible buffer doesn't move cursor in current window", function()
  307. local screen = Screen.new(20, 5)
  308. screen:set_default_attr_ids({
  309. [1] = {bold = true, foreground = Screen.colors.Blue1},
  310. [2] = {bold = true},
  311. })
  312. screen:attach()
  313. insert([[
  314. Who would win?
  315. A real window
  316. with proper text]])
  317. local buf = meths.create_buf(false,true)
  318. screen:expect([[
  319. Who would win? |
  320. A real window |
  321. with proper tex^t |
  322. {1:~ }|
  323. |
  324. ]])
  325. meths.buf_set_lines(buf, 0, -1, true, {'or some', 'scratchy text'})
  326. feed('i') -- provoke redraw
  327. screen:expect([[
  328. Who would win? |
  329. A real window |
  330. with proper tex^t |
  331. {1:~ }|
  332. {2:-- INSERT --} |
  333. ]])
  334. end)
  335. it('set_lines on hidden buffer preserves "previous window" #9741', function()
  336. insert([[
  337. visible buffer line 1
  338. line 2
  339. ]])
  340. local hiddenbuf = meths.create_buf(false,true)
  341. command('vsplit')
  342. command('vsplit')
  343. feed('<c-w>l<c-w>l<c-w>l')
  344. eq(3, funcs.winnr())
  345. feed('<c-w>h')
  346. eq(2, funcs.winnr())
  347. meths.buf_set_lines(hiddenbuf, 0, -1, true,
  348. {'hidden buffer line 1', 'line 2'})
  349. feed('<c-w>p')
  350. eq(3, funcs.winnr())
  351. end)
  352. end)
  353. describe('nvim_buf_set_text', function()
  354. local get_lines, set_text = curbufmeths.get_lines, curbufmeths.set_text
  355. it('works', function()
  356. insert([[
  357. hello foo!
  358. text
  359. ]])
  360. eq({'hello foo!'}, get_lines(0, 1, true))
  361. -- can replace a single word
  362. set_text(0, 6, 0, 9, {'world'})
  363. eq({'hello world!', 'text'}, get_lines(0, 2, true))
  364. -- can insert text
  365. set_text(0, 0, 0, 0, {'well '})
  366. eq({'well hello world!', 'text'}, get_lines(0, 2, true))
  367. -- can delete text
  368. set_text(0, 0, 0, 5, {''})
  369. eq({'hello world!', 'text'}, get_lines(0, 2, true))
  370. -- can replace with multiple lines
  371. set_text(0, 6, 0, 11, {'foo', 'wo', 'more'})
  372. eq({'hello foo', 'wo', 'more!', 'text'}, get_lines(0, 4, true))
  373. -- will join multiple lines if needed
  374. set_text(0, 6, 3, 4, {'bar'})
  375. eq({'hello bar'}, get_lines(0, 1, true))
  376. -- can use negative line numbers
  377. set_text(-2, 0, -2, 5, {'goodbye'})
  378. eq({'goodbye bar', ''}, get_lines(0, -1, true))
  379. set_text(-1, 0, -1, 0, {'text'})
  380. eq({'goodbye bar', 'text'}, get_lines(0, 2, true))
  381. -- can append to a line
  382. set_text(1, 4, -1, 4, {' and', 'more'})
  383. eq({'goodbye bar', 'text and', 'more'}, get_lines(0, 3, true))
  384. end)
  385. it('works with undo', function()
  386. insert([[
  387. hello world!
  388. foo bar
  389. ]])
  390. -- setting text
  391. set_text(0, 0, 0, 0, {'well '})
  392. feed('u')
  393. eq({'hello world!'}, get_lines(0, 1, true))
  394. -- deleting text
  395. set_text(0, 0, 0, 6, {''})
  396. feed('u')
  397. eq({'hello world!'}, get_lines(0, 1, true))
  398. -- inserting newlines
  399. set_text(0, 0, 0, 0, {'hello', 'mr '})
  400. feed('u')
  401. eq({'hello world!'}, get_lines(0, 1, true))
  402. -- deleting newlines
  403. set_text(0, 0, 1, 4, {'hello'})
  404. feed('u')
  405. eq({'hello world!'}, get_lines(0, 1, true))
  406. end)
  407. it('updates the cursor position', function()
  408. insert([[
  409. hello world!
  410. ]])
  411. -- position the cursor on `!`
  412. curwin('set_cursor', {1, 11})
  413. -- replace 'world' with 'foo'
  414. set_text(0, 6, 0, 11, {'foo'})
  415. eq('hello foo!', curbuf_depr('get_line', 0))
  416. -- cursor should be moved left by two columns (replacement is shorter by 2 chars)
  417. eq({1, 9}, curwin('get_cursor'))
  418. end)
  419. it('can handle NULs', function()
  420. set_text(0, 0, 0, 0, {'ab\0cd'})
  421. eq('ab\0cd', curbuf_depr('get_line', 0))
  422. end)
  423. it('adjusts extmarks', function()
  424. local ns = request('nvim_create_namespace', "my-fancy-plugin")
  425. insert([[
  426. foo bar
  427. baz
  428. ]])
  429. local id1 = curbufmeths.set_extmark(ns, 0, 1, {})
  430. local id2 = curbufmeths.set_extmark(ns, 0, 7, {})
  431. local id3 = curbufmeths.set_extmark(ns, 1, 1, {})
  432. set_text(0, 4, 0, 7, {"q"})
  433. eq({'foo q', 'baz'}, get_lines(0, 2, true))
  434. -- mark before replacement point is unaffected
  435. eq({0, 1}, curbufmeths.get_extmark_by_id(ns, id1, {}))
  436. -- mark gets shifted back because the replacement was shorter
  437. eq({0, 5}, curbufmeths.get_extmark_by_id(ns, id2, {}))
  438. -- mark on the next line is unaffected
  439. eq({1, 1}, curbufmeths.get_extmark_by_id(ns, id3, {}))
  440. -- replacing the text spanning two lines will adjust the mark on the next line
  441. set_text(0, 3, 1, 3, {"qux"})
  442. eq({'fooqux', ''}, get_lines(0, 2, true))
  443. eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id3, {}))
  444. -- but mark before replacement point is still unaffected
  445. eq({0, 1}, curbufmeths.get_extmark_by_id(ns, id1, {}))
  446. -- and the mark in the middle was shifted to the end of the insertion
  447. eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id2, {}))
  448. -- marks should be put back into the same place after undoing
  449. set_text(0, 0, 0, 2, {''})
  450. feed('u')
  451. eq({0, 1}, curbufmeths.get_extmark_by_id(ns, id1, {}))
  452. eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id2, {}))
  453. eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id3, {}))
  454. -- marks should be shifted over by the correct number of bytes for multibyte
  455. -- chars
  456. set_text(0, 0, 0, 0, {'Ø'})
  457. eq({0, 3}, curbufmeths.get_extmark_by_id(ns, id1, {}))
  458. eq({0, 8}, curbufmeths.get_extmark_by_id(ns, id2, {}))
  459. eq({0, 8}, curbufmeths.get_extmark_by_id(ns, id3, {}))
  460. end)
  461. it("correctly marks changed region for redraw #13890", function()
  462. local screen = Screen.new(20, 5)
  463. screen:attach()
  464. insert([[
  465. AAA
  466. BBB
  467. ]])
  468. curbufmeths.set_text(0, 0, 1, 3, {'XXX', 'YYY'})
  469. screen:expect([[
  470. XXX |
  471. YYY |
  472. ^ |
  473. ~ |
  474. |
  475. ]])
  476. end)
  477. it('errors on out-of-range', function()
  478. insert([[
  479. hello foo!
  480. text]])
  481. eq('start_row out of bounds', pcall_err(set_text, 2, 0, 3, 0, {}))
  482. eq('start_row out of bounds', pcall_err(set_text, -3, 0, 0, 0, {}))
  483. eq('end_row out of bounds', pcall_err(set_text, 0, 0, 2, 0, {}))
  484. eq('end_row out of bounds', pcall_err(set_text, 0, 0, -3, 0, {}))
  485. eq('start_col out of bounds', pcall_err(set_text, 1, 5, 1, 5, {}))
  486. eq('end_col out of bounds', pcall_err(set_text, 1, 0, 1, 5, {}))
  487. end)
  488. it('errors when start is greater than end', function()
  489. insert([[
  490. hello foo!
  491. text]])
  492. eq('start is higher than end', pcall_err(set_text, 1, 0, 0, 0, {}))
  493. eq('start is higher than end', pcall_err(set_text, 0, 1, 0, 0, {}))
  494. end)
  495. it('no heap-use-after-free when called consecutively #19643', function()
  496. set_text(0, 0, 0, 0, {'one', '', '', 'two'})
  497. eq({'one', '', '', 'two'}, get_lines(0, 4, true))
  498. meths.win_set_cursor(0, {1, 0})
  499. exec_lua([[
  500. vim.api.nvim_buf_set_text(0, 0, 3, 1, 0, {''})
  501. vim.api.nvim_buf_set_text(0, 0, 3, 1, 0, {''})
  502. ]])
  503. eq({'one', 'two'}, get_lines(0, 2, true))
  504. end)
  505. end)
  506. describe('nvim_buf_get_text', function()
  507. local get_text = curbufmeths.get_text
  508. before_each(function()
  509. insert([[
  510. hello foo!
  511. text]])
  512. end)
  513. it('works', function()
  514. eq({'hello'}, get_text(0, 0, 0, 5, {}))
  515. eq({'hello foo!'}, get_text(0, 0, 0, 42, {}))
  516. eq({'foo!'}, get_text(0, 6, 0, 10, {}))
  517. eq({'foo!', 'tex'}, get_text(0, 6, 1, 3, {}))
  518. eq({'foo!', 'tex'}, get_text(-2, 6, -1, 3, {}))
  519. eq({''}, get_text(0, 18, 0, 20, {}))
  520. eq({'ext'}, get_text(-1, 1, -1, 4, {}))
  521. end)
  522. it('errors on out-of-range', function()
  523. eq('Index out of bounds', pcall_err(get_text, 2, 0, 3, 0, {}))
  524. eq('Index out of bounds', pcall_err(get_text, -3, 0, 0, 0, {}))
  525. eq('Index out of bounds', pcall_err(get_text, 0, 0, 2, 0, {}))
  526. eq('Index out of bounds', pcall_err(get_text, 0, 0, -3, 0, {}))
  527. -- no ml_get errors should happen #19017
  528. eq('', meths.get_vvar('errmsg'))
  529. end)
  530. it('errors when start is greater than end', function()
  531. eq('start is higher than end', pcall_err(get_text, 1, 0, 0, 0, {}))
  532. eq('start_col must be less than end_col', pcall_err(get_text, 0, 1, 0, 0, {}))
  533. end)
  534. end)
  535. describe('nvim_buf_get_offset', function()
  536. local get_offset = curbufmeths.get_offset
  537. it('works', function()
  538. curbufmeths.set_lines(0,-1,true,{'Some\r','exa\000mple', '', 'buf\rfer', 'text'})
  539. eq(5, curbufmeths.line_count())
  540. eq(0, get_offset(0))
  541. eq(6, get_offset(1))
  542. eq(15, get_offset(2))
  543. eq(16, get_offset(3))
  544. eq(24, get_offset(4))
  545. eq(29, get_offset(5))
  546. eq('Index out of bounds', pcall_err(get_offset, 6))
  547. eq('Index out of bounds', pcall_err(get_offset, -1))
  548. curbufmeths.set_option('eol', false)
  549. curbufmeths.set_option('fixeol', false)
  550. eq(28, get_offset(5))
  551. -- fileformat is ignored
  552. curbufmeths.set_option('fileformat', 'dos')
  553. eq(0, get_offset(0))
  554. eq(6, get_offset(1))
  555. eq(15, get_offset(2))
  556. eq(16, get_offset(3))
  557. eq(24, get_offset(4))
  558. eq(28, get_offset(5))
  559. curbufmeths.set_option('eol', true)
  560. eq(29, get_offset(5))
  561. command("set hidden")
  562. command("enew")
  563. eq(6, bufmeths.get_offset(1,1))
  564. command("bunload! 1")
  565. eq(-1, bufmeths.get_offset(1,1))
  566. end)
  567. end)
  568. describe('nvim_buf_get_var, nvim_buf_set_var, nvim_buf_del_var', function()
  569. it('works', function()
  570. curbuf('set_var', 'lua', {1, 2, {['3'] = 1}})
  571. eq({1, 2, {['3'] = 1}}, curbuf('get_var', 'lua'))
  572. eq({1, 2, {['3'] = 1}}, nvim('eval', 'b:lua'))
  573. eq(1, funcs.exists('b:lua'))
  574. curbufmeths.del_var('lua')
  575. eq(0, funcs.exists('b:lua'))
  576. eq( 'Key not found: lua', pcall_err(curbufmeths.del_var, 'lua'))
  577. curbufmeths.set_var('lua', 1)
  578. command('lockvar b:lua')
  579. eq('Key is locked: lua', pcall_err(curbufmeths.del_var, 'lua'))
  580. eq('Key is locked: lua', pcall_err(curbufmeths.set_var, 'lua', 1))
  581. eq('Key is read-only: changedtick',
  582. pcall_err(curbufmeths.del_var, 'changedtick'))
  583. eq('Key is read-only: changedtick',
  584. pcall_err(curbufmeths.set_var, 'changedtick', 1))
  585. end)
  586. end)
  587. describe('nvim_buf_get_changedtick', function()
  588. it('works', function()
  589. eq(2, curbufmeths.get_changedtick())
  590. curbufmeths.set_lines(0, 1, false, {'abc\0', '\0def', 'ghi'})
  591. eq(3, curbufmeths.get_changedtick())
  592. eq(3, curbufmeths.get_var('changedtick'))
  593. end)
  594. it('buffer_set_var returns the old value', function()
  595. local val1 = {1, 2, {['3'] = 1}}
  596. local val2 = {4, 7}
  597. eq(NIL, request('buffer_set_var', 0, 'lua', val1))
  598. eq(val1, request('buffer_set_var', 0, 'lua', val2))
  599. end)
  600. it('buffer_del_var returns the old value', function()
  601. local val1 = {1, 2, {['3'] = 1}}
  602. local val2 = {4, 7}
  603. eq(NIL, request('buffer_set_var', 0, 'lua', val1))
  604. eq(val1, request('buffer_set_var', 0, 'lua', val2))
  605. eq(val2, request('buffer_del_var', 0, 'lua'))
  606. end)
  607. end)
  608. describe('nvim_buf_get_option, nvim_buf_set_option', function()
  609. it('works', function()
  610. eq(8, curbuf('get_option', 'shiftwidth'))
  611. curbuf('set_option', 'shiftwidth', 4)
  612. eq(4, curbuf('get_option', 'shiftwidth'))
  613. -- global-local option
  614. curbuf('set_option', 'define', 'test')
  615. eq('test', curbuf('get_option', 'define'))
  616. -- Doesn't change the global value
  617. eq([[^\s*#\s*define]], nvim('get_option', 'define'))
  618. end)
  619. it('returns values for unset local options', function()
  620. -- 'undolevels' is only set to its "unset" value when a new buffer is
  621. -- created
  622. command('enew')
  623. eq(-123456, curbuf('get_option', 'undolevels'))
  624. end)
  625. end)
  626. describe('nvim_buf_get_name, nvim_buf_set_name', function()
  627. it('works', function()
  628. nvim('command', 'new')
  629. eq('', curbuf('get_name'))
  630. local new_name = nvim('eval', 'resolve(tempname())')
  631. curbuf('set_name', new_name)
  632. eq(new_name, curbuf('get_name'))
  633. nvim('command', 'w!')
  634. eq(1, funcs.filereadable(new_name))
  635. os.remove(new_name)
  636. end)
  637. end)
  638. describe('nvim_buf_is_loaded', function()
  639. it('works', function()
  640. -- record our buffer number for when we unload it
  641. local bufnr = curbuf('get_number')
  642. -- api should report that the buffer is loaded
  643. ok(buffer('is_loaded', bufnr))
  644. -- hide the current buffer by switching to a new empty buffer
  645. -- Careful! we need to modify the buffer first or vim will just reuse it
  646. buffer('set_lines', bufnr, 0, -1, 1, {'line1'})
  647. command('hide enew')
  648. -- confirm the buffer is hidden, but still loaded
  649. local infolist = nvim('eval', 'getbufinfo('..bufnr..')')
  650. eq(1, #infolist)
  651. eq(1, infolist[1].hidden)
  652. eq(1, infolist[1].loaded)
  653. -- now force unload the buffer
  654. command('bunload! '..bufnr)
  655. -- confirm the buffer is unloaded
  656. infolist = nvim('eval', 'getbufinfo('..bufnr..')')
  657. eq(0, infolist[1].loaded)
  658. -- nvim_buf_is_loaded() should also report the buffer as unloaded
  659. eq(false, buffer('is_loaded', bufnr))
  660. end)
  661. end)
  662. describe('nvim_buf_is_valid', function()
  663. it('works', function()
  664. nvim('command', 'new')
  665. local b = nvim('get_current_buf')
  666. ok(buffer('is_valid', b))
  667. nvim('command', 'bw!')
  668. ok(not buffer('is_valid', b))
  669. end)
  670. end)
  671. describe('nvim_buf_delete', function()
  672. it('allows for just deleting', function()
  673. nvim('command', 'new')
  674. local b = nvim('get_current_buf')
  675. ok(buffer('is_valid', b))
  676. nvim('buf_delete', b, {})
  677. ok(not buffer('is_loaded', b))
  678. ok(not buffer('is_valid', b))
  679. end)
  680. it('allows for just unloading', function()
  681. nvim('command', 'new')
  682. local b = nvim('get_current_buf')
  683. ok(buffer('is_valid', b))
  684. nvim('buf_delete', b, { unload = true })
  685. ok(not buffer('is_loaded', b))
  686. ok(buffer('is_valid', b))
  687. end)
  688. end)
  689. describe('nvim_buf_get_mark', function()
  690. it('works', function()
  691. curbuf('set_lines', -1, -1, true, {'a', 'bit of', 'text'})
  692. curwin('set_cursor', {3, 4})
  693. nvim('command', 'mark v')
  694. eq({3, 0}, curbuf('get_mark', 'v'))
  695. end)
  696. end)
  697. describe('nvim_buf_set_mark', function()
  698. it('works with buffer local marks', function()
  699. curbufmeths.set_lines(-1, -1, true, {'a', 'bit of', 'text'})
  700. eq(true, curbufmeths.set_mark('z', 1, 1, {}))
  701. eq({1, 1}, curbufmeths.get_mark('z'))
  702. end)
  703. it('works with file/uppercase marks', function()
  704. curbufmeths.set_lines(-1, -1, true, {'a', 'bit of', 'text'})
  705. eq(true, curbufmeths.set_mark('Z', 3, 1, {}))
  706. eq({3, 1}, curbufmeths.get_mark('Z'))
  707. end)
  708. it('fails when invalid marks names are used', function()
  709. eq(false, pcall(curbufmeths.set_mark, '!', 1, 0, {}))
  710. eq(false, pcall(curbufmeths.set_mark, 'fail', 1, 0, {}))
  711. end)
  712. it('fails when invalid buffer number is used', function()
  713. eq(false, pcall(meths.buf_set_mark, 99, 'a', 1, 1, {}))
  714. end)
  715. end)
  716. describe('nvim_buf_del_mark', function()
  717. it('works with buffer local marks', function()
  718. curbufmeths.set_lines(-1, -1, true, {'a', 'bit of', 'text'})
  719. curbufmeths.set_mark('z', 3, 1, {})
  720. eq(true, curbufmeths.del_mark('z'))
  721. eq({0, 0}, curbufmeths.get_mark('z'))
  722. end)
  723. it('works with file/uppercase marks', function()
  724. curbufmeths.set_lines(-1, -1, true, {'a', 'bit of', 'text'})
  725. curbufmeths.set_mark('Z', 3, 3, {})
  726. eq(true, curbufmeths.del_mark('Z'))
  727. eq({0, 0}, curbufmeths.get_mark('Z'))
  728. end)
  729. it('returns false in marks not set in this buffer', function()
  730. local abuf = meths.create_buf(false,true)
  731. bufmeths.set_lines(abuf, -1, -1, true, {'a', 'bit of', 'text'})
  732. bufmeths.set_mark(abuf, 'A', 2, 2, {})
  733. eq(false, curbufmeths.del_mark('A'))
  734. eq({2, 2}, bufmeths.get_mark(abuf, 'A'))
  735. end)
  736. it('returns false if mark was not deleted', function()
  737. curbufmeths.set_lines(-1, -1, true, {'a', 'bit of', 'text'})
  738. curbufmeths.set_mark('z', 3, 1, {})
  739. eq(true, curbufmeths.del_mark('z'))
  740. eq(false, curbufmeths.del_mark('z')) -- Mark was already deleted
  741. end)
  742. it('fails when invalid marks names are used', function()
  743. eq(false, pcall(curbufmeths.del_mark, '!'))
  744. eq(false, pcall(curbufmeths.del_mark, 'fail'))
  745. end)
  746. it('fails when invalid buffer number is used', function()
  747. eq(false, pcall(meths.buf_del_mark, 99, 'a'))
  748. end)
  749. end)
  750. end)