Skip to content

Commit 0cf2bc3

Browse files
committed
Merge pull request #157 from fmdkdd/master
Improve imenu support
2 parents 4fce178 + c69adaf commit 0cf2bc3

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

rust-mode.el

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
(defconst rust-re-ident "[[:word:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*")
3232
(defconst rust-re-lc-ident "[[:lower:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*")
3333
(defconst rust-re-uc-ident "[[:upper:]][[:word:][:multibyte:]_[:digit:]]*")
34+
(defconst rust-re-vis "pub")
3435

3536
(defconst rust-re-non-standard-string
3637
(rx
@@ -543,8 +544,13 @@ buffer."
543544
(defconst rust-re-pre-expression-operators "[-=!%&*/:<>[{(|.^;}]")
544545
(defun rust-re-word (inner) (concat "\\<" inner "\\>"))
545546
(defun rust-re-grab (inner) (concat "\\(" inner "\\)"))
547+
(defun rust-re-shy (inner) (concat "\\(?:" inner "\\)"))
546548
(defun rust-re-item-def (itype)
547549
(concat (rust-re-word itype) "[[:space:]]+" (rust-re-grab rust-re-ident)))
550+
(defun rust-re-item-def-imenu (itype)
551+
(concat "^[[:space:]]*"
552+
(rust-re-shy (concat (rust-re-word rust-re-vis) "[[:space:]]+")) "?"
553+
(rust-re-item-def itype)))
548554

549555
(defconst rust-re-special-types (regexp-opt rust-special-types 'symbols))
550556

@@ -1189,16 +1195,15 @@ the desired identifiers), but does not match type annotations \"foo::<\"."
11891195
;;; Imenu support
11901196
(defvar rust-imenu-generic-expression
11911197
(append (mapcar #'(lambda (x)
1192-
(list nil (rust-re-item-def x) 1))
1193-
'("enum" "struct" "type" "mod" "fn" "trait"))
1194-
`(("Impl" ,(rust-re-item-def "impl") 1)))
1198+
(list (capitalize x) (rust-re-item-def-imenu x) 1))
1199+
'("enum" "struct" "type" "mod" "fn" "trait" "impl"))
1200+
`(("Macro" ,(rust-re-item-def-imenu "macro_rules!") 1)))
11951201
"Value for `imenu-generic-expression' in Rust mode.
11961202
1197-
Create a flat index of the item definitions in a Rust file.
1203+
Create a hierarchical index of the item definitions in a Rust file.
11981204
1199-
Imenu will show all the enums, structs, etc. at the same level.
1200-
Implementations will be shown under the `Impl` subheading. Use
1201-
idomenu (imenu with `ido-mode') for best mileage.")
1205+
Imenu will show all the enums, structs, etc. in their own subheading.
1206+
Use idomenu (imenu with `ido-mode') for best mileage.")
12021207

12031208
;;; Defun Motions
12041209

@@ -1334,6 +1339,7 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
13341339
(setq-local comment-multi-line t)
13351340
(setq-local comment-line-break-function 'rust-comment-indent-new-line)
13361341
(setq-local imenu-generic-expression rust-imenu-generic-expression)
1342+
(setq-local imenu-syntax-alist '((?! . "w"))) ; For macro_rules!
13371343
(setq-local beginning-of-defun-function 'rust-beginning-of-defun)
13381344
(setq-local end-of-defun-function 'rust-end-of-defun)
13391345
(setq-local parse-sexp-lookup-properties t)

0 commit comments

Comments
 (0)