treesit 已经合并进 master 分支了

感谢分享。设置 treesit-language-source-alist 这个变量后,就可以直接在 Emacs 里安装相关的 parser 了

(setq treesit-language-source-alist
      '((bash       . ("https://github.com/tree-sitter/tree-sitter-bash.git"))
        (c          . ("https://github.com/tree-sitter/tree-sitter-c.git"))
        (cmake      . ("https://github.com/uyha/tree-sitter-cmake.git"))
        (cpp        . ("https://github.com/tree-sitter/tree-sitter-cpp.git"))
        (csharp     . ("https://github.com/tree-sitter/tree-sitter-c-sharp.git"))
        (css        . ("https://github.com/tree-sitter/tree-sitter-css.git"))
        (dockerfile . ("https://github.com/camdencheek/tree-sitter-dockerfile.git"))
        (go         . ("https://github.com/tree-sitter/tree-sitter-go.git"))
        (gomod      . ("https://github.com/camdencheek/tree-sitter-go-mod.git"))
        (html       . ("https://github.com/tree-sitter/tree-sitter-html.git"))
        (java       . ("https://github.com/tree-sitter/tree-sitter-java.git"))
        (javascript . ("https://github.com/tree-sitter/tree-sitter-javascript.git"))
        (json       . ("https://github.com/tree-sitter/tree-sitter-json.git"))
        (python     . ("https://github.com/tree-sitter/tree-sitter-python.git"))
        (ruby       . ("https://github.com/tree-sitter/tree-sitter-ruby.git"))
        (rust       . ("https://github.com/tree-sitter/tree-sitter-rust.git"))
        (toml       . ("https://github.com/tree-sitter/tree-sitter-toml.git"))
        (tsx        . ("https://github.com/tree-sitter/tree-sitter-typescript.git" nil "tsx/src"))
        (typescript . ("https://github.com/tree-sitter/tree-sitter-typescript.git" nil "typescript/src"))
        (yaml       . ("https://github.com/ikatyang/tree-sitter-yaml.git"))))

这样直接在 Emacs 里 M-x treesit-install-language-grammar 就可以了,不用每次都要到 admin/notes/xxx 里去手动安装了,这样默认会装在 (user-emacs-directory)/tree-sitter 下面

不过现在 Emacs 更新后,auto-mode-alist 里没了 xxx-ts-mode,还得自己手动加, 以 go-ts-mode 为例

虽然 mode 文件里有写这么一行,但这不应该是调用了 go-ts-mode 后才会生效的吗?除了提前在自己的配置文件里加上 (add-to-list 'auto-mode-alist '("\\.go\\'" . go-ts-mode)),还有啥好的解决办法么


随手糊了一个,但治标不治本,现在将就用

(dolist (list `((cmake      . (,(rx (or "CMakeLists.txt" ".cmake") eos) . cmake-ts-mode))
                (dockerfile . (,(rx (or (seq "Dockerfile" (opt "." (zero-or-more nonl))) (seq "." (any "Dd") "ockerfile")) eos) . dockerfile-ts-mode))
                (go         . (,(rx ".go" eos) . go-ts-mode))
                (gomod      . (,(rx "/go.mod" eos) . go-mod-ts-mode))
                (rust       . (,(rx ".rs" eos) . rust-ts-mode))
                (tsx        . (,(rx ".tsx" eos) . tsx-ts-mode))
                (typescript . (,(rx ".ts" eos) . typescript-ts-mode))
                (yaml       . (,(rx ".y" (opt "a") "ml" eos) . yaml-ts-mode))))
  (let ((parser (car list))
        (alist (cdr list)))
    (when (treesit-ready-p parser)
      (add-to-list 'auto-mode-alist alist))))
3 个赞