Skip to content

Commit c327fe7

Browse files
committed
Allow local grammars in tree-sitter-langs-ensure
* Split the compilation logic in `tree-sitter-langs-compile` from the submodule handling
1 parent 4b9a0cc commit c327fe7

File tree

2 files changed

+65
-47
lines changed

2 files changed

+65
-47
lines changed

tree-sitter-langs-build.el

+56-43
Original file line numberDiff line numberDiff line change
@@ -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 "%sbin/%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,45 @@ 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 lang-symbol (expand-file-name sub-path) paths target)
402+
(when clean
403+
(tree-sitter-langs--call "git" "reset" "--hard" "HEAD")
404+
(tree-sitter-langs--call "git" "clean" "-f"))))
393405

394406
(cl-defun tree-sitter-langs-create-bundle (&optional clean target)
395407
"Create a bundle of language grammars.
@@ -521,16 +533,17 @@ non-nil."
521533
(when (bound-and-true-p dired-omit-mode)
522534
(dired-omit-mode -1)))))))
523535

524-
(defun tree-sitter-langs--copy-query (lang-symbol &optional force)
536+
(defun tree-sitter-langs--copy-query (lang-symbol &optional src-dir force)
525537
"Copy highlights.scm file of LANG-SYMBOL to `tree-sitter-langs--queries-dir'.
526538
This assumes the repo has already been set up, for example by
527539
`tree-sitter-langs-compile'.
528540
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"))))
541+
If the optional arg FORCE is non-nil, any existing file will be overwritten.
542+
If the optional arg SRC-DIR is non-nil, highlights will be extracted from
543+
the provided directory."
544+
(let ((src (thread-first (or src-dir (concat (tree-sitter-langs--repos-dir) (symbol-name lang-symbol)))
545+
file-name-as-directory (concat "queries")
546+
file-name-as-directory (concat "highlights.scm"))))
534547
(when (file-exists-p src)
535548
(let ((dst-dir (file-name-as-directory
536549
(concat tree-sitter-langs--queries-dir

tree-sitter-langs.el

+9-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@
6464
(unless (bound-and-true-p tree-sitter-langs--testing)
6565
(tree-sitter-langs-install-grammars :skip-if-installed)))
6666

67-
(defun tree-sitter-langs-ensure (lang-symbol)
67+
(defun tree-sitter-langs-ensure (lang-symbol &optional src-dir &rest compilation-args)
6868
"Return the language object identified by LANG-SYMBOL.
6969
If it cannot be loaded, this function tries to compile the grammar.
7070
71+
If SRC-DIR is non-nil, the language will be compiled from the provided directory.
72+
COMPILATION-ARGS will provide additional args to `tree-sitter-langs--compile'.
73+
7174
This function also tries to copy highlight query from the language repo, if it
7275
exists.
7376
@@ -79,9 +82,11 @@ See `tree-sitter-langs-repos'."
7982
(display-warning 'tree-sitter-langs
8083
(format "Could not load grammar for `%s', trying to compile it"
8184
lang-symbol))
82-
(tree-sitter-langs-compile lang-symbol)
83-
(tree-sitter-require lang-symbol)))
84-
(tree-sitter-langs--copy-query lang-symbol)))
85+
(if src-dir
86+
(apply #'tree-sitter-langs--compile lang-symbol src-dir compilation-args)
87+
(tree-sitter-langs-compile lang-symbol))
88+
(tree-sitter-require lang-symbol)
89+
(tree-sitter-langs--copy-query lang-symbol src-dir)))))
8590

8691
;;;###autoload
8792
(defun tree-sitter-langs--init-load-path (&rest _args)

0 commit comments

Comments
 (0)