]> git.eshelyaron.com Git - emacs.git/commitdiff
Add the keyword ':copy-queries' to 'treesit-language-source-alist'.
authorJuri Linkov <juri@linkov.net>
Wed, 23 Apr 2025 17:17:46 +0000 (20:17 +0300)
committerEshel Yaron <me@eshelyaron.com>
Wed, 23 Apr 2025 19:34:06 +0000 (21:34 +0200)
* lisp/treesit-x.el (define-treesit-generic-mode): Add keyword
':copy-queries t' to the end of 'source'.

* lisp/treesit.el (treesit-language-source-alist):
Document the keyword ':copy-queries'.
(treesit--install-language-grammar-1): Add &rest args.
Process the keyword args.  Call 'treesit--copy-queries'
when :copy-queries is non-nil.
(treesit--copy-queries): Add arg 'source-dir'.  Copy queries
from source-dir as well.  Copy only the file "highlights.scm".

(cherry picked from commit 261a965ff1a423c9dc0ee7c89974e4fc3c16b863)

lisp/treesit-x.el
lisp/treesit.el

index 1de4b9765e823df80c3db00b2556f90426e50779..b0e3863c034c291137f0cfba2012f9ec67c9bb57 100644 (file)
@@ -110,7 +110,7 @@ of `define-treesit-generic-mode'.
         (_ (pop body))))
 
     (when (stringp source)
-      (setq source (list 'quote (ensure-list source))))
+      (setq source (list 'quote (list source nil nil nil nil nil :copy-queries t))))
     (when (stringp auto-mode)
       (setq auto-mode (list 'quote (ensure-list auto-mode))))
 
index 06500c61a0327675929fe61e0934f6d49717f643..0b10b1aeabd7b6a573227618279598b7eb3de290 100644 (file)
@@ -4955,7 +4955,7 @@ window."
 
 The value should be an alist where each element has the form
 
-    (LANG . (URL REVISION SOURCE-DIR CC C++ COMMIT))
+    (LANG . (URL REVISION SOURCE-DIR CC C++ COMMIT [KEYWORD VALUE]...))
 
 Only LANG and URL are mandatory.  LANG is the language symbol.
 URL is the URL of the grammar's Git repository or a directory
@@ -4970,7 +4970,13 @@ SOURCE-DIR is the relative subdirectory in the repository in which
 the grammar's parser.c file resides, defaulting to \"src\".
 
 CC and C++ are C and C++ compilers, defaulting to \"cc\" and
-\"c++\", respectively.")
+\"c++\", respectively.
+
+The currently supported keywords:
+
+`:copy-queries' when non-nil specifies whether to copy the files
+in the \"queries\" directory from the source directory to the
+installation directory.")
 
 (defun treesit--install-language-grammar-build-recipe (lang)
   "Interactively produce a download/build recipe for LANG and return it.
@@ -5152,7 +5158,7 @@ clone if `treesit--install-language-grammar-blobless' is t."
     (apply #'treesit--call-process-signal args)))
 
 (defun treesit--install-language-grammar-1
-    (out-dir lang url &optional revision source-dir cc c++ commit)
+    (out-dir lang url &optional revision source-dir cc c++ commit &rest args)
   "Compile and install a tree-sitter language grammar library.
 
 OUT-DIR is the directory to put the compiled library file.  If it
@@ -5174,7 +5180,14 @@ If anything goes wrong, this function signals an `treesit-error'."
          (workdir (if url-is-dir
                       maybe-repo-dir
                     (expand-file-name "repo")))
-         version)
+         copy-queries version)
+
+    ;; Process the keyword args.
+    (while (keywordp (car args))
+      (pcase (pop args)
+        (:copy-queries (setq copy-queries (pop args)))
+        (_ (pop args))))
+
     (unwind-protect
         (with-temp-buffer
           (if url-is-dir
@@ -5185,7 +5198,8 @@ If anything goes wrong, this function signals an `treesit-error'."
             (treesit--git-checkout-branch workdir commit))
           (setq version (treesit--language-git-revision workdir))
           (treesit--build-grammar workdir out-dir lang source-dir cc c++)
-          (treesit--copy-queries workdir out-dir lang))
+          (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))
@@ -5264,14 +5278,18 @@ If anything goes wrong, this function signals an `treesit-error'."
         (ignore-errors (delete-file old-fname)))
       (message "Library installed to %s/%s" out-dir lib-name))))
 
-(defun treesit--copy-queries (workdir out-dir lang)
-  "Copy the LANG \"queries\" directory from WORKDIR to OUT-DIR."
-  (let* ((query-dir (expand-file-name "queries" workdir))
+(defun treesit--copy-queries (workdir out-dir lang source-dir)
+  "Copy files in LANG \"queries\" directory from WORKDIR to OUT-DIR.
+The copied query files are queries/highlights.scm."
+  (let* ((query-dir (expand-file-name
+                     (or (and source-dir (format "%s/../queries" source-dir))
+                         "queries")
+                     workdir))
          (dest-dir (expand-file-name (format "queries/%s" lang) out-dir)))
     (when (file-directory-p query-dir)
       (unless (file-directory-p dest-dir)
         (make-directory dest-dir t))
-      (dolist (file (directory-files query-dir t "\\.scm\\'" t))
+      (dolist (file (directory-files query-dir t "highlights\\.scm\\'" t))
         (copy-file file (expand-file-name (file-name-nondirectory file) dest-dir) t)))))
 
 (defcustom treesit-auto-install-grammar 'ask