Skip to content

updated prelude rust module #1388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Replace `yank-pop` key-binding to `counse-yank-pop` for `ivy-mode`.
* The keybinding for `proced` is now enabled unconditionally.
* Replace prelude-go backend with `lsp` instead of unmaintained tools
* Use `rust-analyzer` as language server for prelude-rust and provide nicer syntax highlighting with `tree-sitter`

### Bugs fixed

Expand Down
8 changes: 6 additions & 2 deletions docs/modules/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ following packages in your system:
* `rustc` (Rust compiler)
* `cargo` (Rust package manager)
* `rustfmt` (Rust tool for formatting code)
* `racer` (Rust completion tool, not necessary if `prelude-lsp` is enabled)
* `rls` (Rust Language Server, if the `prelude-lsp` feature is enabled)
* `rust-analyzer` (Rust Language Server, required for `prelude-lsp` feature)

## Rust Mode

Emacs comes with Rust programming support through the built-in
`rust-mode`. Whenever you are editing Rust code run <kbd>C-h m</kbd> to
look at the Rust mode key bindings.

## Syntax highlighting

[tree-sitter-mode](https://emacs-tree-sitter.github.io/) is used for nicer
syntax highlighting.

## Syntax checking

Prelude ships with [Flycheck](https://github.com/flycheck/flycheck),
Expand Down
42 changes: 27 additions & 15 deletions modules/prelude-rust.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; prelude-rust.el --- Emacs Prelude: Rust programming support.
;;
;; Authors: Doug MacEachern, Manoel Vilela, Ben Alex
;; Authors: Doug MacEachern, Manoel Vilela, Ben Alex, Daniel Gerlach

;; This file is not part of GNU Emacs.

Expand Down Expand Up @@ -32,37 +32,49 @@
;; You may need to install the following packages on your system:
;; * rustc (Rust Compiler)
;; * cargo (Rust Package Manager)
;; * racer (Rust Completion Tool)
;; * rustfmt (Rust Tool for formatting code)
;; * rls (Rust Language Server, if the prelude-lsp feature is enabled)
;; * rust-analyzer as lsp server needs to be in global path, see:
;; https://rust-analyzer.github.io/manual.html#rust-analyzer-language-server-binary


(prelude-require-packages '(rust-mode
cargo
flycheck-rust
tree-sitter
tree-sitter-langs
yasnippet
ron-mode))

(unless (featurep 'prelude-lsp)
(prelude-require-packages '(racer)))

(setq rust-format-on-save t)
(require 'tree-sitter)
(require 'tree-sitter-langs)

(with-eval-after-load 'rust-mode
(add-hook 'rust-mode-hook 'cargo-minor-mode)
(add-hook 'flycheck-mode-hook 'flycheck-rust-setup)

(if (featurep 'prelude-lsp)
(add-hook 'rust-mode-hook 'lsp)
(add-hook 'rust-mode-hook 'racer-mode)
(add-hook 'racer-mode-hook 'eldoc-mode))
;; enable lsp for rust, by default it uses rust-analyzer as lsp server
(add-hook 'rust-mode-hook 'lsp)

;; enable tree-sitter for nicer syntax highlighting
(add-hook 'rust-mode-hook #'tree-sitter-mode)
(add-hook 'rust-mode-hook #'tree-sitter-hl-mode)

(defun prelude-rust-mode-defaults ()
(unless (featurep 'prelude-lsp)
(local-set-key (kbd "C-c C-d") 'racer-describe)
(local-set-key (kbd "C-c .") 'racer-find-definition)
(local-set-key (kbd "C-c ,") 'pop-tag-mark))
;; format on save
(setq rust-format-on-save t)

;; lsp settings
(setq
;; enable macro expansion
lsp-rust-analyzer-proc-macro-enable t
lsp-rust-analyzer-experimental-proc-attr-macros t)

;; Prevent #! from chmodding rust files to be executable
(remove-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p)

;; snippets are required for correct lsp autocompletions
(yas-minor-mode)

;; CamelCase aware editing operations
(subword-mode +1))

Expand Down