Skip to content

Commit 92e3f16

Browse files
committed
fix tree-sitter-cli dynamic library directory
1 parent 3cfab8a commit 92e3f16

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

lisp/tree-sitter-cli.el

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,51 @@
1616
(eval-when-compile
1717
(require 'subr-x))
1818

19-
(defun tree-sitter-cli-directory ()
20-
"Return tree-sitter CLI's directory, including the ending separator.
21-
This is the directory where the CLI tool keeps compiled lang definitions, among
22-
other data."
19+
(defvar tree-sitter-version (nth 1 (split-string (shell-command-to-string "tree-sitter -V")))
20+
"Return tree-sitter CLI version.")
21+
22+
(defvar tree-sitter-version-alist
23+
(let ((versions (split-string tree-sitter-version "\\.")))
24+
(list
25+
(cons 'major (string-to-number (nth 0 versions)))
26+
(cons 'minor (string-to-number (nth 1 versions)))
27+
(cons 'patch (string-to-number (nth 2 versions)))))
28+
"Return alist of tree-sitter CLI semantic versioning.")
29+
30+
(defun tree-sitter-cli-config-directory ()
31+
"Return tree-sitter CLI's config directory, including the ending separator.
32+
This is the directory where the CLI stores the configuration file."
2333
(file-name-as-directory
2434
(expand-file-name
2535
;; https://github.com/tree-sitter/tree-sitter/blob/1bad6dc/cli/src/config.rs#L20
2636
(if-let ((dir (getenv "TREE_SITTER_DIR")))
2737
dir
2838
"~/.tree-sitter"))))
2939

30-
(defun tree-sitter-cli-bin-directory ()
40+
(defun tree-sitter-cli-cache-directory ()
41+
"Return tree-sitter CLI's cache directory, including the ending separator.
42+
This is the directory where the CLI tool keeps compiled lang definitions."
43+
(file-name-as-directory
44+
;; https://github.com/tree-sitter/tree-sitter/blob/master/cli/loader/src/lib.rs#L110-L115
45+
(expand-file-name "tree-sitter"
46+
(cond
47+
((eq system-type 'gnu/linux)
48+
(let ((env (getenv "XDG_CACHE_HOME")))
49+
(if (or (null env) (not (file-name-absolute-p env)))
50+
(expand-file-name "~/.cache")
51+
env)))
52+
((eq system-type 'darwin)
53+
"~/Library/Caches")
54+
((memq system-type '(cygwin windows-nt ms-dos))
55+
"~/AppData/Local")))))
56+
57+
(defun tree-sitter-cli-lib-directory ()
3158
"Return the directory used by tree-sitter CLI to store compiled grammars."
3259
(file-name-as-directory
33-
(concat (tree-sitter-cli-directory) "bin")))
60+
(if (and (equal (alist-get 'major tree-sitter-version-alist) 0)
61+
(< (alist-get 'minor tree-sitter-version-alist) 20))
62+
(expand-file-name "bin" (tree-sitter-cli-config-directory))
63+
(expand-file-name "lib" (tree-sitter-cli-cache-directory)))))
3464

3565
(provide 'tree-sitter-cli)
3666
;;; tree-sitter-cli.el ends here

lisp/tree-sitter-load.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"An alist of mappings from language name symbols to language objects.
2626
See `tree-sitter-require'.")
2727

28-
(defvar tree-sitter-load-path (list (tree-sitter-cli-bin-directory))
28+
(defvar tree-sitter-load-path (list (tree-sitter-cli-lib-directory))
2929
"List of directories to search for shared libraries that define languages.")
3030

3131
(defvar tree-sitter-load-suffixes

0 commit comments

Comments
 (0)