tree-sitter-langs icon indicating copy to clipboard operation
tree-sitter-langs copied to clipboard

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

Open ShuguangSun opened this issue 3 years ago • 3 comments

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?

ShuguangSun avatar Nov 30 '21 06:11 ShuguangSun

I don't think this is possible right now. It looks like this feature was requested in https://github.com/emacs-tree-sitter/tree-sitter-langs/issues/50.

parkerfinch avatar Dec 13 '21 03:12 parkerfinch

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

ShuguangSun avatar Dec 13 '21 05:12 ShuguangSun

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.

tpeacock19 avatar Dec 13 '21 19:12 tpeacock19