highlight_spec.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local clear, nvim = helpers.clear, helpers.nvim
  3. local Screen = require('test.functional.ui.screen')
  4. local eq, eval = helpers.eq, helpers.eval
  5. local command = helpers.command
  6. local exec_capture = helpers.exec_capture
  7. local meths = helpers.meths
  8. local funcs = helpers.funcs
  9. local pcall_err = helpers.pcall_err
  10. local ok = helpers.ok
  11. local assert_alive = helpers.assert_alive
  12. describe('API: highlight',function()
  13. local expected_rgb = {
  14. background = Screen.colors.Yellow,
  15. foreground = Screen.colors.Red,
  16. special = Screen.colors.Blue,
  17. bold = true,
  18. }
  19. local expected_cterm = {
  20. background = 10,
  21. underline = true,
  22. }
  23. local expected_rgb2 = {
  24. background = Screen.colors.Yellow,
  25. foreground = Screen.colors.Red,
  26. special = Screen.colors.Blue,
  27. bold = true,
  28. italic = true,
  29. reverse = true,
  30. underline = true,
  31. underlineline = true,
  32. undercurl = true,
  33. underdot = true,
  34. underdash = true,
  35. strikethrough = true,
  36. }
  37. before_each(function()
  38. clear()
  39. command("hi NewHighlight cterm=underline ctermbg=green guifg=red guibg=yellow guisp=blue gui=bold")
  40. end)
  41. it("nvim_get_hl_by_id", function()
  42. local hl_id = eval("hlID('NewHighlight')")
  43. eq(expected_cterm, nvim("get_hl_by_id", hl_id, false))
  44. hl_id = eval("hlID('NewHighlight')")
  45. -- Test valid id.
  46. eq(expected_rgb, nvim("get_hl_by_id", hl_id, true))
  47. -- Test invalid id.
  48. local err, emsg = pcall(meths.get_hl_by_id, 30000, false)
  49. eq(false, err)
  50. eq('Invalid highlight id: 30000', string.match(emsg, 'Invalid.*'))
  51. -- Test all highlight properties.
  52. command('hi NewHighlight gui=underline,bold,underlineline,undercurl,underdot,underdash,italic,reverse,strikethrough')
  53. eq(expected_rgb2, nvim("get_hl_by_id", hl_id, true))
  54. -- Test nil argument.
  55. err, emsg = pcall(meths.get_hl_by_id, { nil }, false)
  56. eq(false, err)
  57. eq('Wrong type for argument 1 when calling nvim_get_hl_by_id, expecting Integer',
  58. string.match(emsg, 'Wrong.*'))
  59. -- Test 0 argument.
  60. err, emsg = pcall(meths.get_hl_by_id, 0, false)
  61. eq(false, err)
  62. eq('Invalid highlight id: 0',
  63. string.match(emsg, 'Invalid.*'))
  64. -- Test -1 argument.
  65. err, emsg = pcall(meths.get_hl_by_id, -1, false)
  66. eq(false, err)
  67. eq('Invalid highlight id: -1',
  68. string.match(emsg, 'Invalid.*'))
  69. -- Test highlight group without ctermbg value.
  70. command('hi Normal ctermfg=red ctermbg=yellow')
  71. command('hi NewConstant ctermfg=green guifg=white guibg=blue')
  72. hl_id = eval("hlID('NewConstant')")
  73. eq({foreground = 10,}, meths.get_hl_by_id(hl_id, false))
  74. -- Test highlight group without ctermfg value.
  75. command('hi clear NewConstant')
  76. command('hi NewConstant ctermbg=Magenta guifg=white guibg=blue')
  77. eq({background = 13,}, meths.get_hl_by_id(hl_id, false))
  78. -- Test highlight group with ctermfg and ctermbg values.
  79. command('hi clear NewConstant')
  80. command('hi NewConstant ctermfg=green ctermbg=Magenta guifg=white guibg=blue')
  81. eq({foreground = 10, background = 13,}, meths.get_hl_by_id(hl_id, false))
  82. end)
  83. it("nvim_get_hl_by_name", function()
  84. local expected_normal = { background = Screen.colors.Yellow,
  85. foreground = Screen.colors.Red }
  86. -- Test `Normal` default values.
  87. eq({}, nvim("get_hl_by_name", 'Normal', true))
  88. eq(expected_cterm, nvim("get_hl_by_name", 'NewHighlight', false))
  89. eq(expected_rgb, nvim("get_hl_by_name", 'NewHighlight', true))
  90. -- Test `Normal` modified values.
  91. command('hi Normal guifg=red guibg=yellow')
  92. eq(expected_normal, nvim("get_hl_by_name", 'Normal', true))
  93. -- Test invalid name.
  94. local err, emsg = pcall(meths.get_hl_by_name , 'unknown_highlight', false)
  95. eq(false, err)
  96. eq('Invalid highlight name: unknown_highlight',
  97. string.match(emsg, 'Invalid.*'))
  98. -- Test nil argument.
  99. err, emsg = pcall(meths.get_hl_by_name , { nil }, false)
  100. eq(false, err)
  101. eq('Wrong type for argument 1 when calling nvim_get_hl_by_name, expecting String',
  102. string.match(emsg, 'Wrong.*'))
  103. -- Test empty string argument.
  104. err, emsg = pcall(meths.get_hl_by_name , '', false)
  105. eq(false, err)
  106. eq('Invalid highlight name: ',
  107. string.match(emsg, 'Invalid.*'))
  108. -- Test "standout" attribute. #8054
  109. eq({ underline = true, },
  110. meths.get_hl_by_name('cursorline', 0));
  111. command('hi CursorLine cterm=standout,underline term=standout,underline gui=standout,underline')
  112. command('set cursorline')
  113. eq({ underline = true, standout = true, },
  114. meths.get_hl_by_name('cursorline', 0));
  115. -- Test cterm & Normal values. #18024 (tail) & #18980
  116. -- Ensure Normal, and groups that match Normal return their fg & bg cterm values
  117. meths.set_hl(0, 'Normal', {ctermfg = 17, ctermbg = 213})
  118. meths.set_hl(0, 'NotNormal', {ctermfg = 17, ctermbg = 213})
  119. -- Note colors are "cterm" values, not rgb-as-ints
  120. eq({foreground = 17, background = 213}, nvim("get_hl_by_name", 'Normal', false))
  121. eq({foreground = 17, background = 213}, nvim("get_hl_by_name", 'NotNormal', false))
  122. end)
  123. it('nvim_get_hl_id_by_name', function()
  124. -- precondition: use a hl group that does not yet exist
  125. eq('Invalid highlight name: Shrubbery', pcall_err(meths.get_hl_by_name, "Shrubbery", true))
  126. eq(0, funcs.hlID("Shrubbery"))
  127. local hl_id = meths.get_hl_id_by_name("Shrubbery")
  128. ok(hl_id > 0)
  129. eq(hl_id, funcs.hlID("Shrubbery"))
  130. command('hi Shrubbery guifg=#888888 guibg=#888888')
  131. eq({foreground=tonumber("0x888888"), background=tonumber("0x888888")},
  132. meths.get_hl_by_id(hl_id, true))
  133. eq({foreground=tonumber("0x888888"), background=tonumber("0x888888")},
  134. meths.get_hl_by_name("Shrubbery", true))
  135. end)
  136. it("nvim_buf_add_highlight to other buffer doesn't crash if undo is disabled #12873", function()
  137. command('vsplit file')
  138. local err, _ = pcall(meths.buf_set_option, 1, 'undofile', false)
  139. eq(true, err)
  140. err, _ = pcall(meths.buf_set_option, 1, 'undolevels', -1)
  141. eq(true, err)
  142. err, _ = pcall(meths.buf_add_highlight, 1, -1, 'Question', 0, 0, -1)
  143. eq(true, err)
  144. assert_alive()
  145. end)
  146. end)
  147. describe("API: set highlight", function()
  148. local highlight_color = {
  149. fg = tonumber('0xff0000'),
  150. bg = tonumber('0x0032aa'),
  151. ctermfg = 8,
  152. ctermbg = 15,
  153. }
  154. local highlight1 = {
  155. background = highlight_color.bg,
  156. foreground = highlight_color.fg,
  157. bold = true,
  158. italic = true,
  159. }
  160. local highlight2_config = {
  161. ctermbg = highlight_color.ctermbg,
  162. ctermfg = highlight_color.ctermfg,
  163. underline = true,
  164. reverse = true,
  165. }
  166. local highlight2_result = {
  167. background = highlight_color.ctermbg,
  168. foreground = highlight_color.ctermfg,
  169. underline = true,
  170. reverse = true,
  171. }
  172. local highlight3_config = {
  173. background = highlight_color.bg,
  174. foreground = highlight_color.fg,
  175. ctermbg = highlight_color.ctermbg,
  176. ctermfg = highlight_color.ctermfg,
  177. bold = true,
  178. italic = true,
  179. reverse = true,
  180. undercurl = true,
  181. underline = true,
  182. underdash = true,
  183. underdot = true,
  184. underlineline = true,
  185. strikethrough = true,
  186. cterm = {
  187. italic = true,
  188. reverse = true,
  189. undercurl = true,
  190. strikethrough = true,
  191. }
  192. }
  193. local highlight3_result_gui = {
  194. background = highlight_color.bg,
  195. foreground = highlight_color.fg,
  196. bold = true,
  197. italic = true,
  198. reverse = true,
  199. undercurl = true,
  200. underline = true,
  201. underdash = true,
  202. underdot = true,
  203. underlineline = true,
  204. strikethrough = true,
  205. }
  206. local highlight3_result_cterm = {
  207. background = highlight_color.ctermbg,
  208. foreground = highlight_color.ctermfg,
  209. italic = true,
  210. reverse = true,
  211. undercurl = true,
  212. strikethrough = true,
  213. }
  214. local function get_ns()
  215. local ns = meths.create_namespace('Test_set_hl')
  216. meths._set_hl_ns(ns)
  217. return ns
  218. end
  219. before_each(clear)
  220. it ("can set gui highlight", function()
  221. local ns = get_ns()
  222. meths.set_hl(ns, 'Test_hl', highlight1)
  223. eq(highlight1, meths.get_hl_by_name('Test_hl', true))
  224. end)
  225. it ("can set cterm highlight", function()
  226. local ns = get_ns()
  227. meths.set_hl(ns, 'Test_hl', highlight2_config)
  228. eq(highlight2_result, meths.get_hl_by_name('Test_hl', false))
  229. end)
  230. it ("can set empty cterm attr", function()
  231. local ns = get_ns()
  232. meths.set_hl(ns, 'Test_hl', { cterm = {} })
  233. eq({}, meths.get_hl_by_name('Test_hl', false))
  234. end)
  235. it ("cterm attr defaults to gui attr", function()
  236. local ns = get_ns()
  237. meths.set_hl(ns, 'Test_hl', highlight1)
  238. eq({
  239. bold = true,
  240. italic = true,
  241. }, meths.get_hl_by_name('Test_hl', false))
  242. end)
  243. it ("can overwrite attr for cterm", function()
  244. local ns = get_ns()
  245. meths.set_hl(ns, 'Test_hl', highlight3_config)
  246. eq(highlight3_result_gui, meths.get_hl_by_name('Test_hl', true))
  247. eq(highlight3_result_cterm, meths.get_hl_by_name('Test_hl', false))
  248. end)
  249. it ("can set a highlight in the global namespace", function()
  250. meths.set_hl(0, 'Test_hl', highlight2_config)
  251. eq('Test_hl xxx cterm=underline,reverse ctermfg=8 ctermbg=15 gui=underline,reverse',
  252. exec_capture('highlight Test_hl'))
  253. meths.set_hl(0, 'Test_hl', { background = highlight_color.bg })
  254. eq('Test_hl xxx guibg=#0032aa',
  255. exec_capture('highlight Test_hl'))
  256. meths.set_hl(0, 'Test_hl2', highlight3_config)
  257. eq('Test_hl2 xxx cterm=undercurl,italic,reverse,strikethrough ctermfg=8 ctermbg=15 gui=bold,underline,underlineline,undercurl,underdot,underdash,italic,reverse,strikethrough guifg=#ff0000 guibg=#0032aa',
  258. exec_capture('highlight Test_hl2'))
  259. -- Colors are stored exactly as they are defined.
  260. meths.set_hl(0, 'Test_hl3', { bg = 'reD', fg = 'bLue'})
  261. eq('Test_hl3 xxx guifg=bLue guibg=reD',
  262. exec_capture('highlight Test_hl3'))
  263. end)
  264. it ("can modify a highlight in the global namespace", function()
  265. meths.set_hl(0, 'Test_hl3', { bg = 'red', fg = 'blue'})
  266. eq('Test_hl3 xxx guifg=blue guibg=red',
  267. exec_capture('highlight Test_hl3'))
  268. meths.set_hl(0, 'Test_hl3', { bg = 'red' })
  269. eq('Test_hl3 xxx guibg=red',
  270. exec_capture('highlight Test_hl3'))
  271. meths.set_hl(0, 'Test_hl3', { ctermbg = 9, ctermfg = 12})
  272. eq('Test_hl3 xxx ctermfg=12 ctermbg=9',
  273. exec_capture('highlight Test_hl3'))
  274. meths.set_hl(0, 'Test_hl3', { ctermbg = 'red' , ctermfg = 'blue'})
  275. eq('Test_hl3 xxx ctermfg=12 ctermbg=9',
  276. exec_capture('highlight Test_hl3'))
  277. meths.set_hl(0, 'Test_hl3', { ctermbg = 9 })
  278. eq('Test_hl3 xxx ctermbg=9',
  279. exec_capture('highlight Test_hl3'))
  280. eq("'redd' is not a valid color",
  281. pcall_err(meths.set_hl, 0, 'Test_hl3', {fg='redd'}))
  282. eq("'bleu' is not a valid color",
  283. pcall_err(meths.set_hl, 0, 'Test_hl3', {ctermfg='bleu'}))
  284. meths.set_hl(0, 'Test_hl3', {fg='#FF00FF'})
  285. eq('Test_hl3 xxx guifg=#FF00FF',
  286. exec_capture('highlight Test_hl3'))
  287. eq("'#FF00FF' is not a valid color",
  288. pcall_err(meths.set_hl, 0, 'Test_hl3', {ctermfg='#FF00FF'}))
  289. for _, fg_val in ipairs{ nil, 'NONE', 'nOnE', '', -1 } do
  290. meths.set_hl(0, 'Test_hl3', {fg = fg_val})
  291. eq('Test_hl3 xxx cleared',
  292. exec_capture('highlight Test_hl3'))
  293. end
  294. meths.set_hl(0, 'Test_hl3', {fg='#FF00FF', blend=50})
  295. eq('Test_hl3 xxx guifg=#FF00FF blend=50',
  296. exec_capture('highlight Test_hl3'))
  297. end)
  298. it ("correctly sets 'Normal' internal properties", function()
  299. -- Normal has some special handling internally. #18024
  300. meths.set_hl(0, 'Normal', {fg='#000083', bg='#0000F3'})
  301. eq({foreground = 131, background = 243}, nvim("get_hl_by_name", 'Normal', true))
  302. end)
  303. end)