searchhl_spec.lua 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local Screen = require('test.functional.ui.screen')
  3. local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert
  4. local command = helpers.command
  5. local feed_command = helpers.feed_command
  6. local eq = helpers.eq
  7. local eval = helpers.eval
  8. local funcs = helpers.funcs
  9. local testprg = helpers.testprg
  10. describe('search highlighting', function()
  11. local screen
  12. before_each(function()
  13. clear()
  14. screen = Screen.new(40, 7)
  15. screen:attach()
  16. screen:set_default_attr_ids( {
  17. [1] = {bold=true, foreground=Screen.colors.Blue},
  18. [2] = {background = Screen.colors.Yellow}, -- Search
  19. [3] = {reverse = true},
  20. [4] = {foreground = Screen.colors.Red}, -- Message
  21. [6] = {foreground = Screen.colors.Blue4, background = Screen.colors.LightGrey}, -- Folded
  22. })
  23. end)
  24. it('is disabled by ":set nohlsearch"', function()
  25. feed_command('set nohlsearch')
  26. insert("some text\nmore text")
  27. feed("gg/text<cr>")
  28. screen:expect([[
  29. some ^text |
  30. more text |
  31. {1:~ }|
  32. {1:~ }|
  33. {1:~ }|
  34. {1:~ }|
  35. /text |
  36. ]])
  37. end)
  38. it('is disabled in folded text', function()
  39. insert("some text\nmore text")
  40. feed_command('1,2fold')
  41. feed("gg/text")
  42. screen:expect{grid=[[
  43. {6:+-- 2 lines: some text·················}|
  44. {1:~ }|
  45. {1:~ }|
  46. {1:~ }|
  47. {1:~ }|
  48. {1:~ }|
  49. /text^ |
  50. ]], win_viewport={
  51. [2] = {win = {id = 1000}, topline = 0, botline = 3, curline = 0, curcol = 9, linecount = 2, sum_scroll_delta = 0};
  52. }}
  53. end)
  54. local function test_search_hl()
  55. insert([[
  56. some text
  57. more textstuff
  58. stupidtexttextstuff
  59. a text word
  60. ]])
  61. -- 'hlsearch' is enabled by default. #2859
  62. feed("gg/text<cr>")
  63. screen:expect([[
  64. some {2:^text} |
  65. more {2:text}stuff |
  66. stupid{2:texttext}stuff |
  67. a {2:text} word |
  68. |
  69. {1:~ }|
  70. /text |
  71. ]])
  72. -- overlapping matches not allowed
  73. feed("3nx")
  74. screen:expect([[
  75. some {2:text} |
  76. more {2:text}stuff |
  77. stupid{2:text}^extstuff |
  78. a {2:text} word |
  79. |
  80. {1:~ }|
  81. /text |
  82. ]])
  83. feed("ggn*") -- search for entire word
  84. screen:expect([[
  85. some {2:text} |
  86. more textstuff |
  87. stupidtextextstuff |
  88. a {2:^text} word |
  89. |
  90. {1:~ }|
  91. /\<text\> |
  92. ]])
  93. feed_command("nohlsearch")
  94. screen:expect([[
  95. some text |
  96. more textstuff |
  97. stupidtextextstuff |
  98. a ^text word |
  99. |
  100. {1:~ }|
  101. :nohlsearch |
  102. ]])
  103. end
  104. it("works when 'winhighlight' is not set", function()
  105. test_search_hl()
  106. end)
  107. it("works when 'winhighlight' doesn't change Search highlight", function()
  108. command('setlocal winhl=NonText:Underlined')
  109. local attrs = screen:get_default_attr_ids()
  110. attrs[1] = {foreground = Screen.colors.SlateBlue, underline = true}
  111. screen:set_default_attr_ids(attrs)
  112. test_search_hl()
  113. end)
  114. it("works when 'winhighlight' changes Search highlight", function()
  115. command('setlocal winhl=Search:Underlined')
  116. local attrs = screen:get_default_attr_ids()
  117. attrs[2] = {foreground = Screen.colors.SlateBlue, underline = true}
  118. screen:set_default_attr_ids(attrs)
  119. test_search_hl()
  120. end)
  121. describe('CurSearch highlight', function()
  122. before_each(function()
  123. screen:set_default_attr_ids({
  124. [1] = {background = Screen.colors.Yellow}, -- Search
  125. [2] = {foreground = Screen.colors.White, background = Screen.colors.Black}, -- CurSearch
  126. [3] = {foreground = Screen.colors.Red}, -- WarningMsg
  127. })
  128. command('highlight CurSearch guibg=Black guifg=White')
  129. end)
  130. it('works for match under cursor', function()
  131. insert([[
  132. There is no way that a bee should be
  133. able to fly. Its wings are too small
  134. to get its fat little body off the
  135. ground. The bee, of course, flies
  136. anyway because bees don't care what
  137. humans think is impossible.]])
  138. feed('/bee<CR>')
  139. screen:expect{grid=[[
  140. There is no way that a {2:^bee} should be |
  141. able to fly. Its wings are too small |
  142. to get its fat little body off the |
  143. ground. The {1:bee}, of course, flies |
  144. anyway because {1:bee}s don't care what |
  145. humans think is impossible. |
  146. {3:search hit BOTTOM, continuing at TOP} |
  147. ]]}
  148. feed('nn')
  149. screen:expect{grid=[[
  150. There is no way that a {1:bee} should be |
  151. able to fly. Its wings are too small |
  152. to get its fat little body off the |
  153. ground. The {1:bee}, of course, flies |
  154. anyway because {2:^bee}s don't care what |
  155. humans think is impossible. |
  156. /bee |
  157. ]]}
  158. feed('N')
  159. screen:expect{grid=[[
  160. There is no way that a {1:bee} should be |
  161. able to fly. Its wings are too small |
  162. to get its fat little body off the |
  163. ground. The {2:^bee}, of course, flies |
  164. anyway because {1:bee}s don't care what |
  165. humans think is impossible. |
  166. ?bee |
  167. ]]}
  168. end)
  169. it('works for multiline match', function()
  170. command([[call setline(1, ['one', 'foo', 'bar', 'baz', 'foo the foo and foo', 'bar'])]])
  171. feed('gg/foo<CR>')
  172. screen:expect([[
  173. one |
  174. {2:^foo} |
  175. bar |
  176. baz |
  177. {1:foo} the {1:foo} and {1:foo} |
  178. bar |
  179. /foo |
  180. ]])
  181. feed('n')
  182. screen:expect([[
  183. one |
  184. {1:foo} |
  185. bar |
  186. baz |
  187. {2:^foo} the {1:foo} and {1:foo} |
  188. bar |
  189. /foo |
  190. ]])
  191. feed('n')
  192. screen:expect([[
  193. one |
  194. {1:foo} |
  195. bar |
  196. baz |
  197. {1:foo} the {2:^foo} and {1:foo} |
  198. bar |
  199. /foo |
  200. ]])
  201. feed('n')
  202. screen:expect([[
  203. one |
  204. {1:foo} |
  205. bar |
  206. baz |
  207. {1:foo} the {1:foo} and {2:^foo} |
  208. bar |
  209. /foo |
  210. ]])
  211. command([[call setline(5, 'foo')]])
  212. feed('0?<CR>')
  213. screen:expect([[
  214. one |
  215. {2:^foo} |
  216. bar |
  217. baz |
  218. {1:foo} |
  219. bar |
  220. ?foo |
  221. ]])
  222. feed('gg/foo\\nbar<CR>')
  223. screen:expect([[
  224. one |
  225. {2:^foo } |
  226. {2:bar} |
  227. baz |
  228. {1:foo } |
  229. {1:bar} |
  230. /foo\nbar |
  231. ]])
  232. command([[call setline(1, ['---', 'abcdefg', 'hijkl', '---', 'abcdefg', 'hijkl'])]])
  233. feed('gg/efg\\nhij<CR>')
  234. screen:expect([[
  235. --- |
  236. abcd{2:^efg } |
  237. {2:hij}kl |
  238. --- |
  239. abcd{1:efg } |
  240. {1:hij}kl |
  241. /efg\nhij |
  242. ]])
  243. feed('n')
  244. screen:expect([[
  245. --- |
  246. abcd{1:efg } |
  247. {1:hij}kl |
  248. --- |
  249. abcd{2:^efg } |
  250. {2:hij}kl |
  251. /efg\nhij |
  252. ]])
  253. end)
  254. end)
  255. it('highlights after EOL', function()
  256. insert("\n\n\n\n\n\n")
  257. feed("gg/^<cr>")
  258. screen:expect([[
  259. {2: } |
  260. {2:^ } |
  261. {2: } |
  262. {2: } |
  263. {2: } |
  264. {2: } |
  265. /^ |
  266. ]])
  267. -- Test that highlights are preserved after moving the cursor.
  268. feed("j")
  269. screen:expect([[
  270. {2: } |
  271. {2: } |
  272. {2:^ } |
  273. {2: } |
  274. {2: } |
  275. {2: } |
  276. /^ |
  277. ]])
  278. -- Repeat the test in rightleft mode.
  279. command("nohlsearch")
  280. command("set rightleft")
  281. feed("gg/^<cr>")
  282. screen:expect([[
  283. {2: }|
  284. {2:^ }|
  285. {2: }|
  286. {2: }|
  287. {2: }|
  288. {2: }|
  289. ^/ |
  290. ]])
  291. feed("j")
  292. screen:expect([[
  293. {2: }|
  294. {2: }|
  295. {2:^ }|
  296. {2: }|
  297. {2: }|
  298. {2: }|
  299. ^/ |
  300. ]])
  301. end)
  302. it('is preserved during :terminal activity', function()
  303. feed((':terminal "%s" REP 5000 foo<cr>'):format(testprg('shell-test')))
  304. feed(':file term<CR>')
  305. feed('G') -- Follow :terminal output.
  306. feed(':vnew<CR>')
  307. insert([[
  308. foo bar baz
  309. bar baz foo
  310. bar foo baz
  311. ]])
  312. feed('/foo')
  313. helpers.poke_eventloop()
  314. screen:sleep(0)
  315. screen:expect_unchanged()
  316. end)
  317. it('works with incsearch', function()
  318. command('set hlsearch')
  319. command('set incsearch')
  320. command('set laststatus=0')
  321. insert([[
  322. the first line
  323. in a little file]])
  324. command('vsplit')
  325. feed("gg/li")
  326. screen:expect([[
  327. the first {3:li}ne │the first {2:li}ne |
  328. in a {2:li}ttle file │in a {2:li}ttle file |
  329. {1:~ }│{1:~ }|
  330. {1:~ }│{1:~ }|
  331. {1:~ }│{1:~ }|
  332. {1:~ }│{1:~ }|
  333. /li^ |
  334. ]])
  335. -- check that consecutive matches are caught by C-g/C-t
  336. feed("<C-g>")
  337. screen:expect([[
  338. the first {2:li}ne │the first {2:li}ne |
  339. in a {3:li}ttle file │in a {2:li}ttle file |
  340. {1:~ }│{1:~ }|
  341. {1:~ }│{1:~ }|
  342. {1:~ }│{1:~ }|
  343. {1:~ }│{1:~ }|
  344. /li^ |
  345. ]])
  346. feed("<C-t>")
  347. screen:expect([[
  348. the first {3:li}ne │the first {2:li}ne |
  349. in a {2:li}ttle file │in a {2:li}ttle file |
  350. {1:~ }│{1:~ }|
  351. {1:~ }│{1:~ }|
  352. {1:~ }│{1:~ }|
  353. {1:~ }│{1:~ }|
  354. /li^ |
  355. ]])
  356. feed("t")
  357. screen:expect([[
  358. the first line │the first line |
  359. in a {3:lit}tle file │in a {2:lit}tle file |
  360. {1:~ }│{1:~ }|
  361. {1:~ }│{1:~ }|
  362. {1:~ }│{1:~ }|
  363. {1:~ }│{1:~ }|
  364. /lit^ |
  365. ]])
  366. feed("<cr>")
  367. screen:expect([[
  368. the first line │the first line |
  369. in a {2:^lit}tle file │in a {2:lit}tle file |
  370. {1:~ }│{1:~ }|
  371. {1:~ }│{1:~ }|
  372. {1:~ }│{1:~ }|
  373. {1:~ }│{1:~ }|
  374. /lit |
  375. ]])
  376. feed("/fir")
  377. screen:expect([[
  378. the {3:fir}st line │the {2:fir}st line |
  379. in a little file │in a little file |
  380. {1:~ }│{1:~ }|
  381. {1:~ }│{1:~ }|
  382. {1:~ }│{1:~ }|
  383. {1:~ }│{1:~ }|
  384. /fir^ |
  385. ]])
  386. -- incsearch have priority over hlsearch
  387. feed("<esc>/ttle")
  388. screen:expect([[
  389. the first line │the first line |
  390. in a li{3:ttle} file │in a li{2:ttle} file |
  391. {1:~ }│{1:~ }|
  392. {1:~ }│{1:~ }|
  393. {1:~ }│{1:~ }|
  394. {1:~ }│{1:~ }|
  395. /ttle^ |
  396. ]])
  397. -- cancelling search resets to the old search term
  398. feed('<esc>')
  399. screen:expect([[
  400. the first line │the first line |
  401. in a {2:^lit}tle file │in a {2:lit}tle file |
  402. {1:~ }│{1:~ }|
  403. {1:~ }│{1:~ }|
  404. {1:~ }│{1:~ }|
  405. {1:~ }│{1:~ }|
  406. |
  407. ]])
  408. eq('lit', eval('@/'))
  409. -- cancelling inc search restores the hl state
  410. feed(':noh<cr>')
  411. screen:expect([[
  412. the first line │the first line |
  413. in a ^little file │in a little file |
  414. {1:~ }│{1:~ }|
  415. {1:~ }│{1:~ }|
  416. {1:~ }│{1:~ }|
  417. {1:~ }│{1:~ }|
  418. :noh |
  419. ]])
  420. feed('/first')
  421. screen:expect([[
  422. the {3:first} line │the {2:first} line |
  423. in a little file │in a little file |
  424. {1:~ }│{1:~ }|
  425. {1:~ }│{1:~ }|
  426. {1:~ }│{1:~ }|
  427. {1:~ }│{1:~ }|
  428. /first^ |
  429. ]])
  430. feed('<esc>')
  431. screen:expect([[
  432. the first line │the first line |
  433. in a ^little file │in a little file |
  434. {1:~ }│{1:~ }|
  435. {1:~ }│{1:~ }|
  436. {1:~ }│{1:~ }|
  437. {1:~ }│{1:~ }|
  438. |
  439. ]])
  440. -- test that pressing C-g in an empty command line does not move the cursor
  441. feed('gg0')
  442. command([[let @/ = 'i']])
  443. -- moves to next match of previous search pattern, just like /<cr>
  444. feed('/<c-g><cr>')
  445. eq({0, 1, 6, 0}, funcs.getpos('.'))
  446. -- moves to next match of previous search pattern, just like /<cr>
  447. feed('/<cr>')
  448. eq({0, 1, 12, 0}, funcs.getpos('.'))
  449. -- moves to next match of previous search pattern, just like /<cr>
  450. feed('/<c-t><cr>')
  451. eq({0, 2, 1, 0}, funcs.getpos('.'))
  452. -- 8.0.1304, test that C-g and C-t works with incsearch and empty pattern
  453. feed('<esc>/fi<CR>')
  454. screen:expect([[
  455. the {2:fi}rst line │the {2:fi}rst line |
  456. in a little {2:^fi}le │in a little {2:fi}le |
  457. {1:~ }│{1:~ }|
  458. {1:~ }│{1:~ }|
  459. {1:~ }│{1:~ }|
  460. {1:~ }│{1:~ }|
  461. /fi |
  462. ]])
  463. feed('//')
  464. screen:expect([[
  465. the {3:fi}rst line │the {2:fi}rst line |
  466. in a little {2:fi}le │in a little {2:fi}le |
  467. {1:~ }│{1:~ }|
  468. {1:~ }│{1:~ }|
  469. {1:~ }│{1:~ }|
  470. {1:~ }│{1:~ }|
  471. //^ |
  472. ]])
  473. feed('<C-g>')
  474. screen:expect([[
  475. the {2:fi}rst line │the {2:fi}rst line |
  476. in a little {3:fi}le │in a little {2:fi}le |
  477. {1:~ }│{1:~ }|
  478. {1:~ }│{1:~ }|
  479. {1:~ }│{1:~ }|
  480. {1:~ }│{1:~ }|
  481. //^ |
  482. ]])
  483. feed('<Esc>')
  484. -- incsearch works after c_CTRL-R_CTRL-R
  485. command('let @" = "file"')
  486. feed('/<C-R><C-R>"')
  487. screen:expect([[
  488. the first line │the first line |
  489. in a little {3:file} │in a little {2:file} |
  490. {1:~ }│{1:~ }|
  491. {1:~ }│{1:~ }|
  492. {1:~ }│{1:~ }|
  493. {1:~ }│{1:~ }|
  494. /file^ |
  495. ]])
  496. end)
  497. it('works with incsearch and offset', function()
  498. feed_command('set hlsearch')
  499. feed_command('set incsearch')
  500. insert([[
  501. not the match you're looking for
  502. the match is here]])
  503. feed("gg/mat/e")
  504. screen:expect([[
  505. not the {3:mat}ch you're looking for |
  506. the {2:mat}ch is here |
  507. {1:~ }|
  508. {1:~ }|
  509. {1:~ }|
  510. {1:~ }|
  511. /mat/e^ |
  512. ]])
  513. -- Search with count and /e offset fixed in Vim patch 7.4.532.
  514. feed("<esc>2/mat/e")
  515. screen:expect([[
  516. not the {2:mat}ch you're looking for |
  517. the {3:mat}ch is here |
  518. {1:~ }|
  519. {1:~ }|
  520. {1:~ }|
  521. {1:~ }|
  522. /mat/e^ |
  523. ]])
  524. feed("<cr>")
  525. screen:expect([[
  526. not the {2:mat}ch you're looking for |
  527. the {2:ma^t}ch is here |
  528. {1:~ }|
  529. {1:~ }|
  530. {1:~ }|
  531. {1:~ }|
  532. /mat/e |
  533. ]])
  534. end)
  535. it('works with multiline regexps', function()
  536. feed_command('set hlsearch')
  537. feed('4oa repeated line<esc>')
  538. feed('/line\\na<cr>')
  539. screen:expect([[
  540. |
  541. a repeated {2:^line } |
  542. {2:a} repeated {2:line } |
  543. {2:a} repeated {2:line } |
  544. {2:a} repeated line |
  545. {1:~ }|
  546. {4:search hit BOTTOM, continuing at TOP} |
  547. ]])
  548. -- it redraws rows above the changed one
  549. feed('4Grb')
  550. screen:expect([[
  551. |
  552. a repeated {2:line } |
  553. {2:a} repeated line |
  554. ^b repeated {2:line } |
  555. {2:a} repeated line |
  556. {1:~ }|
  557. {4:search hit BOTTOM, continuing at TOP} |
  558. ]])
  559. end)
  560. it('works with matchadd and syntax', function()
  561. screen:set_default_attr_ids {
  562. [1] = {bold=true, foreground=Screen.colors.Blue};
  563. [2] = {background = Screen.colors.Yellow};
  564. [3] = {reverse = true};
  565. [4] = {foreground = Screen.colors.Red};
  566. [5] = {bold = true, background = Screen.colors.Green};
  567. [6] = {italic = true, background = Screen.colors.Magenta};
  568. [7] = {bold = true, background = Screen.colors.Yellow};
  569. [8] = {foreground = Screen.colors.Blue4, background = Screen.colors.LightGray};
  570. }
  571. feed_command('set hlsearch')
  572. insert [[
  573. very special text
  574. ]]
  575. feed_command("syntax on")
  576. feed_command("highlight MyGroup guibg=Green gui=bold")
  577. feed_command("highlight MyGroup2 guibg=Magenta gui=italic")
  578. feed_command("call matchadd('MyGroup', 'special')")
  579. feed_command("call matchadd('MyGroup2', 'text', 0)")
  580. -- searchhl and matchadd matches are exclusive, only the highest priority
  581. -- is used (and matches with lower priorities are not combined)
  582. feed_command("/ial te")
  583. screen:expect{grid=[[
  584. very {5:spec^ial}{2: te}{6:xt} |
  585. |
  586. {1:~ }|
  587. {1:~ }|
  588. {1:~ }|
  589. {1:~ }|
  590. {4:search hit BOTTOM, continuing at TOP} |
  591. ]], win_viewport={
  592. [2] = {win = {id = 1000}, topline = 0, botline = 3, curline = 0, curcol = 11, linecount = 2, sum_scroll_delta = 0};
  593. }}
  594. -- check highlights work also in folds
  595. feed("zf4j")
  596. screen:expect{grid=[[
  597. {8:^+-- 2 lines: very special text·········}|
  598. {1:~ }|
  599. {1:~ }|
  600. {1:~ }|
  601. {1:~ }|
  602. {1:~ }|
  603. {4:search hit BOTTOM, continuing at TOP} |
  604. ]]}
  605. command("%foldopen")
  606. screen:expect([[
  607. very {5:spec^ial}{2: te}{6:xt} |
  608. |
  609. {1:~ }|
  610. {1:~ }|
  611. {1:~ }|
  612. {1:~ }|
  613. {4:search hit BOTTOM, continuing at TOP} |
  614. ]])
  615. feed_command("call clearmatches()")
  616. screen:expect([[
  617. very spec{2:^ial te}xt |
  618. |
  619. {1:~ }|
  620. {1:~ }|
  621. {1:~ }|
  622. {1:~ }|
  623. :call clearmatches() |
  624. ]])
  625. -- searchhl has priority over syntax, but in this case
  626. -- nonconflicting attributes are combined
  627. feed_command("syntax keyword MyGroup special")
  628. screen:expect([[
  629. very {5:spec}{7:^ial}{2: te}xt |
  630. |
  631. {1:~ }|
  632. {1:~ }|
  633. {1:~ }|
  634. {1:~ }|
  635. :syntax keyword MyGroup special |
  636. ]])
  637. end)
  638. it('highlights entire pattern on :%g@a/b', function()
  639. command('set inccommand=nosplit')
  640. feed('ia/b/c<Esc>')
  641. feed(':%g@a/b')
  642. screen:expect([[
  643. {3:a/b}/c |
  644. {1:~ }|
  645. {1:~ }|
  646. {1:~ }|
  647. {1:~ }|
  648. {1:~ }|
  649. :%g@a/b^ |
  650. ]])
  651. end)
  652. end)