cljfmt icon indicating copy to clipboard operation
cljfmt copied to clipboard

comments inside map literals aren't handled consistently

Open gonewest818 opened this issue 6 years ago • 10 comments

Here's an example. If you have code like this:

(def h{
;; comment
:this :is
    ;; comment
:a :multi
  ; comment
:line :hash-map
            ;;; comment
})

then cljfmt fix produces this:

(def h {;; comment
        :this :is
    ;; comment
        :a :multi
  ; comment
        :line :hash-map
            ;;; comment
})

where the hash-map entries are all aligned properly to the opening {. The interior comments seem to be ignored, however. And incidentally, the alignment of the closing }) seems identical to #111, but I'm not sure if what's happening with the comments is also related.

gonewest818 avatar Jan 23 '18 23:01 gonewest818

If I'm understanding things correctrly, the first comment moves due to :remove-surrounding-whitespace? being true. The rest of the comments remain untouched. I'm wondering what the expected behavior is here.

I agree that the closing }) is a dupe of #111 and probably also #82.

lread avatar Nov 22 '18 20:11 lread

If we look at conventions, we use two semicolons for code fragments. So the double semicolon comment lines should align with the fragment.

Triple semicolons are are for top level items and really should not be in code, so they should remain untouched.

The single semicolon is for margin comments, for now I propose we leave them untouched.

So I think that means the original example given:

(def h{
;; comment
:this :is
    ;; comment
:a :multi
  ; comment
:line :hash-map
            ;;; comment
})

should format like so:

(def h {;; comment
        :this :is
        ;; comment
        :a :multi
  ; comment
        :line :hash-map
            ;;; comment
        })

lread avatar Nov 23 '18 20:11 lread

@weavejester, when you have a moment, what do you think?

lread avatar Dec 08 '18 18:12 lread

I'm not convinced we should be indenting comments. However, I'd be open to adding this as an option, disabled by default.

weavejester avatar Dec 08 '18 18:12 weavejester

Thanks @weavejester, I think I'll wait to see if there is interest from others before working on this then. Given this guidance I thought the double semicolon might be a good candidate for indenting by default, but maybe its usage as a code fragment comment is not as idiomatic as we'd like?

By the way, I'm using The Clojure Style Guide as my reference, is that typically the reference cljfmt uses?

lread avatar Dec 09 '18 15:12 lread

I'd like an option to format comments, and this is the behaviour I would like:

;;;; Remove any indentation.
;;; Remove any indentation.
;; Remove any indentation.
; Remove any indentation.

(defn foo []
  ;;;; Align with the following code.
  ;;; Align with the following code.
  ;; Align with the following code.
  ; Align with the following code.
  (let [a 1]
    (f a) ; TODO Leave as is.
          ;      Align like so.
          ;      Align like so.
    ;; Align with the following code.
    a))

I wrote up my thoughts at http://blogish.nomistech.com/how-to-comment-in-clojure/ [EDIT: I've updated the examples at that link to include something along the lines of the example that this issue kicked off with.]

I know this issue is about comments inside map literals, but I don't think that makes any difference.

simon-katz avatar Dec 09 '18 18:12 simon-katz

@simon-katz that seems like a well thought out and reasonable approach to me.

lread avatar Dec 15 '18 00:12 lread

While I don't dislike @simon-katz's proposal (it's well-aligned with the Clojure Style Guide), personally I've observed that relatively few people actually use a hierarchy of comments.

More typically either ; or ;; is chosen (depending on the author's editor), and they are used invariably. Also things like ns docstrings play the role that ;;; supposedly does.

Comments of such users (including myself) are rarely margin-aligned, but rather, aligned with the code's current indentation.

Accordingly, a simpler implementation which probably addresses the most frequent problems (inconsistent choice of ;/;;, random alignments of comments, IDE discrepancies) would be:

  • Choose a single style of comment, e.g. ;;
  • Have one and exactly one correct place for the comment, which depends on the current code's level of indentation
  • For comments that are placed to the right of some piece of code, there's no alignment whatsoever, just a fixed single space

e.g.

;; Comment

(defn foo []
  ;; Comment
  (let [a 1]
    (f a) ;; Comment
    ;; Comment
    a))

My idea of course is not exclusive with Simon's - it's easy to see how different options could be plugged into cljfmt.

Might give a shot to this - I've played quite a lot with zprint lately.

vemv avatar Mar 07 '19 07:03 vemv

is there any configuration for comment?

vinurs avatar Apr 09 '21 12:04 vinurs

is there any configuration for comment?

Not currently.

weavejester avatar Apr 09 '21 12:04 weavejester