From f74dd1cd87987ea7faf0cfc6240c2284ef9133cb Mon Sep 17 00:00:00 2001 From: brotzeit Date: Tue, 27 Dec 2022 19:16:44 +0100 Subject: [PATCH 1/2] provide alternative rust-mode that derives from rust-ts-mode --- README.md | 17 ++++++++-- rust-mode-treesitter.el | 19 +++++++++++ rust-mode.el | 70 ++++++----------------------------------- rust-prog-mode.el | 70 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 63 deletions(-) create mode 100644 rust-mode-treesitter.el create mode 100644 rust-prog-mode.el diff --git a/README.md b/README.md index 01dc5e8..217b990 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ - [Clippy](#clippy) - [Easy insertion of dbg!](#easy-insertion-of-dbg) - [More commands](#more-commands) - - [highlighting with tree-sitter](#highlighting-with-tree-sitter) + - [tree-sitter](#tree-sitter) - [LSP](#lsp) - [eglot](#eglot) - [lsp-mode](#lsp-mode) @@ -191,9 +191,20 @@ This is bound to C-c C-d by default. - `rust-toggle-mutability` toggle mut for var defined at current line -## highlighting with tree-sitter +## tree-sitter -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: +You can try the new native treesitter mode `rust-ts-mode` with: + +```elisp +(use-package rust-mode + :init + (setq rust-mode-treesitter-derive t)) +``` + +In case you want to use treesitter but can't use emacs master, you can +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: ```elisp (use-package tree-sitter diff --git a/rust-mode-treesitter.el b/rust-mode-treesitter.el new file mode 100644 index 0000000..1183e98 --- /dev/null +++ b/rust-mode-treesitter.el @@ -0,0 +1,19 @@ +;;; rust-mode-treesitter.el --- use native rust-ts-mode -*-lexical-binding: t-*- +;;; Commentary: + +;; Derive from rust-ts-mode instead of prog-mode + +;;; Code: + +;;;###autoload +(define-derived-mode rust-mode rust-ts-mode "Rust" + "Major mode for Rust code. + +\\{rust-mode-map}" + :group 'rust-mode + + (add-hook 'before-save-hook rust-before-save-hook nil t) + (add-hook 'after-save-hook rust-after-save-hook nil t)) + +(provide 'rust-mode-treesitter) +;;; rust-mode-treesitter.el ends here diff --git a/rust-mode.el b/rust-mode.el index 4bb1bcb..0c68c43 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -88,6 +88,13 @@ to the function arguments. When nil, `->' will be indented one level." :group 'rust-mode :safe #'booleanp) +(defcustom rust-mode-treesitter-derive nil + "Whether rust-mode should derive from the new treesitter mode `rust-ts-mode' +instead of `prog-mode'. This option requires emacs29+." + :version "29.1" + :type 'boolean + :group 'rustic) + ;;; Faces (define-obsolete-face-alias 'rust-unsafe-face @@ -250,66 +257,9 @@ See `prettify-symbols-compose-predicate'." map) "Keymap for Rust major mode.") -;;;###autoload -(define-derived-mode rust-mode prog-mode "Rust" - "Major mode for Rust code. - -\\{rust-mode-map}" - :group 'rust-mode - :syntax-table rust-mode-syntax-table - - ;; Syntax - (setq-local syntax-propertize-function #'rust-syntax-propertize) - - ;; Indentation - (setq-local indent-line-function 'rust-mode-indent-line) - - ;; Fonts - (setq-local font-lock-defaults - '(rust-font-lock-keywords - nil nil nil nil - (font-lock-syntactic-face-function - . rust-mode-syntactic-face-function))) - - ;; Misc - (setq-local comment-start "// ") - (setq-local comment-end "") - (setq-local open-paren-in-column-0-is-defun-start nil) - - ;; Auto indent on } - (setq-local electric-indent-chars - (cons ?} (and (boundp 'electric-indent-chars) - electric-indent-chars))) - - ;; Allow paragraph fills for comments - (setq-local comment-start-skip "\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*") - (setq-local paragraph-start - (concat "[[:space:]]*\\(?:" - comment-start-skip - "\\|\\*/?[[:space:]]*\\|\\)$")) - (setq-local paragraph-separate paragraph-start) - (setq-local normal-auto-fill-function #'rust-do-auto-fill) - (setq-local fill-paragraph-function #'rust-fill-paragraph) - (setq-local fill-forward-paragraph-function #'rust-fill-forward-paragraph) - (setq-local adaptive-fill-function #'rust-find-fill-prefix) - (setq-local adaptive-fill-first-line-regexp "") - (setq-local comment-multi-line t) - (setq-local comment-line-break-function #'rust-comment-indent-new-line) - (setq-local imenu-generic-expression rust-imenu-generic-expression) - (setq-local imenu-syntax-alist '((?! . "w"))) ; For macro_rules! - (setq-local beginning-of-defun-function #'rust-beginning-of-defun) - (setq-local end-of-defun-function #'rust-end-of-defun) - (setq-local parse-sexp-lookup-properties t) - (setq-local electric-pair-inhibit-predicate - #'rust-electric-pair-inhibit-predicate-wrap) - (add-function :before-until (local 'electric-pair-skip-self) - #'rust-electric-pair-skip-self) - ;; Configure prettify - (setq prettify-symbols-alist rust-prettify-symbols-alist) - (setq prettify-symbols-compose-predicate #'rust--prettify-symbols-compose-p) - - (add-hook 'before-save-hook rust-before-save-hook nil t) - (add-hook 'after-save-hook rust-after-save-hook nil t)) +(if (and (version<= "29.1" emacs-version) rust-mode-treesitter-derive) + (require 'rust-mode-treesitter) + (require 'rust-prog-mode)) ;;;###autoload (add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode)) diff --git a/rust-prog-mode.el b/rust-prog-mode.el new file mode 100644 index 0000000..f443da8 --- /dev/null +++ b/rust-prog-mode.el @@ -0,0 +1,70 @@ +;;; rust-prog-mode.el --- old rust-mode without treesitter -*-lexical-binding: t-*- +;;; Commentary: + +;; rust-mode code deriving from prog-mode instead of rust-ts-mode + +;;; Code: + +;;;###autoload +(define-derived-mode rust-mode prog-mode "Rust" + "Major mode for Rust code. + +\\{rust-mode-map}" + :group 'rust-mode + :syntax-table rust-mode-syntax-table + + ;; Syntax + (setq-local syntax-propertize-function #'rust-syntax-propertize) + + ;; Indentation + (setq-local indent-line-function 'rust-mode-indent-line) + + ;; Fonts + (setq-local font-lock-defaults + '(rust-font-lock-keywords + nil nil nil nil + (font-lock-syntactic-face-function + . rust-mode-syntactic-face-function))) + + ;; Misc + (setq-local comment-start "// ") + (setq-local comment-end "") + (setq-local open-paren-in-column-0-is-defun-start nil) + + ;; Auto indent on } + (setq-local electric-indent-chars + (cons ?} (and (boundp 'electric-indent-chars) + electric-indent-chars))) + + ;; Allow paragraph fills for comments + (setq-local comment-start-skip "\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*") + (setq-local paragraph-start + (concat "[[:space:]]*\\(?:" + comment-start-skip + "\\|\\*/?[[:space:]]*\\|\\)$")) + (setq-local paragraph-separate paragraph-start) + (setq-local normal-auto-fill-function #'rust-do-auto-fill) + (setq-local fill-paragraph-function #'rust-fill-paragraph) + (setq-local fill-forward-paragraph-function #'rust-fill-forward-paragraph) + (setq-local adaptive-fill-function #'rust-find-fill-prefix) + (setq-local adaptive-fill-first-line-regexp "") + (setq-local comment-multi-line t) + (setq-local comment-line-break-function #'rust-comment-indent-new-line) + (setq-local imenu-generic-expression rust-imenu-generic-expression) + (setq-local imenu-syntax-alist '((?! . "w"))) ; For macro_rules! + (setq-local beginning-of-defun-function #'rust-beginning-of-defun) + (setq-local end-of-defun-function #'rust-end-of-defun) + (setq-local parse-sexp-lookup-properties t) + (setq-local electric-pair-inhibit-predicate + #'rust-electric-pair-inhibit-predicate-wrap) + (add-function :before-until (local 'electric-pair-skip-self) + #'rust-electric-pair-skip-self) + ;; Configure prettify + (setq prettify-symbols-alist rust-prettify-symbols-alist) + (setq prettify-symbols-compose-predicate #'rust--prettify-symbols-compose-p) + + (add-hook 'before-save-hook rust-before-save-hook nil t) + (add-hook 'after-save-hook rust-after-save-hook nil t)) + +(provide 'rust-prog-mode) +;;; rust-prog-mode.el ends here \ No newline at end of file From 08cea61390971d26b982677bc0658a9ff79b34e0 Mon Sep 17 00:00:00 2001 From: Sibi Prabakaran Date: Sat, 17 Feb 2024 10:44:38 +0530 Subject: [PATCH 2/2] Update docs and fix treesiter mode --- README.md | 2 +- rust-mode-treesitter.el | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 217b990..2b77636 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,7 @@ You can try the new native treesitter mode `rust-ts-mode` with: (setq rust-mode-treesitter-derive t)) ``` -In case you want to use treesitter but can't use emacs master, you can +In case you want to use treesitter but can't use Emacs 29.1, you can 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: diff --git a/rust-mode-treesitter.el b/rust-mode-treesitter.el index 1183e98..3c3bd57 100644 --- a/rust-mode-treesitter.el +++ b/rust-mode-treesitter.el @@ -6,6 +6,9 @@ ;;; Code: ;;;###autoload +(require 'treesit) +(require 'rust-ts-mode) + (define-derived-mode rust-mode rust-ts-mode "Rust" "Major mode for Rust code.