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

Conversation

p4v4n
Copy link
Contributor

@p4v4n p4v4n commented Mar 6, 2023

Edit: This is currently breaking sorting without any comments/space in the end! Will check.
Edit2: Tests are passing now.

Issue/Fix Description:

  • M-x clojure-sort-ns hangs as described in the issue when comments are present only in end of require.
  • This change removes any comments in the end before sorting with sort-subr.
  • The alternative fix of keeping these comments and ensuring the sort order is correct from sort-subr fn is much uglier. Also the comments in the end are not associated with any require based on the docstring for clojure--sort-following-sexps.

Edit4: Adding a test to cover this case revealed a small issue with this approach.
When there is a comment in the end and after sorting if the last import also has a comment there is an extra newline because of code at

;; Does the last line now end in a comment?
. But fixing this without adding a hack doesn't seem possible. I am silencing the test failure for now by adding another newline in the test as this issue is already present with the current code in a different scenario. Will wait for a review before further changes.

@p4v4n p4v4n force-pushed the fix-clojure-sort-ns branch from 86f7447 to 74550a9 Compare March 6, 2023 18:50
@p4v4n p4v4n force-pushed the fix-clojure-sort-ns branch from 3a32f57 to b53c714 Compare March 6, 2023 19:49
Copy link
Member

@vemv vemv left a comment

Choose a reason for hiding this comment

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

Sorry for the late review!

Some suggestions.

(with-clojure-buffer "(ns my-app.core
(:require [rum.core :as rum] ;comment
[my-app.views [user-page :as user-page]]
;;[comment2]\n))"
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit surprising to see \n in addition to line breaks. I'd prefer if we only had line breaks

(I see it's also in existing tests. Good to change also)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the \ns to linebreaks.

(with-clojure-buffer "(ns my-app.core
(:require [rum.core :as rum] ;comment
[my-app.views [user-page :as user-page]]
;;[comment2]\n))"
Copy link
Member

Choose a reason for hiding this comment

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

Please let's have an additional test case looking like this:

;; 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 (note that it starts with "T")
   [foo] ;; foo comment
   ;; Middle comment
   [bar] ;; bar comment
   ;; A last comment (note that it starts with "A")
   ))

...not that supporting such a ns is tremendouly important, but it can helps us make sure the fix will have an overall great quality.

The copyright header part should remain untouched (certainly not sorted a-z).

I don't have a fixed expectation for the other comments (i.e. do they get sorted? Placed at the bottom? Beginning?). If the current code works (i.e. doesn't loop) and emits something reasonable, we would ship that.

Copy link
Contributor Author

@p4v4n p4v4n Jun 24, 2023

Choose a reason for hiding this comment

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

Added the above test case.

The docstring for fn clojure--sort-following-sexps says
Comments at the start of a line are considered part of the following sexp. Comments at the end of a line (after some other content) are considered part of the preceding sexp.

Based on this the last comment does't belong to any import. My change here ignores this last comment before sorting the imports/requires.

@p4v4n
Copy link
Contributor Author

p4v4n commented Jun 24, 2023

Sorry for the late review!

Some suggestions.

@vemv Thanks for the taking the time to review.
My original post was not very clear. Even I had trouble understanding it now!
I have addressed all the comments.

The issue with additional linebreak still exists as seen in the tests. But fixing it doesn't seem like it's worth the effort IMO.

Copy link
Member

@vemv vemv left a comment

Choose a reason for hiding this comment

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

Kudos for getting back in so quickly!

I left some optional feedback.

Either way, please rebase master in, add a Changelog entry and we're good to go.

Comment on lines +170 to +180
";; 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
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.

@vemv vemv merged commit f5b85ca into clojure-emacs:master Jun 24, 2023
@vemv
Copy link
Member

vemv commented Jun 24, 2023

New clojure-mode version will be out soon.

@p4v4n p4v4n deleted the fix-clojure-sort-ns branch June 24, 2023 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infinite loop in clojure-sort-ns when sorting a ns with comments
2 participants