From e8cef564ac5c7e6c416148a33bb0791e23a34aa6 Mon Sep 17 00:00:00 2001 From: Bozhidar Batsov Date: Fri, 20 Feb 2026 18:09:14 +0200 Subject: [PATCH] [Fix #619] Fix clojure-thread-last-all breaking forms with line comments The dangling-parens join in clojure--thread-last would merge closing parens onto a line ending in a comment, absorbing them into the comment and making the form invalid. Now checks for a preceding comment before joining. --- CHANGELOG.md | 1 + clojure-mode.el | 8 +++++++- test/clojure-mode-refactor-threading-test.el | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24cb6fcf..ace9beb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ ### Bugs fixed +* [#619](https://github.com/clojure-emacs/clojure-mode/issues/619): Fix `clojure-thread-last-all` breaking forms containing line comments by absorbing closing parens into comments. * Fix typos in `clojure-mode-extra-font-locking`: `halt-when?` -> `halt-when`, `simple-indent?` -> `simple-ident?`. * Fix `doc` and `find-doc` misplaced under `clojure.core` instead of `clojure.repl` in extra font-locking. diff --git a/clojure-mode.el b/clojure-mode.el index 27347286..050f36f9 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -2637,7 +2637,13 @@ With universal argument \\[universal-argument], fully unwind thread." (clojure--remove-superfluous-parens) ;; cljr #255 Fix dangling parens (forward-sexp) - (when (looking-back "^\\s-*\\()+\\)\\s-*" (line-beginning-position)) + (when (and (looking-back "^\\s-*\\()+\\)\\s-*" (line-beginning-position)) + ;; Don't join if previous line ends in a comment, + ;; as that would absorb the parens into the comment. + (not (save-excursion + (forward-line -1) + (end-of-line) + (nth 4 (syntax-ppss))))) (let ((pos (match-beginning 1))) (put-text-property pos (1+ pos) 'clojure-thread-line-joined t)) (join-line)) diff --git a/test/clojure-mode-refactor-threading-test.el b/test/clojure-mode-refactor-threading-test.el index 302b0a2f..f593db05 100644 --- a/test/clojure-mode-refactor-threading-test.el +++ b/test/clojure-mode-refactor-threading-test.el @@ -379,6 +379,18 @@ (comp (serve)) (deftask dev []))" + (beginning-of-buffer) + (clojure-thread-last-all nil)) + + (when-refactoring-it "should preserve line comments" + "(foo x ;; grobble + (bar y))" + + "(->> y + bar + (foo x ;; grobble + ))" + (beginning-of-buffer) (clojure-thread-last-all nil)))