weavejester / cljfmt

A tool for formatting Clojure code
Eclipse Public License 1.0
1.11k stars 120 forks source link

Format clojure file with shebang #306

Closed jakepearson closed 1 year ago

jakepearson commented 1 year ago

Hi. I have a clojure file (for babashka) that has a shebang at the top (#!/usr/bin/env bb). When I have the shebang, the file is not formatted by cljfmt. Is there a way to enable this? Or is this a bug? I would be happy to work on it if someone can point me in the right direction.

weavejester commented 1 year ago

Shebangs are not currently supported, but I'm open to pull requests to add that feature.

jakepearson commented 1 year ago

Okie dokie, I'll take a look when I get some free time.

weavejester commented 1 year ago

Probably best to do it as a wrapper function, i.e. check for the shebang, remove it and pass it onto the formatting function, then take the result and add the shebang back.

jakepearson commented 1 year ago

I started to take a look and wrote a test that appeared to pass without any changes. I then found that there was already a test for shebang (https://github.com/weavejester/cljfmt/blob/master/cljfmt/test/cljfmt/core_test.cljc#L432C7-L437) that passes and reformats the code. I assume that means I need another test.

Do you think I should put the wrapper function you mentioned around fix-one and check-one in tool.clj (https://github.com/weavejester/cljfmt/blob/master/cljfmt/src/cljfmt/tool.clj#L123-L134)?

weavejester commented 1 year ago

It looks like rewrite-clj already supports reading shebangs, so there's no need for a wrapper function after all.

I ran a quick test using the latest version of cljfmt and it seems to handle reformatting fine, even with files that have a shebang. Can you provide an example of when it fails?

jakepearson commented 1 year ago

I see this in VSCode with Calva, when I try in the terminal it works just fine. I'll dig in on the Calva side. Thanks for the help!!

Have a nice day