Commit 5e88b50d authored by João Távora's avatar João Távora
Browse files

Correctly reindent previous line in electric-indent-mode

Fixes: bug#35254

Do this even when electric-indent-inhibit is t, except when the
newline insertion is being performed by electric-layout-mode.

* lisp/electric.el (electric-indent-post-self-insert-function):
Reindent previous line unless operating under
electric-layout-mode.
(electric-layout-post-self-insert-function-1): Bind
electric-indent-inhibit to 'electric-layout-mode.

* test/lisp/electric-tests.el
(electric-layout-control-reindentation): New test.
parent 2a2a1bdb
Pipeline #2225 failed with stage
in 63 minutes and 5 seconds
...@@ -270,10 +270,13 @@ or comment." ...@@ -270,10 +270,13 @@ or comment."
(goto-char before) (goto-char before)
(condition-case-unless-debug () (condition-case-unless-debug ()
(indent-according-to-mode) (indent-according-to-mode)
(error (throw 'indent-error nil))) (error (throw 'indent-error nil))))
;; The goal here will be to remove the trailing (unless (eq electric-indent-inhibit 'electric-layout-mode)
;; whitespace after reindentation of the previous line ;; Unless we're operating under
;; because that may have (re)introduced it. ;; `electric-layout-mode' (Bug#35254), the goal here
;; will be to remove the trailing whitespace after
;; reindentation of the previous line because that
;; may have (re)introduced it.
(goto-char before) (goto-char before)
;; We were at EOL in marker `before' before the call ;; We were at EOL in marker `before' before the call
;; to `indent-according-to-mode' but after we may ;; to `indent-according-to-mode' but after we may
...@@ -451,7 +454,7 @@ If multiple rules match, only first one is executed.") ...@@ -451,7 +454,7 @@ If multiple rules match, only first one is executed.")
;; really wants to reindent, then ;; really wants to reindent, then
;; `last-command-event' should be in ;; `last-command-event' should be in
;; `electric-indent-chars'. ;; `electric-indent-chars'.
(let ((electric-indent-inhibit t)) (let ((electric-indent-inhibit 'electric-layout-mode))
(funcall nl-after))))))) (funcall nl-after)))))))
(pcase sym (pcase sym
('before (funcall nl-before)) ('before (funcall nl-before))
......
...@@ -876,6 +876,25 @@ baz\"\"" ...@@ -876,6 +876,25 @@ baz\"\""
(call-interactively (key-binding `[,last-command-event]))) (call-interactively (key-binding `[,last-command-event])))
(should (equal (buffer-string) "int main () {\n \n}")))) (should (equal (buffer-string) "int main () {\n \n}"))))
(ert-deftest electric-layout-control-reindentation ()
"Same as `e-l-int-main-kernel-style', but checking Bug#35254."
(ert-with-test-buffer ()
(plainer-c-mode)
(electric-layout-local-mode 1)
(electric-pair-local-mode 1)
(electric-indent-local-mode 1)
(setq-local electric-layout-rules
'((?\{ . (after))
(?\} . (before))))
(insert "int main () ")
(let ((last-command-event ?\{))
(call-interactively (key-binding `[,last-command-event])))
(should (equal (buffer-string) "int main () {\n \n}"))
;; insert an additional newline and check indentation and
;; reindentation
(call-interactively 'newline)
(should (equal (buffer-string) "int main () {\n\n \n}"))))
(define-derived-mode plainer-c-mode c-mode "pC" (define-derived-mode plainer-c-mode c-mode "pC"
"A plainer/saner C-mode with no internal electric machinery." "A plainer/saner C-mode with no internal electric machinery."
(c-toggle-electric-state -1) (c-toggle-electric-state -1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment