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

Unexpected indentation with let binding? #151

Open
borkdude opened this issue Nov 7, 2024 · 6 comments
Open

Unexpected indentation with let binding? #151

borkdude opened this issue Nov 7, 2024 · 6 comments

Comments

@borkdude
Copy link

borkdude commented Nov 7, 2024

What should happen in the following case after you format it?

(let [x 1
        y 2]
  3)

See squint playground

@PEZ
Copy link

PEZ commented Nov 7, 2024

I think it's using rule 3 for the vector. And I think that's a bug, @oakmac? 😄

@oakmac
Copy link
Owner

oakmac commented Nov 9, 2024

Thank you for the report. While the code here looks "off", I am not sure this is a bug with the formatter.

I think it's using rule 3 for the vector.

Yes - this is exactly what is happening in this case. y is vertically aligned with 1 on the line above, so Standard Clojure Style assumes that is an intentional choice from the developer.

Some more examples to clarify:

InputAfter formatting
(let [x 1
       y 2
              z 4]
  :foo)
(let [x 1
      y 2
      z 4]
  :foo)
InputAfter formatting
(let [x 1
        y 2
              z 4]
  :foo)
(let [x 1
        y 2
        z 4]
  :foo)

Maybe Rule 3 should not apply to Vectors? I need to give this a think.

@oakmac
Copy link
Owner

oakmac commented Nov 9, 2024

Curious if @shaunlebron has any thoughts about this?

@shaunlebron
Copy link

Eh, hiccups use vectors too, so I wouldn't disable rule three for them.

Chris if you have a rule to columnate let-bindings (I think I saw this in your talk), I suppose there would be enough semantic detection in place to add an exception for this as well.

@oakmac
Copy link
Owner

oakmac commented Nov 9, 2024

hiccups use vectors too, so I wouldn't disable rule three for them

This is a great point - thank you!

if you have a rule to columnate let-bindings (I think I saw this in your talk), I suppose there would be enough semantic detection in place to add an exception for this as well.

Standard Clojure Style does not vertically align let bindings by default. If you pass in vertically-aligned let bindings, it does not change them, but it does not do that formatting for you (relevant test case for this behavior).

There is enough information from the parser in order to determine "Does this look like a let binding?", but I am not sure it is a great idea to add an exception for this. I need to give this a think.

I also wonder if this a case of "is this a realistic scenario? or a theoretical concern?" Standard Clojure Style is not a pretty-printer, so if you pass in strange-looking code you may get some strange looking code as the output.

@oakmac
Copy link
Owner

oakmac commented Nov 9, 2024

I also wonder if this a case of "is this a realistic scenario? or a theoretical concern?" Standard Clojure Style is not a pretty-printer, so if you pass in strange-looking code you may get some strange looking code as the output.

Someone at the conj asked me what would happen if you passed in a file that was all on one line. In this case, Standard Clojure Style will pretty-print the ns form and then leave the rest of the file printed on one line. This output is "weird" code by any reasonable definition, but it is also not a meaningful use case for this tool in any realistic scenario. No one writes code all on one line, and if they do they are likely not the intended audience for something like Standard Clojure Style.

zprint is an excellent project for "from scratch" pretty-printing. And some people / teams may prefer using it over Standard Clojure Style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants