robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
2.6 KiB86 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local command = n.command
7local eq = t.eq
8local exec_lua = n.exec_lua
9local insert = n.insert
10local skip = t.skip
11
12local lua_text = [[
13local M = {}
14
15-- returns the sum of two numbers
16local function add(a, b)
17  return a + b
18end
19
20M.add = add
21return M
22]]
23
24describe('treesitter WASM parser', function()
25  before_each(clear)
26
27  it('lua WASM parser produces same highlight captures as native parser', function()
28    skip(
29      exec_lua('return vim._ts_add_language_from_wasm == nil'),
30      'N/A ENABLE_WASMTIME not enabled'
31    )
32
33    local wasm_files = exec_lua(function()
34      return vim.api.nvim_get_runtime_file('parser/lua.wasm', true)
35    end)
36    assert(
37      #wasm_files > 0,
38      'lua.wasm not found in runtimepath (rebuild with ENABLE_WASMTIME=ON USE_BUNDLED_TS_PARSERS=ON)'
39    )
40
41    local wasm_path = wasm_files[1]
42
43    local query_str = exec_lua(function()
44      local files = vim.api.nvim_get_runtime_file('queries/lua/highlights.scm', false)
45      assert(#files > 0, 'queries/lua/highlights.scm not found')
46      local f = assert(io.open(files[1]))
47      local s = f:read('*a')
48      f:close()
49      return s
50    end)
51
52    -- Buffer 1: parse with the native lua parser.
53    insert(lua_text)
54    local native_captures = exec_lua(function(qstr)
55      local query = vim.treesitter.query.parse('lua', qstr)
56      local parser = vim.treesitter.get_parser(0, 'lua')
57      local tree = parser:parse()[1]
58      local result = {}
59      for id, node in query:iter_captures(tree:root(), 0) do
60        local r = { node:range() }
61        table.insert(result, { query.captures[id], r[1], r[2], r[3], r[4] })
62      end
63      return result
64    end, query_str)
65
66    -- Buffer 2: same content, but the lua language loaded from WASM.
67    command('new')
68    insert(lua_text)
69    local wasm_captures = exec_lua(function(path, qstr)
70      -- Remove the native registration so the WASM one can take its place.
71      vim._ts_remove_language('lua')
72      vim._ts_add_language_from_wasm(path, 'lua')
73      local query = vim.treesitter.query.parse('lua', qstr)
74      local parser = vim.treesitter.get_parser(0, 'lua')
75      local tree = parser:parse()[1]
76      local result = {}
77      for id, node in query:iter_captures(tree:root(), 0) do
78        local r = { node:range() }
79        table.insert(result, { query.captures[id], r[1], r[2], r[3], r[4] })
80      end
81      return result
82    end, wasm_path, query_str)
83
84    eq(native_captures, wasm_captures)
85  end)
86end)