buffer_spec.lua 30 KB

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