Skip to content

Commit 1d42d69

Browse files
riscymookid
authored andcommitted
Speed up line indentation inside strings (#313)
1 parent 7e2533f commit 1d42d69

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

rust-mode.el

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,8 @@ buffer."
365365
((nth 3 (syntax-ppss))
366366
(let*
367367
((string-begin-pos (nth 8 (syntax-ppss)))
368-
(end-of-prev-line-pos (when (> (line-number-at-pos) 1)
369-
(save-excursion
370-
(forward-line -1)
371-
(end-of-line)
372-
(point)))))
368+
(end-of-prev-line-pos (unless (rust--same-line-p (point) (point-min))
369+
(line-end-position 0))))
373370
(when
374371
(and
375372
;; If the string begins with an "r" it's a raw string and
@@ -389,7 +386,7 @@ buffer."
389386

390387
;; Indent to the same level as the previous line, or the
391388
;; start of the string if the previous line starts the string
392-
(if (= (line-number-at-pos end-of-prev-line-pos) (line-number-at-pos string-begin-pos))
389+
(if (rust--same-line-p end-of-prev-line-pos string-begin-pos)
393390
;; The previous line is the start of the string.
394391
;; If the backslash is the only character after the
395392
;; string beginning, indent to the next indent
@@ -530,6 +527,11 @@ buffer."
530527
(indent-line-to indent)
531528
(save-excursion (indent-line-to indent))))))
532529

530+
(defun rust--same-line-p (pos1 pos2)
531+
"Return non-nil if POS1 and POS2 are on the same line."
532+
(save-excursion (= (progn (goto-char pos1) (line-end-position))
533+
(progn (goto-char pos2) (line-end-position)))))
534+
533535

534536
;; Font-locking definitions and helpers
535537
(defconst rust-mode-keywords

0 commit comments

Comments
 (0)