Skip to content
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

added optional method chain indentations for emacs major mode #19913

Merged
merged 2 commits into from
Jan 22, 2015
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
33 changes: 30 additions & 3 deletions src/etc/emacs/rust-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
:type 'integer
:group 'rust-mode)

(defcustom rust-indent-method-chain nil
"Indent Rust method chains, aligned by the '.' operators"
:type 'boolean
:group 'rust-mode)

(defun rust-paren-level () (nth 0 (syntax-ppss)))
(defun rust-in-str-or-cmnt () (nth 8 (syntax-ppss)))
(defun rust-rewind-past-str-cmnt () (goto-char (nth 8 (syntax-ppss))))
Expand All @@ -68,10 +73,19 @@
;; open bracket ends the line
(when (not (looking-at "[[:blank:]]*\\(?://.*\\)?$"))
(when (looking-at "[[:space:]]")
(forward-word 1)
(backward-word 1))
(forward-word 1)
(backward-word 1))
(current-column))))

(defun rust-align-to-method-chain ()
(save-excursion
(previous-line)
(end-of-line)
(backward-word 1)
(backward-char)
(when (looking-at "\\..+\(.*\)\n")
(- (current-column) rust-indent-offset))))

(defun rust-rewind-to-beginning-of-current-level-expr ()
(let ((current-level (rust-paren-level)))
(back-to-indentation)
Expand All @@ -94,10 +108,13 @@
;; the inside of it correctly relative to the outside.
(if (= 0 level)
0
(or
(when rust-indent-method-chain
(rust-align-to-method-chain))
(save-excursion
(backward-up-list)
(rust-rewind-to-beginning-of-current-level-expr)
(+ (current-column) rust-indent-offset)))))
(+ (current-column) rust-indent-offset))))))
(cond
;; A function return type is indented to the corresponding function arguments
((looking-at "->")
Expand All @@ -109,6 +126,16 @@
;; A closing brace is 1 level unindended
((looking-at "}") (- baseline rust-indent-offset))

;;Line up method chains by their .'s
((when (and rust-indent-method-chain
(looking-at "\..+\(.*\);?\n"))
(or
(let ((method-indent (rust-align-to-method-chain)))
(when method-indent
(+ method-indent rust-indent-offset)))
(+ baseline rust-indent-offset))))


;; Doc comments in /** style with leading * indent to line up the *s
((and (nth 4 (syntax-ppss)) (looking-at "*"))
(+ 1 baseline))
Expand Down