Function: treesit--compile-query-with-cache

treesit--compile-query-with-cache is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit--compile-query-with-cache LANG QUERY)

Documentation

Return the cached compiled QUERY for LANG.

If QUERY is eagerly compiled, return it as-is.

If QUERY is lazily compiled (i.e., not actually compiled) or not compiled, return the cached compiled version of QUERY (either by finding the cached version or compile and cache QUERY and return it). Note that if QUERY is lazily compiled and there is a cache hit, the cached compiled query will be returned and QUERY is simply discarded (rather than eagerly compiled and returned).

If QUERY is invalid, signals treesit-query-error. The fact that QUERY is invalid is also stored in cache, and the next call to this function with the same QUERY will signal too.

QUERY is compared with equal, so string form vs sexp form of a query, and the same query written differently are all considered separate queries.

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit--compile-query-with-cache (lang query)
  "Return the cached compiled QUERY for LANG.

If QUERY is eagerly compiled, return it as-is.

If QUERY is lazily compiled (i.e., not actually compiled) or not
compiled, return the cached compiled version of QUERY (either by finding
the cached version or compile and cache QUERY and return it).  Note that
if QUERY is lazily compiled and there is a cache hit, the cached
compiled query will be returned and QUERY is simply discarded (rather
than eagerly compiled and returned).

If QUERY is invalid, signals `treesit-query-error'.  The fact that QUERY
is invalid is also stored in cache, and the next call to this function
with the same QUERY will signal too.

QUERY is compared with `equal', so string form vs sexp form of a query,
and the same query written differently are all considered separate
queries."
  (cl-assert (treesit-query-p query))
  ;; No need to asset LANG matches the language of QUERY if QUERY is
  ;; compiled, if LANG is wrong, compilation will error anyway.
  (if (and (treesit-compiled-query-p query)
           (treesit-query-eagerly-compiled-p query))
      query
    (let* ((query-source (if (treesit-compiled-query-p query)
                             (treesit-query-source query)
                           query))
           (value (gethash (cons lang query-source) treesit--query-cache)))
      (if value
          (if (treesit-compiled-query-p value)
              value
            (signal 'treesit-query-error value))
        (condition-case err
            (let ((compiled (treesit-query-compile lang query 'eager)))
              (puthash (cons lang query-source) compiled treesit--query-cache)
              compiled)
          (treesit-query-error
           (puthash (cons lang query-source) (cdr err) treesit--query-cache)
           (signal 'treesit-query-error (cdr err))))))))