Function: treesit--install-language-grammar-1

treesit--install-language-grammar-1 is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit--install-language-grammar-1 OUT-DIR LANG URL &rest ARGS)

Documentation

Compile and install a tree-sitter language grammar library.

OUT-DIR is the directory to put the compiled library file. If it is nil, the "tree-sitter" directory under user's Emacs configuration directory is used (and automatically created if it does not exist).

For ARGS, see treesit-language-source-alist.

Return the git revision of the installed grammar. The revision is generated by "git describe". It only works when treesit--install-language-grammar-full-clone is t.

If anything goes wrong, this function signals an treesit-error.

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit--install-language-grammar-1
    (out-dir lang url &rest args)
  "Compile and install a tree-sitter language grammar library.

OUT-DIR is the directory to put the compiled library file.  If it
is nil, the \"tree-sitter\" directory under user's Emacs
configuration directory is used (and automatically created if it
does not exist).

For ARGS, see `treesit-language-source-alist'.

Return the git revision of the installed grammar.  The revision is
generated by \"git describe\".  It only works when
`treesit--install-language-grammar-full-clone' is t.

If anything goes wrong, this function signals an `treesit-error'."
  (let* ((default-directory (make-temp-file "treesit-workdir" t))
         (maybe-repo-dir (expand-file-name url))
         (url-is-dir (file-accessible-directory-p maybe-repo-dir))
         (workdir (if url-is-dir
                      maybe-repo-dir
                    (expand-file-name "repo")))
         version
         revision source-dir cc c++ commit copy-queries)

    ;; Process the keyword args.
    (while (keywordp (car args))
      (pcase (pop args)
        (:revision     (setq revision     (pop args)))
        (:source-dir   (setq source-dir   (pop args)))
        (:cc           (setq cc           (pop args)))
        (:c++          (setq c++          (pop args)))
        (:commit       (setq commit       (pop args)))
        (:copy-queries (setq copy-queries (pop args)))))

    ;; Old positional convention for backward-compatibility.
    (unless revision   (setq revision   (nth 0 args)))
    (unless source-dir (setq source-dir (nth 1 args)))
    (unless cc         (setq cc         (nth 2 args)))
    (unless c++        (setq c++        (nth 3 args)))
    (unless commit     (setq commit     (nth 4 args)))

    (unwind-protect
        (with-temp-buffer
          (if url-is-dir
              (when revision
                (treesit--git-checkout-branch workdir revision))
            (if commit
                ;; Force blobless full clone to be able later
                ;; to checkout a commit (bug#78542).
                (let ((treesit--install-language-grammar-full-clone t)
                      (treesit--install-language-grammar-blobless t))
                  (treesit--git-clone-repo url revision workdir))
              (treesit--git-clone-repo url revision workdir)))
          (when commit
            (treesit--git-checkout-branch workdir commit))
          (setq version (treesit--language-git-revision workdir))
          (treesit--build-grammar workdir out-dir lang source-dir cc c++)
          (when copy-queries
            (treesit--copy-queries workdir out-dir lang source-dir)))
      ;; Remove workdir if it's not a repo owned by user and we
      ;; managed to create it in the first place.
      (when (and (not url-is-dir) (file-exists-p workdir))
        (delete-directory workdir t)))
    version))