Skip to content

Commit 1ceac8b

Browse files
committed
Re-fontify buffer after it is reverted
Fix #104
1 parent 95d089c commit 1ceac8b

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

rust-mode-tests.el

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,29 @@ Fontification needs to include this whole string or none of it.
24272427
(should (<= font-lock-beg 1))
24282428
(should (>= font-lock-end 12)))))
24292429

2430+
(ert-deftest redo-syntax-after-change-far-from-point ()
2431+
(let*
2432+
((tmp-file-name (make-temp-file "rust-mdoe-test-issue104"))
2433+
(base-contents (apply 'concat (append '("fn foo() {\n\n}\n") (make-list 500 "// More stuff...\n") '("fn bar() {\n\n}\n")))))
2434+
;; Create the temp file...
2435+
(with-temp-file tmp-file-name
2436+
(insert base-contents))
2437+
(with-temp-buffer
2438+
(insert-file-contents tmp-file-name 'VISIT nil nil 'REPLACE)
2439+
(rust-mode)
2440+
(goto-char (point-max))
2441+
(should (= 0 (rust-paren-level)))
2442+
(with-temp-file tmp-file-name
2443+
(insert base-contents)
2444+
(goto-char 12) ;; On the blank line in the middle of fn foo
2445+
(insert " let z = 1 < 3;")
2446+
)
2447+
(revert-buffer 'IGNORE-AUTO 'NOCONFIRM 'PRESERVE-MODES)
2448+
(should (= 0 (rust-paren-level)))
2449+
)
2450+
)
2451+
)
2452+
24302453
;; If electric-pair-mode is available, load it and run the tests that use it. If not,
24312454
;; no error--the tests will be skipped.
24322455
(require 'elec-pair nil t)

rust-mode.el

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,9 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
12291229
(setq-local beginning-of-defun-function 'rust-beginning-of-defun)
12301230
(setq-local end-of-defun-function 'rust-end-of-defun)
12311231
(setq-local parse-sexp-lookup-properties t)
1232-
(setq-local electric-pair-inhibit-predicate 'rust-electric-pair-inhibit-predicate-wrap))
1232+
(setq-local electric-pair-inhibit-predicate 'rust-electric-pair-inhibit-predicate-wrap)
1233+
(add-hook 'after-revert-hook 'rust--after-revert-hook 'LOCAL)
1234+
)
12331235

12341236
;;;###autoload
12351237
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
@@ -1240,6 +1242,17 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
12401242
(require 'rust-mode)
12411243
(rust-mode))
12421244

1245+
;; Issue #104: When reverting the buffer, make sure all fontification is redone
1246+
;; so that we don't end up missing a non-angle-bracket '<' or '>' character.
1247+
(defun rust--after-revert-hook ()
1248+
(let
1249+
;; Newer emacs versions (25 and later) make `font-lock-fontify-buffer'
1250+
;; interactive-only, and want lisp code to call `font-lock-flush' or
1251+
;; `font-lock-ensure'. But those don't exist in emacs 24 and earlier.
1252+
((font-lock-ensure-fn (if (fboundp 'font-lock-ensure) 'font-lock-ensure 'font-lock-fontify-buffer)))
1253+
(funcall font-lock-ensure-fn))
1254+
)
1255+
12431256
;; Issue #6887: Rather than inheriting the 'gnu compilation error
12441257
;; regexp (which is broken on a few edge cases), add our own 'rust
12451258
;; compilation error regexp and use it instead.

0 commit comments

Comments
 (0)