mirror of
				https://github.com/neovim/neovim.git
				synced 2025-11-04 01:34:25 +00:00 
			
		
		
		
	fix(treesitter): show proper node name error messages
**Problem:** Currently node names with non-alphanumeric, non underscore/hyphen characters (only possible with anonymous nodes) are not given a proper error message. See tree-sitter issue 3892 for more details. **Solution:** Apply a different scanning logic to anonymous nodes to correctly identify the entire node name (i.e., up until the final double quote)
This commit is contained in:
		
				
					committed by
					
						
						Christian Clason
					
				
			
			
				
	
			
			
			
						parent
						
							37f665c504
						
					
				
				
					commit
					36990f324d
				
			@@ -1528,11 +1528,26 @@ static void query_err_string(const char *src, int error_offset, TSQueryError err
 | 
				
			|||||||
      || error_type == TSQueryErrorField
 | 
					      || error_type == TSQueryErrorField
 | 
				
			||||||
      || error_type == TSQueryErrorCapture) {
 | 
					      || error_type == TSQueryErrorCapture) {
 | 
				
			||||||
    const char *suffix = src + error_offset;
 | 
					    const char *suffix = src + error_offset;
 | 
				
			||||||
 | 
					    bool is_anonymous = error_type == TSQueryErrorNodeType && suffix[-1] == '"';
 | 
				
			||||||
    int suffix_len = 0;
 | 
					    int suffix_len = 0;
 | 
				
			||||||
    char c = suffix[suffix_len];
 | 
					    char c = suffix[suffix_len];
 | 
				
			||||||
 | 
					    if (is_anonymous) {
 | 
				
			||||||
 | 
					      int backslashes = 0;
 | 
				
			||||||
 | 
					      // Stop when we hit an unescaped double quote
 | 
				
			||||||
 | 
					      while (c != '"' || backslashes % 2 != 0) {
 | 
				
			||||||
 | 
					        if (c == '\\') {
 | 
				
			||||||
 | 
					          backslashes += 1;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					          backslashes = 0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        c = suffix[++suffix_len];
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      // Stop when we hit the end of the identifier
 | 
				
			||||||
      while (isalnum(c) || c == '_' || c == '-' || c == '.') {
 | 
					      while (isalnum(c) || c == '_' || c == '-' || c == '.') {
 | 
				
			||||||
        c = suffix[++suffix_len];
 | 
					        c = suffix[++suffix_len];
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    snprintf(err, errlen, "\"%.*s\":\n", suffix_len, suffix);
 | 
					    snprintf(err, errlen, "\"%.*s\":\n", suffix_len, suffix);
 | 
				
			||||||
    offset = strlen(err);
 | 
					    offset = strlen(err);
 | 
				
			||||||
    errlen = errlen - offset;
 | 
					    errlen = errlen - offset;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -722,7 +722,25 @@ void ui_refresh(void)
 | 
				
			|||||||
      eq(exp, pcall_err(exec_lua, "vim.treesitter.query.parse('c', ...)", cquery))
 | 
					      eq(exp, pcall_err(exec_lua, "vim.treesitter.query.parse('c', ...)", cquery))
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    -- Invalid node type
 | 
					    -- Invalid node types
 | 
				
			||||||
 | 
					    test(
 | 
				
			||||||
 | 
					      '.../query.lua:0: Query error at 1:2. Invalid node type ">\\">>":\n'
 | 
				
			||||||
 | 
					        .. '">\\">>" @operator\n'
 | 
				
			||||||
 | 
					        .. ' ^',
 | 
				
			||||||
 | 
					      '">\\">>" @operator'
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    test(
 | 
				
			||||||
 | 
					      '.../query.lua:0: Query error at 1:2. Invalid node type "\\\\":\n'
 | 
				
			||||||
 | 
					        .. '"\\\\" @operator\n'
 | 
				
			||||||
 | 
					        .. ' ^',
 | 
				
			||||||
 | 
					      '"\\\\" @operator'
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    test(
 | 
				
			||||||
 | 
					      '.../query.lua:0: Query error at 1:2. Invalid node type ">>>":\n'
 | 
				
			||||||
 | 
					        .. '">>>" @operator\n'
 | 
				
			||||||
 | 
					        .. ' ^',
 | 
				
			||||||
 | 
					      '">>>" @operator'
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
    test(
 | 
					    test(
 | 
				
			||||||
      '.../query.lua:0: Query error at 1:2. Invalid node type "dentifier":\n'
 | 
					      '.../query.lua:0: Query error at 1:2. Invalid node type "dentifier":\n'
 | 
				
			||||||
        .. '(dentifier) @variable\n'
 | 
					        .. '(dentifier) @variable\n'
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user