@@ -276,16 +276,13 @@ If VERSION and OS are not spcified, use the defaults of
276276 (format " tree-sitter-grammars-%s -%s .tar%s "
277277 os version ext))
278278
279- (defun tree-sitter-langs-compile (lang-symbol &optional clean target )
280- " Download and compile the grammar for LANG-SYMBOL.
281- This function requires git and tree-sitter CLI.
279+ (defun tree-sitter-langs--compile (lang-symbol dir &optional paths target )
280+ " Download and compile the grammar at DIR for LANG-SYMBOL.
282281
283- If the optional arg CLEAN is non-nil, compile from the revision recorded in this
284- project (through git submodules), and clean up afterwards. Otherwise, compile
285- from the current state of the grammar repo, without cleanup."
286- (message " [tree-sitter-langs] Processing %s " lang-symbol)
287- (unless (executable-find " git" )
288- (error " Could not find git (needed to download grammars) " ))
282+ If a directory contains multiple subgrammars, PATHS may be used to specify them.
283+ TARGET allows a cross-compilation target to be specified.
284+
285+ This function requires tree-sitter CLI."
289286 (unless (executable-find " tree-sitter" )
290287 (error " Could not find tree-sitter executable (needed to compile grammars) " ))
291288 (setq target
@@ -294,32 +291,11 @@ from the current state of the grammar repo, without cleanup."
294291 (" aarch64-apple-darwin" " arm64-apple-macos11" )
295292 (" nil" nil )
296293 (_ (error " Unsupported cross-compilation target %s " target))))
297- (let* ((source (tree-sitter-langs--source lang-symbol))
298- (dir (if source
299- (file-name-as-directory
300- (concat (tree-sitter-langs--repos-dir)
301- (symbol-name lang-symbol)))
302- (error " Unknown language `%s' " lang-symbol)))
303- (sub-path (format " repos/%s " lang-symbol))
304- (status (tree-sitter-langs--repo-status lang-symbol))
305- (paths (plist-get source :paths ))
294+ (let* ((paths (or paths '(" " )))
306295 (bin-dir (tree-sitter-langs--bin-dir))
307296 (tree-sitter-langs--out (tree-sitter-langs--buffer
308297 (format " *tree-sitter-langs-compile %s * " lang-symbol))))
309- (let ((default-directory tree-sitter-langs-git-dir))
310- (pcase status
311- (:uninitialized
312- (tree-sitter-langs--call " git" " submodule" " update" " --init" " --checkout" " --" sub-path))
313- (:modified
314- (when clean
315- (let ((default-directory dir))
316- (tree-sitter-langs--call " git" " stash" " push" ))
317- (tree-sitter-langs--call " git" " submodule" " update" " --init" " --checkout" " --force" " --" sub-path)))
318- (:conflicts
319- (error " Unresolved conflicts in %s " dir))
320- (:synchronized nil )
321- (_
322- (error " Weird status from git-submodule '%s ' " status))))
298+
323299 (let ((default-directory dir))
324300 (when (member lang-symbol tree-sitter-langs--langs-with-deps)
325301 (tree-sitter-langs--call " npm" " set" " progress=false" )
@@ -365,7 +341,8 @@ from the current state of the grammar repo, without cleanup."
365341 " src/parser.c"
366342 " -o" (format " %s bin/%s .so" tree-sitter-langs-grammar-dir lang-symbol)
367343 " -target" target))))
368- (:default (tree-sitter-langs--call " tree-sitter" " test" )))))
344+ (:default
345+ (tree-sitter-langs--call " tree-sitter" " test" )))))
369346 ; ; Replace underscores with hyphens. Example: c_sharp.
370347 (let ((default-directory bin-dir))
371348 (dolist (file (directory-files default-directory))
@@ -386,10 +363,49 @@ from the current state of the grammar repo, without cleanup."
386363 (let ((new-name (concat (file-name-base file) " .dylib" )))
387364 (when (file-exists-p new-name)
388365 (delete-file new-name))
389- (rename-file file new-name))))))
390- (when clean
391- (tree-sitter-langs--call " git" " reset" " --hard" " HEAD" )
392- (tree-sitter-langs--call " git" " clean" " -f" )))))
366+ (rename-file file new-name)))))))))
367+
368+ (defun tree-sitter-langs-compile (lang-symbol &optional clean target )
369+ " Download and compile the grammar for LANG-SYMBOL.
370+ This function requires git and tree-sitter CLI.
371+
372+ If the optional arg CLEAN is non-nil, compile from the revision recorded in this
373+ project (through git submodules), and clean up afterwards. Otherwise, compile
374+ from the current state of the grammar repo, without cleanup."
375+ (message " [tree-sitter-langs] Processing %s " lang-symbol)
376+ (unless (executable-find " git" )
377+ (error " Could not find git (needed to download grammars) " ))
378+ (let* ((source (tree-sitter-langs--source lang-symbol))
379+ (dir (if source
380+ (file-name-as-directory
381+ (concat (tree-sitter-langs--repos-dir)
382+ (symbol-name lang-symbol)))
383+ (error " Unknown language `%s' " lang-symbol)))
384+ (sub-path (format " repos/%s " lang-symbol))
385+ (status (tree-sitter-langs--repo-status lang-symbol))
386+ (paths (plist-get source :paths )))
387+ (let ((default-directory tree-sitter-langs-git-dir))
388+ (pcase status
389+ (:uninitialized
390+ (tree-sitter-langs--call " git" " submodule" " update" " --init" " --checkout" " --" sub-path))
391+ (:modified
392+ (when clean
393+ (let ((default-directory dir))
394+ (tree-sitter-langs--call " git" " stash" " push" ))
395+ (tree-sitter-langs--call " git" " submodule" " update" " --init" " --checkout" " --force" " --" sub-path)))
396+ (:conflicts
397+ (error " Unresolved conflicts in %s " dir))
398+ (:synchronized nil )
399+ (_
400+ (error " Weird status from git-submodule '%s ' " status))))
401+ (tree-sitter-langs--compile
402+ lang-symbol
403+ (file-name-concat tree-sitter-langs-git-dir sub-path)
404+ paths
405+ target)
406+ (when clean
407+ (tree-sitter-langs--call " git" " reset" " --hard" " HEAD" )
408+ (tree-sitter-langs--call " git" " clean" " -f" ))))
393409
394410(cl-defun tree-sitter-langs-create-bundle (&optional clean target )
395411 " Create a bundle of language grammars.
@@ -521,16 +537,17 @@ non-nil."
521537 (when (bound-and-true-p dired-omit-mode)
522538 (dired-omit-mode -1 )))))))
523539
524- (defun tree-sitter-langs--copy-query (lang-symbol &optional force )
540+ (defun tree-sitter-langs--copy-query (lang-symbol &optional src-dir force )
525541 " Copy highlights.scm file of LANG-SYMBOL to `tree-sitter-langs--queries-dir' .
526542This assumes the repo has already been set up, for example by
527543`tree-sitter-langs-compile' .
528544
529- If the optional arg FORCE is non-nil, any existing file will be overwritten."
530- (let ((src (thread-first (tree-sitter-langs--repos-dir)
531- (concat (symbol-name lang-symbol))
532- file-name-as-directory (concat " queries" )
533- file-name-as-directory (concat " highlights.scm" ))))
545+ If the optional arg FORCE is non-nil, any existing file will be overwritten.
546+ If the optional arg SRC-DIR is non-nil, highlights will be extracted from
547+ the provided directory."
548+ (let ((src (thread-first (or src-dir (concat (tree-sitter-langs--repos-dir) (symbol-name lang-symbol)))
549+ file-name-as-directory (concat " queries" )
550+ file-name-as-directory (concat " highlights.scm" ))))
534551 (when (file-exists-p src)
535552 (let ((dst-dir (file-name-as-directory
536553 (concat tree-sitter-langs--queries-dir
0 commit comments