zprint icon indicating copy to clipboard operation
zprint copied to clipboard

Trailing parens question

Open atdixon opened this issue 4 years ago • 7 comments

Is there a way in zprint to configure whether a trailing parens:

  • aligns with the open or indent according to arg/body indent?
(foo 
  )

instead of

(foo
)
  • is collapsed back to the form (so that it is no longer trailing)
(foo)

Thanks!

atdixon avatar Feb 11 '21 04:02 atdixon

Good questions!

There is no way to specify where a trailing paren goes (at present). In general, a trailing paren is collapsed back to the form in all cases. There are two broad reasons why it would not be:

  • if there was a comment before the trailing paren
  • if there was a newline or a blank line before the trailing paren, and "respect newlines" or "respect blank lines" was the current style, or you were using indent-only (which never changes what line something is on)

In the event that there is a trailing paren because of one of the above reasons, it now goes directly below the matching open paren. This is discussed in a bit more detail in Issue #143, where there is an example of why I chose that approach. That approach was new in 1.0.0, as prior to that, trailing parens usually (but not always) went to the indent for the collection. It was actually a lot of work to standardize on putting it directly under the matching open paren.

I have two questions for you:

  1. Do you routinely have trailing parens? If so, why?
  2. Where would you like them to show up? If you can articulate a reason for why you would like that, it would help to motivate any change I might make to allow this to be configuable.

Thanks!

kkinnear avatar Feb 11 '21 16:02 kkinnear

@kkinnear Thanks for the quick and detailed reply!

I'll admit we have some atypical requirements as we make heavy use of Clojure DSL/macros, where we slightly depart in some cases from the Clojure style guide. Using trailing parens is something we occasionally prefer to do and, when we do, we prefer the trailing to indent with the body. So we would use such a config if zprint had it, but understand that it's a bit off the common path.

Incidentally we would probably use a zprint feature that allowed us to both "respect newlines" but collapse trailing parens (ie, don't respect newlines for just trailing parens). That might be a more generally interesting option.

Thanks for the answer & context; just wanted to make sure we weren't missing a config option.

atdixon avatar Feb 11 '21 19:02 atdixon

@kkinnear sorry for necrobumping but maybe it is possible to control trailing parens now with :indent-only?

macovsky avatar Feb 21 '24 15:02 macovsky

Well, :indent-only isn't going to do it for you, unfortunately. It bypasses a lot of the actual formatting, and it is almost separate internally. The results of :indent-only are pretty unlovely -- at least to my eyes.

Are you using :indent-only or :respect-nl now?

That said, I'm open to exploring this for the next release (after 1.2.9, which is coming "real soon now").

I think you would prefer to have the newlines indented instead of lined up. I'll investigate why I made that decision, since I'm a bit hazy on that at this juncture. But I remember that I had "good reasons".

I'll also investigate if there is a good way to have :respect-nl not respect the newline if there are only parens on the next line. Which would probably be easier. If that is the best I can do, would that still be interesting for you?

Thanks for asking again. I think this was asked again in Issue #270, specifically about :respect-nl, so clearly you aren't the only folks wanting this.

kkinnear avatar Feb 21 '24 21:02 kkinnear

Well, :indent-only isn't going to do it for you, unfortunately. It bypasses a lot of the actual formatting, and it is almost separate internally. The results of :indent-only are pretty unlovely -- at least to my eyes.

Are you using :indent-only or :respect-nl now?

I'm not the OP just in case but we're using :indent-only now and it's quite fine because of its slight extent of invasion. so I thought maybe it could be possible to add some formatting hints to it for trailing parens.

macovsky avatar Feb 22 '24 08:02 macovsky

Well, as it happens, I believe that I have found a way to make the parens that are on a line by themselves move up to the end of the previous (non-comment!) line for :indent-only as well as :respect-nl and :respect-bl. I think I can also get the indentation to be configurable if you prefer that. It won't be in 1.2.9, because I have to release that soon as people are waiting. But I have prototyped the code for this and I think it will work. Still need to write tests.

kkinnear avatar Feb 24 '24 19:02 kkinnear

Well, as it happens, I believe that I have found a way to make the parens that are on a line by themselves move up to the end of the previous (non-comment!) line for :indent-only as well as :respect-nl and :respect-bl. I think I can also get the indentation to be configurable if you prefer that. It won't be in 1.2.9, because I have to release that soon as people are waiting. But I have prototyped the code for this and I think it will work. Still need to write tests.

that sounds great! thanks so much for your effort.

macovsky avatar Feb 24 '24 19:02 macovsky