summaryrefslogtreecommitdiff
path: root/rust-mode.el
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2017-07-30 23:13:09 +0100
committerWilfred Hughes <me@wilfred.me.uk>2017-07-30 23:31:01 +0100
commit2a9fcd033b18bec27bbdeecc1397f2ddf4092bb1 (patch)
tree87169c668cc8b1d88f7299052cefea15093cd528 /rust-mode.el
parent09efc45dee786cb220c233426ddd82b26ad950eb (diff)
downloadrust-mode-2a9fcd033b18bec27bbdeecc1397f2ddf4092bb1.tar.gz
Add syntax highlighting and imenu support for `union`
`union` is a contextual keyword, so highlight it in the correct context. Otherwise, treat `union` similarly to `struct`.
Diffstat (limited to 'rust-mode.el')
-rw-r--r--rust-mode.el24
1 files changed, 17 insertions, 7 deletions
diff --git a/rust-mode.el b/rust-mode.el
index eac246f..46231fd 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -39,12 +39,18 @@
(defconst rust-re-vis "pub")
(defconst rust-re-unsafe "unsafe")
(defconst rust-re-extern "extern")
+(defconst rust-re-union
+ (rx-to-string
+ `(seq
+ (or space line-start)
+ (group symbol-start "union" symbol-end)
+ (+ space) (regexp ,rust-re-ident))))
;;; Start of a Rust item
(defvar rust-top-item-beg-re
(concat "\\s-*\\(?:priv\\|pub\\)?\\s-*"
(regexp-opt
- '("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
+ '("enum" "struct" "union" "type" "mod" "use" "fn" "static" "impl"
"extern" "trait"))
"\\_>"))
@@ -577,9 +583,12 @@ the desired identifiers), but does not match type annotations \"foo::<\"."
(append
`(
;; Keywords proper
- ("\\_<\\(default\\)[[:space:]]+fn\\_>" 1 font-lock-keyword-face)
(,(regexp-opt rust-mode-keywords 'symbols) . font-lock-keyword-face)
+ ;; Contextual keywords
+ ("\\_<\\(default\\)[[:space:]]+fn\\_>" 1 font-lock-keyword-face)
+ (,rust-re-union 1 font-lock-keyword-face)
+
;; Special types
(,(regexp-opt rust-special-types 'symbols) . font-lock-type-face)
@@ -613,12 +622,13 @@ the desired identifiers), but does not match type annotations \"foo::<\"."
("\\?" . 'rust-question-mark-face)
)
- ;; Item definitions
+ ;; Ensure we highlight `Foo` in `struct Foo` as a type.
(mapcar #'(lambda (x)
(list (rust-re-item-def (car x))
1 (cdr x)))
'(("enum" . font-lock-type-face)
("struct" . font-lock-type-face)
+ ("union" . font-lock-type-face)
("type" . font-lock-type-face)
("mod" . font-lock-constant-face)
("use" . font-lock-constant-face)
@@ -671,7 +681,7 @@ the desired identifiers), but does not match type annotations \"foo::<\"."
(rust-rewind-irrelevant)
(rust-rewind-type-param-list)
(cond
- ((rust-looking-back-symbols '("fn" "trait" "enum" "struct" "impl" "type")) ident-pos)
+ ((rust-looking-back-symbols '("fn" "trait" "enum" "struct" "union" "impl" "type")) ident-pos)
((equal 5 (rust-syntax-class-before-point))
(backward-sexp)
@@ -758,7 +768,7 @@ the desired identifiers), but does not match type annotations \"foo::<\"."
(not (and (rust-rewind-to-decl-name)
(progn
(rust-rewind-irrelevant)
- (rust-looking-back-symbols '("enum" "struct" "trait" "type"))))))
+ (rust-looking-back-symbols '("enum" "struct" "union" "trait" "type"))))))
))
((equal token 'ambiguous-operator)
@@ -1167,7 +1177,7 @@ raw string, or to `end', whichever comes first."
(defvar rust-imenu-generic-expression
(append (mapcar #'(lambda (x)
(list (capitalize x) (rust-re-item-def-imenu x) 1))
- '("enum" "struct" "type" "mod" "fn" "trait" "impl"))
+ '("enum" "struct" "union" "type" "mod" "fn" "trait" "impl"))
`(("Macro" ,(rust-re-item-def-imenu "macro_rules!") 1)))
"Value for `imenu-generic-expression' in Rust mode.
@@ -1242,7 +1252,7 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
(error "Rustfmt failed, see *rustfmt* buffer for details"))))
(delete-file tmpf))))
-(defconst rust--format-word "\\b\\(else\\|enum\\|fn\\|for\\|if\\|let\\|loop\\|match\\|struct\\|unsafe\\|while\\)\\b")
+(defconst rust--format-word "\\b\\(else\\|enum\\|fn\\|for\\|if\\|let\\|loop\\|match\\|struct\\|union\\|unsafe\\|while\\)\\b")
(defconst rust--format-line "\\([\n]\\)")
;; Counts number of matches of regex beginning up to max-beginning,