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

Is there a user customized path to highlights.scm for some lang? #54

Open
ShuguangSun opened this issue Nov 30, 2021 · 3 comments
Open
Labels
question Further information is requested

Comments

@ShuguangSun
Copy link
Contributor

For example, python, I would like to add some rule of highlight and I can modified the highlights.scm shiped with tree-sitter-langs. However, if I reintall tree-sitter-langs, it will be lost. Is it possible to customize the path to highlights.scm for some lang to somewhere else?

@shackra shackra added the question Further information is requested label Dec 2, 2021
@parkerfinch
Copy link
Contributor

I don't think this is possible right now. It looks like this feature was requested in #50.

@ShuguangSun
Copy link
Contributor Author

Thanks. I find out it can be done with tree-sitter-hl-add-patterns. An example can be found in
https://github.com/ShuguangSun/tree-sitter-ess-r

@tpeacock19
Copy link

I accomplish this with the following.

(defvar arx-tree-sitter-parser-dir "~/src/github/tree-sitter/")
(defvar arx-tree-sitter-load-dir "~/.cache/tree-sitter/lib/")
(cl-pushnew (expand-file-name arx-tree-sitter-load-dir) tree-sitter-load-path :test #'cl-equalp)
(cl-pushnew '(ess-r-mode . r) tree-sitter-major-mode-language-alist)
(tree-sitter-load 'r)

(defmacro arx-tsc-set-highlights (fn-name files)
  `(arx-defun ,fn-name ()
     (gsetq-local tree-sitter-hl-default-patterns ,(arx-read-files files))))

(defmacro arx-tsc-add-parser (lang &rest files)
  (let* ((parse-dir (expand-file-name (format "tree-sitter-%s" lang)
                                      arx-tree-sitter-parser-dir))
         (files (or files '("highlights")))
         (fn-name (intern (format "arx-tsc-add-%s-hl-default-patterns" lang)))
         highlights)
    (cl-flet* ((f (file)
                  (expand-file-name (format "queries/%s.scm" file) parse-dir)))
      (setq highlights (mapcar #'f files)))
    `(progn
       (arx-tsc-set-highlights ,fn-name ,highlights))))

(general-add-hook 'ess-r-mode-hook (arx-tsc-add-parser "r" "highlights" "locals"))

Using the highlights.scm queries from r-lib/tree-sitter-r cloned to ~/src/github/tree-sitter/tree-sitter-r. And this method works for any other repos I haved cloned there, such as tree-sitter-ledger or tree-sitter-zig.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants