Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tree-sitter-cli dynamic library directory #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions lisp/tree-sitter-cli.el
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,50 @@
(eval-when-compile
(require 'subr-x))

(defun tree-sitter-cli-directory ()
"Return tree-sitter CLI's directory, including the ending separator.
This is the directory where the CLI tool keeps compiled lang definitions, among
other data."
(defvar tree-sitter-binary (executable-find "tree-sitter")
"Tree-sitter binary location.")

(defun tree-sitter-version ()
"Return tree-sitter CLI version."
(if tree-sitter-binary
(nth 1 (split-string
(shell-command-to-string
(concat tree-sitter-binary " -V"))))
"0"))

(defun tree-sitter-cli-config-directory ()
"Return tree-sitter CLI's config directory, including the ending separator.
This is the directory where the CLI stores the configuration file."
(file-name-as-directory
(expand-file-name
;; https://github.com/tree-sitter/tree-sitter/blob/1bad6dc/cli/src/config.rs#L20
(if-let ((dir (getenv "TREE_SITTER_DIR")))
dir
"~/.tree-sitter"))))

(defun tree-sitter-cli-bin-directory ()
(defun tree-sitter-cli-cache-directory ()
"Return tree-sitter CLI's cache directory, including the ending separator.
This is the directory where the CLI tool keeps compiled lang definitions."
(file-name-as-directory
;; https://github.com/tree-sitter/tree-sitter/blob/master/cli/loader/src/lib.rs#L110-L115
(expand-file-name "tree-sitter"
(cond
((eq system-type 'gnu/linux)
(let ((env (getenv "XDG_CACHE_HOME")))
(if (or (null env) (not (file-name-absolute-p env)))
(expand-file-name "~/.cache")
env)))
((eq system-type 'darwin)
"~/Library/Caches")
((memq system-type '(cygwin windows-nt ms-dos))
"~/AppData/Local")))))

(defun tree-sitter-cli-lib-directory ()
"Return the directory used by tree-sitter CLI to store compiled grammars."
(file-name-as-directory
(concat (tree-sitter-cli-directory) "bin")))
(if (version<= "0.20" (tree-sitter-version))
(expand-file-name "lib" (tree-sitter-cli-cache-directory))
(expand-file-name "bin" (tree-sitter-cli-config-directory)))))

(provide 'tree-sitter-cli)
;;; tree-sitter-cli.el ends here
2 changes: 1 addition & 1 deletion lisp/tree-sitter-load.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"An alist of mappings from language name symbols to language objects.
See `tree-sitter-require'.")

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

(defvar tree-sitter-load-suffixes
Expand Down