mirror of
				https://github.com/neovim/neovim.git
				synced 2025-11-04 01:34:25 +00:00 
			
		
		
		
	fix(treesitter): nil access when running string parser async
This commit is contained in:
		
				
					committed by
					
						
						Christian Clason
					
				
			
			
				
	
			
			
			
						parent
						
							f3381a8b64
						
					
				
				
					commit
					096ae3bfd7
				
			@@ -476,21 +476,26 @@ function LanguageTree:_async_parse(range, on_parse)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  local source = self._source
 | 
			
		||||
  local buf = vim.b[source]
 | 
			
		||||
  local ct = buf.changedtick
 | 
			
		||||
  local is_buffer_parser = type(source) == 'number'
 | 
			
		||||
  local buf = is_buffer_parser and vim.b[source] or nil
 | 
			
		||||
  local ct = is_buffer_parser and buf.changedtick or nil
 | 
			
		||||
  local total_parse_time = 0
 | 
			
		||||
  local redrawtime = vim.o.redrawtime
 | 
			
		||||
  local timeout = not vim.g._ts_force_sync_parsing and default_parse_timeout_ms or nil
 | 
			
		||||
 | 
			
		||||
  local function step()
 | 
			
		||||
    if type(source) == 'number' and not vim.api.nvim_buf_is_valid(source) then
 | 
			
		||||
      return nil
 | 
			
		||||
    end
 | 
			
		||||
    if is_buffer_parser then
 | 
			
		||||
      if
 | 
			
		||||
        not vim.api.nvim_buf_is_valid(source --[[@as number]])
 | 
			
		||||
      then
 | 
			
		||||
        return nil
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
    -- If buffer was changed in the middle of parsing, reset parse state
 | 
			
		||||
    if buf.changedtick ~= ct then
 | 
			
		||||
      ct = buf.changedtick
 | 
			
		||||
      total_parse_time = 0
 | 
			
		||||
      -- If buffer was changed in the middle of parsing, reset parse state
 | 
			
		||||
      if buf.changedtick ~= ct then
 | 
			
		||||
        ct = buf.changedtick
 | 
			
		||||
        total_parse_time = 0
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    local parse_time, trees, finished = tcall(self._parse, self, range, timeout)
 | 
			
		||||
 
 | 
			
		||||
@@ -504,6 +504,15 @@ end]]
 | 
			
		||||
    eq({ 0, 0, 0, 13 }, ret)
 | 
			
		||||
  end)
 | 
			
		||||
 | 
			
		||||
  it('can run async parses with string parsers', function()
 | 
			
		||||
    local ret = exec_lua(function()
 | 
			
		||||
      local parser = vim.treesitter.get_string_parser('int foo = 42;', 'c')
 | 
			
		||||
      return { parser:parse(nil, function() end)[1]:root():range() }
 | 
			
		||||
    end)
 | 
			
		||||
 | 
			
		||||
    eq({ 0, 0, 0, 13 }, ret)
 | 
			
		||||
  end)
 | 
			
		||||
 | 
			
		||||
  it('allows to run queries with string parsers', function()
 | 
			
		||||
    local txt = [[
 | 
			
		||||
      int foo = 42;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user