Skip to content

rust-mode cleanup. #13432

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 1 commit into from
Apr 16, 2014
Merged
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
109 changes: 58 additions & 51 deletions src/etc/emacs/rust-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
;; Version: 0.2.0
;; Author: Mozilla
;; Url: https://github.com/mozilla/rust
;; Keywords: languages

;;; Commentary:
;;

;;; Code:

(eval-when-compile (require 'cl))
(eval-when-compile (require 'misc))

;; for GNU Emacs < 24.3
(eval-when-compile
(unless (fboundp 'setq-local)
(defmacro setq-local (var val)
"Set variable VAR to value VAL in current buffer."
(list 'set (list 'make-local-variable (list 'quote var)) val))))

;; Syntax definitions and helpers
(defvar rust-mode-syntax-table
(let ((table (make-syntax-table)))

;; Operators
(loop for i in '(?+ ?- ?* ?/ ?& ?| ?^ ?! ?< ?> ?~ ?@)
do (modify-syntax-entry i "." table))
(dolist (i '(?+ ?- ?* ?/ ?& ?| ?^ ?! ?< ?> ?~ ?@))
(modify-syntax-entry i "." table))

;; Strings
(modify-syntax-entry ?\" "\"" table)
Expand All @@ -30,10 +42,14 @@

table))

(defgroup rust-mode nil "Support for Rust code.")
(defgroup rust-mode nil
"Support for Rust code."
:link '(url-link "http://www.rust-lang.org/")
:group 'languages)

(defcustom rust-indent-offset 4
"*Indent Rust code by this number of spaces."
"Indent Rust code by this number of spaces."
:type 'integer
:group 'rust-mode)

(defun rust-paren-level () (nth 0 (syntax-ppss)))
Expand Down Expand Up @@ -226,17 +242,16 @@
)

;; Item definitions
(loop for (item . face) in

'(("enum" . font-lock-type-face)
("struct" . font-lock-type-face)
("type" . font-lock-type-face)
("mod" . font-lock-type-face)
("use" . font-lock-type-face)
("fn" . font-lock-function-name-face)
("static" . font-lock-constant-face))

collect `(,(rust-re-item-def item) 1 ,face))))
(mapcar #'(lambda (x)
(list (rust-re-item-def (car x))
1 (cdr x)))
'(("enum" . font-lock-type-face)
("struct" . font-lock-type-face)
("type" . font-lock-type-face)
("mod" . font-lock-type-face)
("use" . font-lock-type-face)
("fn" . font-lock-function-name-face)
("static" . font-lock-constant-face)))))

(defun rust-fill-prefix-for-comment-start (line-start)
"Determine what to use for `fill-prefix' based on what is at the beginning of a line."
Expand Down Expand Up @@ -350,17 +365,17 @@

;;; Imenu support
(defvar rust-imenu-generic-expression
(append (loop for item in
'("enum" "struct" "type" "mod" "fn" "trait")
collect `(nil ,(rust-re-item-def item) 1))
(append (mapcar #'(lambda (x)
(list nil (rust-re-item-def x) 1))
'("enum" "struct" "type" "mod" "fn" "trait"))
`(("Impl" ,(rust-re-item-def "impl") 1)))
"Value for `imenu-generic-expression' in Rust mode.

Create a flat index of the item definitions in a Rust file.

Imenu will show all the enums, structs, etc. at the same level.
Implementations will be shown under the `Impl` subheading.
Use idomenu (imenu with ido-mode) for best mileage.")
Implementations will be shown under the `Impl` subheading. Use
idomenu (imenu with `ido-mode') for best mileage.")

;;; Defun Motions

Expand All @@ -369,8 +384,7 @@ Use idomenu (imenu with ido-mode) for best mileage.")
(concat "^\\s-*\\(?:priv\\|pub\\)?\\s-*"
(regexp-opt
'("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
"extern" "impl" "static" "trait"
))))
"extern" "impl" "static" "trait"))))

(defun rust-beginning-of-defun (&optional arg)
"Move backward to the beginning of the current defun.
Expand Down Expand Up @@ -411,52 +425,43 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
(define-derived-mode rust-mode rust-parent-mode "Rust"
"Major mode for Rust code."
:group 'rust-mode

;; Basic syntax
(set-syntax-table rust-mode-syntax-table)
:syntax-table rust-mode-syntax-table

;; Indentation
(set (make-local-variable 'indent-line-function)
'rust-mode-indent-line)
(setq-local indent-line-function 'rust-mode-indent-line)

;; Fonts
(set (make-local-variable 'font-lock-defaults)
'(rust-mode-font-lock-keywords nil nil nil nil))
(setq-local font-lock-defaults '(rust-mode-font-lock-keywords nil nil nil nil))

;; Misc
(set (make-local-variable 'comment-start) "// ")
(set (make-local-variable 'comment-end) "")
(set (make-local-variable 'indent-tabs-mode) nil)
(setq-local comment-start "// ")
(setq-local comment-end "")
(setq-local indent-tabs-mode nil)

;; Allow paragraph fills for comments
(set (make-local-variable 'comment-start-skip)
"\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*")
(set (make-local-variable 'paragraph-start)
(setq-local comment-start-skip "\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*")
(setq-local paragraph-start
(concat "[[:space:]]*\\(?:" comment-start-skip "\\|\\*/?[[:space:]]*\\|\\)$"))
(set (make-local-variable 'paragraph-separate) paragraph-start)
(set (make-local-variable 'normal-auto-fill-function) 'rust-do-auto-fill)
(set (make-local-variable 'fill-paragraph-function) 'rust-fill-paragraph)
(set (make-local-variable 'fill-forward-paragraph-function) 'rust-fill-forward-paragraph)
(set (make-local-variable 'adaptive-fill-function) 'rust-find-fill-prefix)
(set (make-local-variable 'comment-multi-line) t)
(set (make-local-variable 'comment-line-break-function) 'rust-comment-indent-new-line)
(set (make-local-variable 'imenu-generic-expression) rust-imenu-generic-expression)
(set (make-local-variable 'beginning-of-defun-function) 'rust-beginning-of-defun)
(set (make-local-variable 'end-of-defun-function) 'rust-end-of-defun)
)

(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 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 beginning-of-defun-function 'rust-beginning-of-defun)
(setq-local end-of-defun-function 'rust-end-of-defun))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rs$" . rust-mode))
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))

(defun rust-mode-reload ()
(interactive)
(unload-feature 'rust-mode)
(require 'rust-mode)
(rust-mode))

(provide 'rust-mode)

;; Issue #6887: Rather than inheriting the 'gnu compilation error
;; regexp (which is broken on a few edge cases), add our own 'rust
;; compilation error regexp and use it instead.
Expand All @@ -480,4 +485,6 @@ See `compilation-error-regexp-alist for help on their format.")
(cons 'rustc rustc-compilation-regexps))
(add-to-list 'compilation-error-regexp-alist 'rustc)))

(provide 'rust-mode)

;;; rust-mode.el ends here