Skip to content

Commit 5231709

Browse files
committed
Speed up calls to "looking-back"
1 parent 6e72f64 commit 5231709

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

rust-mode.el

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020
"Set variable VAR to value VAL in current buffer."
2121
(list 'set (list 'make-local-variable (list 'quote var)) val))))
2222

23+
(defun rust-looking-back-str (str)
24+
"Like `looking-back' but for fixed strings rather than regexps (so that it's not so slow)"
25+
(let ((len (length str)))
26+
(and (> (point) len)
27+
(equal str (buffer-substring-no-properties (- (point) len) (point))))))
28+
29+
(defun rust-looking-back-symbols (SYMS)
30+
"Return non-nil if the point is just after a complete symbol that is a member of the list of strings SYMS"
31+
(save-excursion
32+
(let* ((pt-orig (point))
33+
(beg-of-symbol (progn (forward-thing 'symbol -1) (point)))
34+
(end-of-symbol (progn (forward-thing 'symbol 1) (point))))
35+
(and
36+
(= end-of-symbol pt-orig)
37+
(member (buffer-substring-no-properties beg-of-symbol pt-orig) SYMS)))))
38+
39+
(defun rust-looking-back-ident ()
40+
"Non-nil if we are looking backwards at a valid rust identifier"
41+
(let ((beg-of-symbol (save-excursion (forward-thing 'symbol -1) (point))))
42+
(looking-back rust-re-ident beg-of-symbol)))
43+
2344
;; Syntax definitions and helpers
2445
(defvar rust-mode-syntax-table
2546
(let ((table (make-syntax-table)))
@@ -77,7 +98,7 @@
7798
(defun rust-rewind-irrelevant ()
7899
(let ((starting (point)))
79100
(skip-chars-backward "[:space:]\n")
80-
(if (looking-back "\\*/" nil) (backward-char))
101+
(if (rust-looking-back-str "*/") (backward-char))
81102
(if (rust-in-str-or-cmnt)
82103
(rust-rewind-past-str-cmnt))
83104
(if (/= starting (point))
@@ -141,13 +162,13 @@
141162
;;
142163
((skip-dot-identifier
143164
(lambda ()
144-
(when (looking-back (concat "\\." rust-re-ident) nil)
165+
(when (and (rust-looking-back-ident) (save-excursion (forward-thing 'symbol -1) (= ?. (char-before))))
145166
(forward-thing 'symbol -1)
146167
(backward-char)
147168
(- (current-column) rust-indent-offset)))))
148169
(cond
149170
;; foo.bar(...)
150-
((looking-back ")" nil)
171+
((rust-looking-back-str ")")
151172
(backward-list 1)
152173
(funcall skip-dot-identifier))
153174

@@ -271,7 +292,7 @@
271292
;; ..or if the previous line ends with any of these:
272293
;; { ? : ( , ; [ }
273294
;; then we are at the beginning of an expression, so stay on the baseline...
274-
(looking-back "[(,:;?[{}]\\|[^|]|" nil)
295+
(looking-back "[(,:;?[{}]\\|[^|]|" (- (point) 2))
275296
;; or if the previous line is the end of an attribute, stay at the baseline...
276297
(progn (rust-rewind-to-beginning-of-current-level-expr) (looking-at "#")))))
277298
baseline

0 commit comments

Comments
 (0)