Repository: neovim/neovim
File: test/functional/treesitter/parser_spec.lua (Lua)

Code before:
      }]])

    exec_lua(function()
      _G.parser = vim.treesitter.get_parser(0, 'c')
      _G.lang = vim.treesitter.language.inspect('c')
      _G.parser:parse(nil, function(_, trees)
        _G.tree = trees[1]
        _G.root = _G.tree:root()
      end)
      vim.wait(100, function() end)
    end)

    eq('<tree>', exec_lua('return tostring(tree)'))
    eq('<node translation_unit>', exec_lua('return tostring(root)'))
    eq({ 0, 0, 3, 0 }, exec_lua('return {root:range()}'))

    eq(1, exec_lua('return root:child_count()'))
    exec_lua('child = root:child(0)')
    eq('<node function_definition>', exec_lua('return tostring(child)'))
    eq({ 0, 0, 2, 1 }, exec_lua('return {child:range()}'))

    eq('function_definition', exec_lua('return child:type()'))
    eq(true, exec_lua('return child:named()'))
    eq('number', type(exec_lua('return child:symbol()')))
    eq(true, exec_lua('return lang.symbols[child:type()]'))

    exec_lua('anon = root:descendant_for_range(0,8,0,9)')
    eq('(', exec_lua('return anon:type()'))
    eq(false, exec_lua('return anon:named()'))
    eq('number', type(exec_lua('return anon:symbol()')))
    eq(false, exec_lua([=[return lang.symbols[string.format('"%s"', anon:type())]]=]))

    exec_lua('descendant = root:descendant_for_range(1,2,1,12)')
    eq('<node declaration>', exec_lua('return tostring(descendant)'))
    eq({ 1, 2, 1, 12 }, exec_lua('return {descendant:range()}'))
    eq(
      '(declaration type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))',
      exec_lua('return descendant:sexpr()')
    )

    feed('2G7|ay')
    exec_lua(function()
      _G.parser:parse(nil, function(_, trees)
        _G.tree2 = trees[1]
        _G.root2 = _G.tree2:root()
        _G.descendant2 = _G.root2:descendant_for_range(1, 2, 1, 13)
      end)
      vim.wait(100, function() end)
    end)
    eq(false, exec_lua('return tree2 == tree1'))
    eq(false, exec_lua('return root2 == root'))

Review comment (bug):
Probably not intended. Could be a bug in the `on_bytes` logic.

Maybe related:

- https://github.com/neovim/neovim/issues/34272

Code after:
      } // :D]])

    exec_lua(function()
      vim.bo.eol = false
      vim.bo.fixeol = false
      _G.parser = vim.treesitter.get_parser(0, 'c')
      _G.tree = _G.parser:parse()[1]
      _G.root = _G.tree:root()
      _G.lang = vim.treesitter.language.inspect('c')
    end)

    eq(
      '<node translation_unit>',
      exec_lua(function()
        return tostring(_G.root)
      end)
    )
    eq(
      { 0, 0, 0, 2, 7, 33 },
      exec_lua(function()
        return { _G.root:range(true) }
      end)
    )

    -- NOTE: Changing these settings marks the buffer as `modified` but does not fire `on_bytes`,
    -- meaning this test case does not pass... is this intended?
    -- exec_lua(function()
    --   vim.bo.eol = true
    --   vim.bo.fixeol = true
    --   vim.cmd.update()
    --   _G.parser = vim.treesitter.get_parser(0, 'c')
    --   _G.tree = _G.parser:parse()[1]
    --   _G.root = _G.tree:root()
    --   _G.lang = vim.treesitter.language.inspect('c')
    -- end)
    --
    -- eq(
    --   '<node translation_unit>',
    --   exec_lua(function()
    --     return tostring(root)
    --   end)
    -- )
    -- eq(
    --   { 0, 0, 0, 3, 0, 34 },
    --   exec_lua(function()
    --     return { root:range(true) }
    --   end)
    -- )
  end)

  it('parses buffer asynchronously', function()