Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clojure-sort-ns with comments in the end #646

Merged
merged 6 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bugs fixed

* [#645](https://github.com/clojure-emacs/clojure-mode/issues/645): Fix infinite loop when sorting a ns with comments in the end.
* [#586](https://github.com/clojure-emacs/clojure-mode/issues/586): Fix infinite loop when opening file containing `comment` with `clojure-toplevel-inside-comment-form` set to `t`.

## 5.16.0 (2022-12-14)
Expand Down
8 changes: 7 additions & 1 deletion clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,13 @@ content) are considered part of the preceding sexp."
(save-restriction
(narrow-to-region (point) (save-excursion
(up-list)
(1- (point))))
;; Ignore any comments in the end before sorting
(backward-char)
(forward-sexp -1)
(clojure-forward-logical-sexp)
(unless (looking-at-p ")")
(search-forward-regexp "$"))
(point)))
(skip-chars-forward "\r\n[:blank:]")
(sort-subr nil
(lambda () (skip-chars-forward "\r\n[:blank:]"))
Expand Down
44 changes: 43 additions & 1 deletion test/clojure-mode-util-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,49 @@
(expect (buffer-string) :to-equal
"(ns my-app.core
(:require [my-app.views [user-page :as user-page]]
[rum.core :as rum] ;comment\n))")))
[rum.core :as rum] ;comment
))")))

(it "should sort requires in a basic ns with comments in the end"
(with-clojure-buffer "(ns my-app.core
(:require [rum.core :as rum] ;comment
[my-app.views [user-page :as user-page]]
;;[comment2]
))"
(clojure-sort-ns)
(expect (buffer-string) :to-equal
"(ns my-app.core
(:require [my-app.views [user-page :as user-page]]
[rum.core :as rum] ;comment

;;[comment2]
))")))
(it "should sort requires in ns with copyright disclamer and comments"
(with-clojure-buffer ";; Copyright (c) John Doe. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
(ns clojure.core
(:require
;; The first comment
[foo] ;; foo comment
;; Middle comment
[bar] ;; bar comment
;; A last comment
))"
(clojure-sort-ns)
(expect (buffer-string) :to-equal
";; Copyright (c) John Doe. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
(ns clojure.core
(:require
;; Middle comment
[bar] ;; bar comment
;; The first comment
[foo] ;; foo comment

;; A last comment
Comment on lines +170 to +180
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resulting formatting for this (fairly pathological) case aren't spectacular, although I'm willing to merge this in as-is.

Note that clojure--sort-following-sexps is a private function with a single usage in clojure-mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't understood you completely.
Is it about additional linebreak or the sorting order itself?
The linebreak issue is not exactly related to this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In either case it will be better to handle it in a separate PR.
Will be happy to discuss it with you further on slack.

I have merged the master and added a Changelog entry.Please feel free to squash the commits as you see it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of Middle comment and The first comment get swapped

If you'd be wiliing to work on it please create an issue, else we can let it slip.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. But that is the expected behaviour currently.
Comments at the start of a line are considered part of the following sexp.
The sorting happens only based on what is inside the import. The comments are moved along with the sexp.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but that's a private function, we could change it as desired!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

IMHO the comments should be moved along with the imports instead of being sorted separately. So I feel the current behaviour makes some sense. Whether the comments should be placed below or above the sexp is up for debate.

Also if we make any changes to the sorting order it will break the current user experience. So a discussion + new ticket is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for the review and the merge. :)

Copy link
Member

@vemv vemv Jun 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers 🍻 tbh it's no big deal, after all there are many competing formatters for ns-sorting functionality. Something that doesn't loop forever probably satisfies this project's scope.

))")))

(it "should also sort imports in a ns"
(with-clojure-buffer "\n(ns my-app.core
Expand Down