Skip to content

Commit bff7bd3

Browse files
authored
Merge pull request #482 from rust-lang/treesitter
provide alternative rust-mode that derives from rust-ts-mode
2 parents 8bbe70b + 08cea61 commit bff7bd3

File tree

4 files changed

+116
-63
lines changed

4 files changed

+116
-63
lines changed

README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
- [Clippy](#clippy)
2222
- [Easy insertion of dbg!](#easy-insertion-of-dbg)
2323
- [More commands](#more-commands)
24-
- [highlighting with tree-sitter](#highlighting-with-tree-sitter)
24+
- [tree-sitter](#tree-sitter)
2525
- [LSP](#lsp)
2626
- [eglot](#eglot)
2727
- [lsp-mode](#lsp-mode)
@@ -191,9 +191,20 @@ This is bound to <kbd>C-c C-d</kbd> by default.
191191

192192
- `rust-toggle-mutability` toggle mut for var defined at current line
193193

194-
## highlighting with tree-sitter
194+
## tree-sitter
195195

196-
You should take a look at [tree-sitter](https://github.com/emacs-tree-sitter/elisp-tree-sitter). When the dependencies are installed you can activate the feature with:
196+
You can try the new native treesitter mode `rust-ts-mode` with:
197+
198+
```elisp
199+
(use-package rust-mode
200+
:init
201+
(setq rust-mode-treesitter-derive t))
202+
```
203+
204+
In case you want to use treesitter but can't use Emacs 29.1, you can
205+
take a look at
206+
[tree-sitter](https://github.com/emacs-tree-sitter/elisp-tree-sitter). When
207+
the dependencies are installed you can activate the feature with:
197208

198209
```elisp
199210
(use-package tree-sitter

rust-mode-treesitter.el

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
;;; rust-mode-treesitter.el --- use native rust-ts-mode -*-lexical-binding: t-*-
2+
;;; Commentary:
3+
4+
;; Derive from rust-ts-mode instead of prog-mode
5+
6+
;;; Code:
7+
8+
;;;###autoload
9+
(require 'treesit)
10+
(require 'rust-ts-mode)
11+
12+
(define-derived-mode rust-mode rust-ts-mode "Rust"
13+
"Major mode for Rust code.
14+
15+
\\{rust-mode-map}"
16+
:group 'rust-mode
17+
18+
(add-hook 'before-save-hook rust-before-save-hook nil t)
19+
(add-hook 'after-save-hook rust-after-save-hook nil t))
20+
21+
(provide 'rust-mode-treesitter)
22+
;;; rust-mode-treesitter.el ends here

rust-mode.el

+10-60
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ to the function arguments. When nil, `->' will be indented one level."
8888
:group 'rust-mode
8989
:safe #'booleanp)
9090

91+
(defcustom rust-mode-treesitter-derive nil
92+
"Whether rust-mode should derive from the new treesitter mode `rust-ts-mode'
93+
instead of `prog-mode'. This option requires emacs29+."
94+
:version "29.1"
95+
:type 'boolean
96+
:group 'rustic)
97+
9198
;;; Faces
9299

93100
(define-obsolete-face-alias 'rust-unsafe-face
@@ -250,66 +257,9 @@ See `prettify-symbols-compose-predicate'."
250257
map)
251258
"Keymap for Rust major mode.")
252259

253-
;;;###autoload
254-
(define-derived-mode rust-mode prog-mode "Rust"
255-
"Major mode for Rust code.
256-
257-
\\{rust-mode-map}"
258-
:group 'rust-mode
259-
:syntax-table rust-mode-syntax-table
260-
261-
;; Syntax
262-
(setq-local syntax-propertize-function #'rust-syntax-propertize)
263-
264-
;; Indentation
265-
(setq-local indent-line-function 'rust-mode-indent-line)
266-
267-
;; Fonts
268-
(setq-local font-lock-defaults
269-
'(rust-font-lock-keywords
270-
nil nil nil nil
271-
(font-lock-syntactic-face-function
272-
. rust-mode-syntactic-face-function)))
273-
274-
;; Misc
275-
(setq-local comment-start "// ")
276-
(setq-local comment-end "")
277-
(setq-local open-paren-in-column-0-is-defun-start nil)
278-
279-
;; Auto indent on }
280-
(setq-local electric-indent-chars
281-
(cons ?} (and (boundp 'electric-indent-chars)
282-
electric-indent-chars)))
283-
284-
;; Allow paragraph fills for comments
285-
(setq-local comment-start-skip "\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*")
286-
(setq-local paragraph-start
287-
(concat "[[:space:]]*\\(?:"
288-
comment-start-skip
289-
"\\|\\*/?[[:space:]]*\\|\\)$"))
290-
(setq-local paragraph-separate paragraph-start)
291-
(setq-local normal-auto-fill-function #'rust-do-auto-fill)
292-
(setq-local fill-paragraph-function #'rust-fill-paragraph)
293-
(setq-local fill-forward-paragraph-function #'rust-fill-forward-paragraph)
294-
(setq-local adaptive-fill-function #'rust-find-fill-prefix)
295-
(setq-local adaptive-fill-first-line-regexp "")
296-
(setq-local comment-multi-line t)
297-
(setq-local comment-line-break-function #'rust-comment-indent-new-line)
298-
(setq-local imenu-generic-expression rust-imenu-generic-expression)
299-
(setq-local imenu-syntax-alist '((?! . "w"))) ; For macro_rules!
300-
(setq-local beginning-of-defun-function #'rust-beginning-of-defun)
301-
(setq-local end-of-defun-function #'rust-end-of-defun)
302-
(setq-local parse-sexp-lookup-properties t)
303-
(setq-local electric-pair-inhibit-predicate
304-
#'rust-electric-pair-inhibit-predicate-wrap)
305-
(add-function :before-until (local 'electric-pair-skip-self)
306-
#'rust-electric-pair-skip-self)
307-
;; Configure prettify
308-
(setq prettify-symbols-alist rust-prettify-symbols-alist)
309-
(setq prettify-symbols-compose-predicate #'rust--prettify-symbols-compose-p)
310-
311-
(add-hook 'before-save-hook rust-before-save-hook nil t)
312-
(add-hook 'after-save-hook rust-after-save-hook nil t))
260+
(if (and (version<= "29.1" emacs-version) rust-mode-treesitter-derive)
261+
(require 'rust-mode-treesitter)
262+
(require 'rust-prog-mode))
313263

314264
;;;###autoload
315265
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))

rust-prog-mode.el

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
;;; rust-prog-mode.el --- old rust-mode without treesitter -*-lexical-binding: t-*-
2+
;;; Commentary:
3+
4+
;; rust-mode code deriving from prog-mode instead of rust-ts-mode
5+
6+
;;; Code:
7+
8+
;;;###autoload
9+
(define-derived-mode rust-mode prog-mode "Rust"
10+
"Major mode for Rust code.
11+
12+
\\{rust-mode-map}"
13+
:group 'rust-mode
14+
:syntax-table rust-mode-syntax-table
15+
16+
;; Syntax
17+
(setq-local syntax-propertize-function #'rust-syntax-propertize)
18+
19+
;; Indentation
20+
(setq-local indent-line-function 'rust-mode-indent-line)
21+
22+
;; Fonts
23+
(setq-local font-lock-defaults
24+
'(rust-font-lock-keywords
25+
nil nil nil nil
26+
(font-lock-syntactic-face-function
27+
. rust-mode-syntactic-face-function)))
28+
29+
;; Misc
30+
(setq-local comment-start "// ")
31+
(setq-local comment-end "")
32+
(setq-local open-paren-in-column-0-is-defun-start nil)
33+
34+
;; Auto indent on }
35+
(setq-local electric-indent-chars
36+
(cons ?} (and (boundp 'electric-indent-chars)
37+
electric-indent-chars)))
38+
39+
;; Allow paragraph fills for comments
40+
(setq-local comment-start-skip "\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*")
41+
(setq-local paragraph-start
42+
(concat "[[:space:]]*\\(?:"
43+
comment-start-skip
44+
"\\|\\*/?[[:space:]]*\\|\\)$"))
45+
(setq-local paragraph-separate paragraph-start)
46+
(setq-local normal-auto-fill-function #'rust-do-auto-fill)
47+
(setq-local fill-paragraph-function #'rust-fill-paragraph)
48+
(setq-local fill-forward-paragraph-function #'rust-fill-forward-paragraph)
49+
(setq-local adaptive-fill-function #'rust-find-fill-prefix)
50+
(setq-local adaptive-fill-first-line-regexp "")
51+
(setq-local comment-multi-line t)
52+
(setq-local comment-line-break-function #'rust-comment-indent-new-line)
53+
(setq-local imenu-generic-expression rust-imenu-generic-expression)
54+
(setq-local imenu-syntax-alist '((?! . "w"))) ; For macro_rules!
55+
(setq-local beginning-of-defun-function #'rust-beginning-of-defun)
56+
(setq-local end-of-defun-function #'rust-end-of-defun)
57+
(setq-local parse-sexp-lookup-properties t)
58+
(setq-local electric-pair-inhibit-predicate
59+
#'rust-electric-pair-inhibit-predicate-wrap)
60+
(add-function :before-until (local 'electric-pair-skip-self)
61+
#'rust-electric-pair-skip-self)
62+
;; Configure prettify
63+
(setq prettify-symbols-alist rust-prettify-symbols-alist)
64+
(setq prettify-symbols-compose-predicate #'rust--prettify-symbols-compose-p)
65+
66+
(add-hook 'before-save-hook rust-before-save-hook nil t)
67+
(add-hook 'after-save-hook rust-after-save-hook nil t))
68+
69+
(provide 'rust-prog-mode)
70+
;;; rust-prog-mode.el ends here

0 commit comments

Comments
 (0)