[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/rust-mode eba544147a 3/3: Merge pull request #429 from Chr
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/rust-mode eba544147a 3/3: Merge pull request #429 from Chris00/prettify |
Date: |
Sun, 26 Dec 2021 13:58:24 -0500 (EST) |
branch: elpa/rust-mode
commit eba544147aa6769f4f0e153e9f7373801dd2db89
Merge: 65f96278b2 f14fa86a5c
Author: brotzeit <brotzeitmacher@gmail.com>
Commit: GitHub <noreply@github.com>
Merge pull request #429 from Chris00/prettify
Enable the use of prettify-symbols-mode
---
README.md | 15 +++++++++++++++
rust-mode.el | 23 +++++++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/README.md b/README.md
index 46c48ea93b..e20d7e4f20 100644
--- a/README.md
+++ b/README.md
@@ -129,6 +129,21 @@ on save:
(setq rust-format-on-save t)
```
+### Prettifying
+
+You can toggle prettification of your code by running `M-x
+prettify-symbols-mode`. If you'd like to automatically enable this
+for all rust files, add the following to your init.el.
+
+```elisp
+(add-hook 'rust-mode-hook
+ (lambda () (prettify-symbols-mode)))
+```
+
+You can add your own prettifications to `rust-prettify-symbols-alist`.
+For example, to display `x.add(y)` as `x∔(y)`, simply add to your init
+file `(push '(".add" . ?∔) rust-prettify-symbols-alist)`.
+
### Running / testing / compiling code
The `rust-run`, `rust-test`, `rust-compile` and `rust-check` functions
diff --git a/rust-mode.el b/rust-mode.el
index 57feaaa59a..eec6057fb0 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -41,6 +41,12 @@ This variable might soon be remove again.")
:type 'function
:group 'rust-mode)
+(defvar rust-prettify-symbols-alist
+ '(("&&" . ?∧) ("||" . ?∨)
+ ("<=" . ?≤) (">=" . ?≥) ("!=" . ?≠)
+ ("INFINITY" . ?∞) ("->" . ?→) ("=>" . ?⇒))
+ "Alist of symbol prettifications used for `prettify-symbols-alist'.")
+
;;; Customization
(defgroup rust-mode nil
@@ -209,6 +215,20 @@ Use idomenu (imenu with `ido-mode') for best mileage.")
table)
"Syntax definitions and helpers.")
+;;; Prettify
+
+(defun rust--prettify-symbols-compose-p (start end match)
+ "Return true iff the symbol MATCH should be composed.
+See `prettify-symbols-compose-predicate'."
+ (and (fboundp 'prettify-symbols-default-compose-p)
+ (prettify-symbols-default-compose-p start end match)
+ ;; Make sure there is a space before || as it is also used for
+ ;; functions with 0 arguments.
+ (not (and (string= match "||")
+ (save-excursion
+ (goto-char start)
+ (looking-back "\\(?:\\<move\\|=\\) *"))))))
+
;;; Mode
(defvar rust-mode-map
@@ -278,6 +298,9 @@ Use idomenu (imenu with `ido-mode') for best mileage.")
(setq-local electric-pair-inhibit-predicate
'rust-electric-pair-inhibit-predicate-wrap)
(setq-local electric-pair-skip-self 'rust-electric-pair-skip-self-wrap)
+ ;; 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))