-
-
Notifications
You must be signed in to change notification settings - Fork 247
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
Conversation
86f7447
to
74550a9
Compare
3a32f57
to
b53c714
Compare
There was a problem hiding this 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.
test/clojure-mode-util-test.el
Outdated
(with-clojure-buffer "(ns my-app.core | ||
(:require [rum.core :as rum] ;comment | ||
[my-app.views [user-page :as user-page]] | ||
;;[comment2]\n))" |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the \n
s to linebreaks.
test/clojure-mode-util-test.el
Outdated
(with-clojure-buffer "(ns my-app.core | ||
(:require [rum.core :as rum] ;comment | ||
[my-app.views [user-page :as user-page]] | ||
;;[comment2]\n))" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@vemv Thanks for the taking the time to review. 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. |
There was a problem hiding this 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.
";; 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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.
New clojure-mode version will be out soon. |
clojure-sort-ns
when sorting a ns with comments #645Edit: This is currently breaking sorting without any comments/space in the end! Will check.
Edit2: Tests are passing now.
Issue/Fix Description:
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
clojure-mode/clojure-mode.el
Line 2063 in 3a32f57