robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
12.0 KiB461 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear = n.clear
7local exec = n.exec
8local exec_lua = n.exec_lua
9local eq = t.eq
10local fn = n.fn
11local api = n.api
12local insert = n.insert
13
14local function html_syntax_match()
15  local styles =
16    vim.split(api.nvim_exec2([[/<style>/+,/<\/style>/-p]], { output = true }).output, '\n')
17  local attrnames = {
18    ['font%-weight: bold'] = 'bold',
19    ['text%-decoration%-line: [^;]*underline'] = 'underline',
20    ['font%-style: italic'] = 'italic',
21    ['text%-decoration%-line: [^;]*line%-through'] = 'strikethrough',
22  }
23  local hls = {}
24  for _, style in ipairs(styles) do
25    local attr = {}
26    for match, attrname in pairs(attrnames) do
27      if style:find(match) then
28        ---@type boolean
29        attr[attrname] = true
30      end
31    end
32    if style:find('text%-decoration%-style: wavy') and attr.underline then
33      ---@type boolean
34      attr.underline = nil
35      attr.undercurl = true
36    end
37    attr.sp = style:match('text%-decoration%-color: #(%x+)')
38    if attr.sp then
39      attr.sp = tonumber(attr.sp, 16)
40    end
41    attr.bg = style:match('background%-color: #(%x+)')
42    if attr.bg then
43      attr.bg = tonumber(attr.bg, 16)
44    end
45    attr.fg = style:match('[^%-]color: #(%x+)')
46    if attr.fg then
47      attr.fg = tonumber(attr.fg, 16)
48    end
49    if style:match('^%.(%w+)') then
50      ---@type table
51      hls[style:match('^%.(%w+)')] = attr
52    end
53  end
54  local whitelist = {
55    'fg',
56    'bg',
57    'sp',
58    --'blend',
59    'bold',
60    --'standout',
61    'underline',
62    'undercurl',
63    --'underdouble',
64    --'underdotted',
65    --'underdashed',
66    'strikethrough',
67    'italic',
68    --'reverse',
69    --'nocombine',
70  }
71  for name, attrs_old in
72    pairs(api.nvim_get_hl(0, { link = true }) --[[@as table<string,table>]])
73  do
74    ---@type table
75    local other = hls[name:gsub('%.', '-'):gsub('@', '-')]
76    if other then
77      local attrs = {}
78      for _, attrname in ipairs(whitelist) do
79        ---@type table
80        attrs[attrname] = attrs_old[attrname]
81      end
82      eq(attrs, other)
83    end
84  end
85  return hls
86end
87
88local function html_to_extmarks()
89  local buf = api.nvim_get_current_buf()
90  local ns = api.nvim_create_namespace 'test-namespace'
91  api.nvim_buf_clear_namespace(buf, ns, 0, -1)
92  exec 'silent! norm! ggd/^<pre>$\rddG3dk'
93  local stack = {}
94  exec [[set filetype=]]
95  exec [[silent! %s/</¤/g]]
96  exec [[silent! %s/&quot;/"/g]]
97  exec [[silent! %s/&amp;/\&/g]]
98  exec [[silent! %s/&gt;/>/g]]
99  exec [[silent! %s/&lt;/</g]]
100  for _, match in
101    ipairs(
102      fn.matchbufline(buf, [[¤span class="\([^"]\+\)">\|¤/span>]], 1, '$', { submatches = true }) --[[@as (table[])]]
103    )
104  do
105    if match.text == '¤/span>' then
106      local val = table.remove(stack)
107      api.nvim_buf_set_extmark(buf, ns, val.lnum - 1, val.byteidx, {
108        hl_group = val.submatches[1],
109        end_row = match.lnum - 1,
110        end_col = match.byteidx,
111      })
112    else
113      table.insert(stack, match)
114    end
115  end
116  exec [[silent! %s/¤\/span>//g]]
117  exec [[silent! %s/¤span[^>]*>//g]]
118end
119
120---@param screen test.functional.ui.screen
121---@param func function?
122local function run_tohtml_and_assert(screen, func)
123  exec('norm! ggO--;')
124  screen:expect({ any = vim.pesc('--^;') })
125  exec('norm! :\rh')
126  screen:expect({ any = vim.pesc('-^-;') })
127  local expected = screen:get_snapshot()
128  do
129    (func or exec)('TOhtml')
130  end
131  exec('only')
132  html_syntax_match()
133  html_to_extmarks()
134  exec('norm! gg0f;')
135  screen:expect({ any = vim.pesc('--^;') })
136  exec('norm! :\rh')
137  screen:expect({ grid = expected.grid, attr_ids = expected.attr_ids })
138end
139
140---@param guifont boolean
141local function test_generates_html(guifont, expect_font)
142  insert([[line]])
143  exec('set termguicolors')
144  local bg = fn.synIDattr(fn.hlID('Normal'), 'bg#', 'gui')
145  local fg = fn.synIDattr(fn.hlID('Normal'), 'fg#', 'gui')
146  local tmpfile = t.tmpname()
147
148  exec_lua(
149    [[
150    local guifont, outfile = ...
151    local html = (guifont
152      and require('tohtml').tohtml(0,{title="title"})
153      or require('tohtml').tohtml(0,{title="title",font={ "dumyfont","anotherfont" }}))
154    vim.fn.writefile(html, outfile)
155    vim.cmd.split(outfile)
156  ]],
157    guifont,
158    tmpfile
159  )
160
161  local out_file = api.nvim_buf_get_name(api.nvim_get_current_buf())
162  eq({
163    '<!-- vim: set nomodeline: -->',
164    '<!DOCTYPE html>',
165    '<html>',
166    '<head>',
167    '<meta charset="UTF-8">',
168    '<title>title</title>',
169    ('<meta name="colorscheme" content="%s"></meta>'):format(api.nvim_get_var('colors_name')),
170    '<style>',
171    ('* {font-family: %s,monospace}'):format(expect_font),
172    ('body {background-color: %s; color: %s}'):format(bg, fg),
173    '</style>',
174    '</head>',
175    '<body style="display: flex">',
176    '<pre>',
177    'line',
178    '',
179    '</pre>',
180    '</body>',
181    '</html>',
182  }, fn.readfile(out_file))
183end
184
185describe(':TOhtml', function()
186  --- @type test.functional.ui.screen
187  local screen
188  before_each(function()
189    clear({ args = { '--clean' } })
190    n.command('packadd nvim.tohtml')
191    screen = Screen.new(80, 80, { term_name = 'xterm' })
192    exec('colorscheme default')
193  end)
194
195  it('generates html with given font', function()
196    test_generates_html(false, '"dumyfont","anotherfont"')
197  end)
198
199  it("generates html, respects 'guifont'", function()
200    exec_lua [[vim.o.guifont='Font,Escape\\,comma, Ignore space after comma']]
201    test_generates_html(true, '"Font","Escape,comma","Ignore space after comma"')
202  end)
203
204  it('generates html from range', function()
205    insert([[
206    line1
207    line2
208    line3
209    ]])
210    local ns = api.nvim_create_namespace ''
211    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 1, end_row = 1, hl_group = 'Visual' })
212    exec('set termguicolors')
213    local bg = fn.synIDattr(fn.hlID('Normal'), 'bg#', 'gui')
214    local fg = fn.synIDattr(fn.hlID('Normal'), 'fg#', 'gui')
215    exec_lua [[vim.o.guifont='Courier New' ]]
216    n.command('2,2TOhtml')
217    local out_file = api.nvim_buf_get_name(api.nvim_get_current_buf())
218    eq({
219      '<!-- vim: set nomodeline: -->',
220      '<!DOCTYPE html>',
221      '<html>',
222      '<head>',
223      '<meta charset="UTF-8">',
224      '<title></title>',
225      ('<meta name="colorscheme" content="%s"></meta>'):format(api.nvim_get_var('colors_name')),
226      '<style>',
227      ('* {font-family: "%s",monospace}'):format(n.eval('&guifont')),
228      ('body {background-color: %s; color: %s}'):format(bg, fg),
229      '.Visual {background-color: #9b9ea4}',
230      '</style>',
231      '</head>',
232      '<body style="display: flex">',
233      '<pre><span class="Visual">',
234      'l</span>ine2',
235      '',
236      '</pre>',
237      '</body>',
238      '</html>',
239    }, fn.readfile(out_file))
240  end)
241
242  it('generates highlight attributes', function()
243    --Make sure to uncomment the attribute in `html_syntax_match()`
244    exec('hi LINE guisp=#00ff00 gui=' .. table.concat({
245      'bold',
246      'underline',
247      'italic',
248      'strikethrough',
249    }, ','))
250    exec('hi UNDERCURL gui=undercurl')
251    exec('syn keyword LINE line')
252    exec('syn keyword UNDERCURL undercurl')
253    insert('line\nundercurl')
254    run_tohtml_and_assert(screen)
255  end)
256
257  it('syntax', function()
258    insert [[
259    function main()
260      print("hello world")
261    end
262    ]]
263    exec('set termguicolors')
264    exec('syntax enable')
265    exec('setf lua')
266    exec_lua('vim.treesitter.stop()') -- Ensure that legacy syntax (not just TS) is tested.
267    run_tohtml_and_assert(screen)
268  end)
269
270  it('diff', function()
271    exec('set diffopt=')
272    insert [[
273    diffadd
274    nochage
275    diffchange1
276    ]]
277    exec('new')
278    insert [[
279    nochage
280    diffchange2
281    diffremove
282    ]]
283    exec('set diff')
284    exec('close')
285    exec('set diff')
286    run_tohtml_and_assert(screen)
287  end)
288
289  it('treesitter', function()
290    insert [[
291    function main()
292      print("hello world")
293    end
294    ]]
295    exec('setf lua')
296    exec_lua('vim.treesitter.start()')
297    run_tohtml_and_assert(screen)
298  end)
299
300  it('matchadd', function()
301    insert [[
302    line
303    ]]
304    fn.matchadd('Visual', 'line')
305    run_tohtml_and_assert(screen)
306  end)
307
308  describe('conceallevel', function()
309    local function run(level)
310      insert([[
311      line0
312      line1
313      line2
314      line3
315      ]])
316      local ns = api.nvim_create_namespace ''
317      fn.matchadd('Conceal', 'line1', 3, 5, { conceal = 'a' })
318      api.nvim_buf_set_extmark(0, ns, 2, 0, { conceal = 'a', end_col = 5 })
319      exec(':syntax match Conceal "line3" conceal cchar=a')
320      exec('set conceallevel=' .. level)
321      run_tohtml_and_assert(screen)
322    end
323    it('conceallevel=0', function()
324      run(0)
325    end)
326    it('conceallevel=1', function()
327      run(1)
328    end)
329    it('conceallevel=2', function()
330      run(2)
331    end)
332    it('conceallevel=3', function()
333      run(3)
334    end)
335  end)
336
337  describe('extmarks', function()
338    it('virt_text', function()
339      insert [[
340      line1
341      line2
342      line3
343      line4
344      ]]
345      local ns = api.nvim_create_namespace ''
346      api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'foo' } } })
347      api.nvim_buf_set_extmark(
348        0,
349        ns,
350        1,
351        0,
352        { virt_text = { { 'foo' } }, virt_text_pos = 'overlay' }
353      )
354      api.nvim_buf_set_extmark(
355        0,
356        ns,
357        2,
358        0,
359        { virt_text = { { 'fo┊o', { 'Conceal', 'Comment' } } }, virt_text_pos = 'inline' }
360      )
361      --api.nvim_buf_set_extmark(0,ns,3,0,{virt_text={{'foo'}},virt_text_pos='right_align'})
362      run_tohtml_and_assert(screen)
363    end)
364    it('highlight', function()
365      insert [[
366      line1
367      ]]
368      local ns = api.nvim_create_namespace ''
369      api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 2, hl_group = 'Visual' })
370      run_tohtml_and_assert(screen)
371    end)
372    it('virt_line', function()
373      insert [[
374      line1
375      line2
376      ]]
377      local ns = api.nvim_create_namespace ''
378      api.nvim_buf_set_extmark(0, ns, 1, 0, { end_col = 2, virt_lines = { { { 'foo' } } } })
379      run_tohtml_and_assert(screen)
380    end)
381  end)
382
383  it('listchars', function()
384    exec('setlocal list')
385    exec(
386      'setlocal listchars=eol:$,tab:<->,space:-,multispace:++,lead:_,leadmultispace:##,trail:&,nbsp:%'
387    )
388    fn.setline(1, '\tfoo\t')
389    fn.setline(2, ' foo foo ')
390    fn.setline(3, '  foo  foo  ')
391    fn.setline(4, 'foo\194\160 \226\128\175foo')
392    run_tohtml_and_assert(screen)
393    exec('new|only')
394    fn.setline(1, '\tfoo\t')
395    exec('setlocal list')
396    exec('setlocal listchars=tab:a-')
397    run_tohtml_and_assert(screen)
398  end)
399
400  it('folds', function()
401    insert([[
402    line1
403    line2
404    ]])
405    exec('set foldtext=foldtext()')
406    exec('%fo')
407    run_tohtml_and_assert(screen)
408  end)
409
410  it('warns about highlighted foldtext from a Lua callback', function()
411    insert('line1\nline2')
412    exec_lua(function()
413      _G.warnings = {}
414      vim.notify = function(msg)
415        table.insert(_G.warnings, msg)
416      end
417      vim.o.foldtext = function()
418        return { { 'folded', 'Comment' } }
419      end
420    end)
421    exec('%fold')
422    exec_lua([[require('tohtml').tohtml(0)]])
423    eq({
424      'TOhtml: foldtext returning a table with highlights is not supported, HTML may be incorrect',
425    }, exec_lua('return _G.warnings'))
426  end)
427
428  it('statuscol', function()
429    local function run()
430      local buf = api.nvim_get_current_buf()
431      run_tohtml_and_assert(screen, function()
432        exec_lua(function()
433          local outfile = vim.fn.tempname() .. '.html'
434          local html = require('tohtml').tohtml(0, { number_lines = true })
435          vim.fn.writefile(html, outfile)
436          vim.cmd.split(outfile)
437        end)
438      end)
439      api.nvim_set_current_buf(buf)
440    end
441    insert([[
442    line1
443    line2
444    ]])
445    exec('setlocal relativenumber')
446    run()
447    exec('setlocal norelativenumber')
448    exec('setlocal number')
449    run()
450    exec('setlocal relativenumber')
451    run()
452    exec('setlocal signcolumn=yes:2')
453    run()
454    exec('setlocal foldcolumn=2')
455    run()
456    exec('setlocal norelativenumber')
457    run()
458    exec('setlocal signcolumn=no')
459    run()
460  end)
461end)