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 2 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
8 changes: 7 additions & 1 deletion clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,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
12 changes: 12 additions & 0 deletions test/clojure-mode-util-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@
(:require [my-app.views [user-page :as user-page]]
[rum.core :as rum] ;comment\n))")))

(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]\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.

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.

(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\n
;;[comment2]\n))")))

(it "should also sort imports in a ns"
(with-clojure-buffer "\n(ns my-app.core
(:require [my-app.views [front-page :as front-page]]
Expand Down