From 843426065091c0ee03d066ceb8432ab13ca1fa3e Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Tue, 8 Jul 2025 23:14:20 -0700 Subject: [PATCH] Add a synchronous way to get parser change ranges (bug#78402) This commit only adds the new function, the fix for the bug is in the next commit. * doc/lispref/parsing.texi (Using Parser): Add docs. * lisp/treesit.el (treesit): Add shortdoc. * src/treesit.c (treesit_get_affected_ranges): New function extracted from treesit_call_after_change_functions. (treesit_call_after_change_functions): Extract out. (treesit_ensure_parsed): Return affected regions. (Ftreesit_parser_changed_regions): New function that returns the affected regions. (cherry picked from commit 564b947745b2a685edcd93eb8f0d8825352030b8) --- doc/lispref/parsing.texi | 11 +++++++++++ lisp/treesit.el | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi index 922ba217920..91dc5d35e54 100644 --- a/doc/lispref/parsing.texi +++ b/doc/lispref/parsing.texi @@ -573,6 +573,17 @@ symbol, rather than a lambda function. This function returns the list of @var{parser}'s notifier functions. @end defun +A lisp program can also choose to force a parser to reparse and get the +changed regions immediately with @code{treesit-parser-changed-regions}. + +@defun treesit-parser-changed-regions parser +This function force @var{parser} to reparse, and return the affected +regions: a list of @w{@code{(@var{start} . @var{end})}}. If the parser +has nothing new to reparse, or the affected regions are empty, +@code{nil} is returned. +@end defun + + @heading Substitute parser for another language @cindex remap language grammar, tree-sitter @cindex replace language grammar, tree-sitter diff --git a/lisp/treesit.el b/lisp/treesit.el index dfe7efe7d4f..a137160b382 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -5430,6 +5430,19 @@ Tree-sitter grammar for `%s' is missing; install it?" (treesit-parser-language :no-eval (treesit-parser-language parser) :eg-result c) + (treesit-parser-tag + :no-eval (treesit-parser-tag parser) + :eg-result 'embeded) + (treesit-parser-changed-regions + :no-eval (treesit-parser-changed-regions parser) + :eg-result '((1 . 10) (24 . 58))) + + (treesit-parser-embed-level + :no-eval (treesit-parser-embed-level parser) + :eg-result 1) + (treesit-parser-set-embed-level + :no-eval (treesit-parser-set-embed-level parser 1)) + (treesit-parser-add-notifier) (treesit-parser-remove-notifier) (treesit-parser-notifiers -- 2.39.5