Function: treesit-query-compile

treesit-query-compile is a function defined in treesit.c.

Signature

(treesit-query-compile LANGUAGE QUERY &optional EAGER)

Documentation

Compile QUERY to a compiled query.

Querying with a compiled query is much faster than an uncompiled one. So it's a good idea to use compiled query in tight loops, etc.

LANGUAGE is the language this query is for.

If EAGER is non-nil, immediately load LANGUAGE and compile the query. Otherwise defer the compilation until the query is first used.

Signal treesit-query-error if QUERY is malformed or something else goes wrong. (This only happens if EAGER is non-nil.) You can use treesit-query-validate to validate and debug a query.

Other relevant functions are documented in the treesit group.

View in manual

Shortdoc

;; treesit
(treesit-query-compile 'c '((identifier) @id "return" @ret))
    e.g. => #<treesit-compiled-query>

Source Code

// Defined in /usr/src/emacs/src/treesit.c
{
  if (NILP (Ftreesit_query_p (query)))
    wrong_type_argument (Qtreesit_query_p, query);
  CHECK_SYMBOL (language);

  treesit_initialize ();

  if (TS_COMPILED_QUERY_P (query))
    {
      if (NILP (eager))
	return query;
      treesit_ensure_query_compiled_signal (query);
      return query;
    }

  /* We don't map language here, instead, we remap language when
     actually compiling the query.  This way the query appears to have
     the unmapped language to the Lisp world.  */
  Lisp_Object lisp_query = make_treesit_query (query, language);

  /* Maybe actually compile.  */
  if (NILP (eager))
    return lisp_query;
  else
    {
      treesit_ensure_query_compiled_signal (lisp_query);
      return lisp_query;
    }
}