treesit 已经合并进 master 分支了

唉, C-h m 就是看不见。是我没有(add-hook 'python-mode-hook 'python-ts-mode)。对比了一下tressit和tree-sitter(https://github.com/emacs-tree-sitter), 感觉emacs自带的treesit不如tree-sitter稳定。暂时先不折腾了。

只要当前 major-mode 是 xxx-ts-mode,就是用 tree-sitter 解析的

这句话不太对,现在 python-modepython-ts-mode 都是 major-mode,两者是独立的,都继承自 python-base-mode

c++-ts-mode 缩进问题在下面这个 commit 已经修复了,编译最新版本就可以了。

1 个赞

刚刚合并不久,有问题和不完善的地方是在所难免。有问题建议大佬报 Bug :grin:

看起来相当不错,我也准备来编译新版本试试看

treesitter 能干的事情很多的,语法高亮,智能缩进,基于作用域的expand-region啥的都能干。现在这个自带的xxx-ts-mode可以conditional的只允许启用某些feature吗?比如只启用高亮,不启用缩进。

emacs自带的treesit 有些语法怎么不能高亮呢?

下面是我 tree-sitter 的一些高亮设置, 现在使用 自带的treesit 又怎么来设置呢?

  (use-package tree-sitter
    :ensure t
    :custom-face
    (tree-sitter-hl-face:operator      ((t (:inherit default))))
    ;; (tree-sitter-hl-face:constant        ((t (:foreground "LimeGreen"))))
    ;; c 语言结构体
    (tree-sitter-hl-face:property        ((t (:inherit font-lock-variable-name-face))))
    (tree-sitter-hl-face:function.call ((t (:inherit font-lock-function-name-face
                                            :underline t
                                            :italic t))))
    (tree-sitter-hl-face:function.method.call ((t)))
    (tree-sitter-hl-face:method.call   ((t (:inherit font-lock-function-name-face))))
    :config
    (global-tree-sitter-mode)
    (setq tsc-dyn-get-from '(:github))
    (add-hook 'tree-sitter-after-on-hook #'tree-sitter-hl-mode))

  (use-package tree-sitter-langs
    :ensure t
    :after tree-sitter)

现在有没有可用的 go-ts-moderust-ts-mode 呢,现在已经有动态库了,可是 master 分支现在只有 js,python,c 系列几个 ts mode,在网上找了一圈,没有找到比较合适的 Go 和 Rust ts mode

Emacs官方仓库的脚本也更新了,现在可以直接用这个脚本来编译支持的几个 tree-sitter 语言邦定了。

➜  build-module pwd
~/src/emacs/admin/notes/tree-sitter/build-module
➜  build-module ./batch.sh
Building c
Building cpp
Building css
Building c-sharp
Building go
Building html
Building javascript
Building json
Building python
Building rust
Building typescript
Building tsx
➜  build-module ls dist
libtree-sitter-c-sharp.dylib  libtree-sitter-cpp.dylib  libtree-sitter-go.dylib    libtree-sitter-javascript.dylib  libtree-sitter-python.dylib  libtree-sitter-tsx.dylib
libtree-sitter-c.dylib        libtree-sitter-css.dylib  libtree-sitter-html.dylib  libtree-sitter-json.dylib        libtree-sitter-rust.dylib    libtree-sitter-typescript.dylib

然后在 Emacs 中设置 treesit-extra-load-path 就可以了。

(setq treesit-extra-load-path '("~/src/emacs/admin/notes/tree-sitter/build-module/dist"))

也可以把 dist 下面的文件拷贝到 /usr/local/lib文件夹下,就不需要设置这个变量。

sudo cp ~/src/emacs/admin/notes/tree-sitter/build-module/dist/*.dylib  /usr/local/lib/   

Windows 用户要修改下生成目标文件的后缀为 .dll

6 个赞

补充一下,~/.emacs.d/tree-sitter 目录也可以。

Emacs first looks in the directories mentioned in this variable,
then in the tree-sitter subdirectory of user-emacs-directory, and
then in the system default locations for dynamic libraries, in that order.

大佬,咨询一下,编译出来这些 binding,没有对应的 ts-mode 要怎么办呢?像 Go 和 Rust 这些,现在都只有 binding,没有 ts-mode?

官方的 tree-sitter 还没看到支持 Go 和 Rust,因为这 2 个 major-mode 不是官方维护的,得去 go-mode 和 rust-mode 问下。

现在 ruby 的支持也正在施工中 :smile:

1 个赞

现在最新的 Emacs-29 已经有 go-ts-mode 了,你可以编译最新版本试试。

https://git.savannah.gnu.org/cgit/emacs.git/commit/?h=emacs-29&id=fee2efe1b035d601ac53a32801227402e9be8bca

1 个赞

好想有一个emacs的treesitter implementation。网上有一个,感觉不是很好用,很久没更新了。也没有被官方支持。感觉大家都不感兴趣

编译了emacs-29, 可以手动开启c+±ts-mode, 如何设置默认打开c+±ts-mode?

(when (treesit-available-p)
  (push '(c++-mode . c++-ts-mode) major-mode-remap-alist)
  (push '(c-mode . c-ts-mode) major-mode-remap-alist))

其他的依此类推

7 个赞

rust-ts-mode 也进 Emacs core 了

使用 c-ts-mode 模式后, yasnippet-snippets 就不能 使用 c-mode, cc-mode 里面定义的 代码片段了。 这个问题 怎么解决呢?

在snippets目录下新建几个mode同名子目录,把原来的模板文件拷贝到新建目录里。

yas-snippet-dirs 下新建一个 c-ts-mode 的文件夹,里面新建一个文件 .yas-parents,内容为

c-mode
cc-mode

就可以复用填的这些 mode 的模板文件了,其他同理

4 个赞