Fork me on GitHub
#editors
<
2018-06-13
>
Nick Cabral19:06:50

@pez I’m really enjoying Calva for vscode! One question. The code formatting you and your team have implemented for Calva that adheres to this guide: https://github.com/bbatsov/clojure-style-guide is excellent, but it differs in output from the clj-format library used by calva-format. Any plans long term to make calva-format use your custom code formatter?

pez19:06:57

Good to hear, @nick652! Can you give an example for where the output differs? It is not supposed to differ.

Nick Cabral19:06:44

Sure, here’s one rule from that guide that Calva gets right, but calva-format (clj-format) gets wrong: https://github.com/bbatsov/clojure-style-guide#one-space-indent

Nick Cabral19:06:08

(or
 this
 that)

Nick Cabral19:06:37

clj-format doesn’t seem to have a concept of “single-space indented forms”

Nick Cabral19:06:28

I’ve noticed a couple other discrepancies while writing code, but I didn’t write them down and I’m having some trouble recalling the specifics. Happy to add more detail as I encounter it. I figured I’d ask here before logging anything in github in case it’s as-designed (since calva-format intentionally uses a different lib).

Nick Cabral19:06:35

Thanks for the quick response, by the way.

pez21:06:49

The whole thing is a bit confusing because different formatters are in play. The formatting done by Calva still (which is a leftover from when we moved the formatting from Calva to calva-fmt) is actually ”just” a way to try get the cursor at the right place when entering new lines. It’s a very naive implementation that (sometimes) fails when comments and strings have brackets. I was hoping we should be able to use the same module as calva-fmt uses for that task too, but maybe it is not one-indent form aware. Maybe @pedrorgirardi knows more about that.

cfleming21:06:27

Formatting is one of those things which seems incredibly simple, until you actually do it….

👍 2
Nick Cabral21:06:04

great post! I really like Bob Nystrom’s writing.

cfleming21:06:16

Yeah, Bob is awesome.

Nick Cabral21:06:06

@pez that makes sense. I haven’t encountered any discrepancies that are a huge deal. I mainly figured I’d ask about it here since I was surprised to see the great work of the Calva extension get tweaked when I run calva-format with cmd+shift+p > “Format Doc”

pez21:06:19

Thanks for alerting us to it. I really would like formatting to be fully automatic and as-you-type. But that might be a pipe dream. We will keep trying for Calva Formatter to be as helpful as possible.

👍 1
Nick Cabral21:06:15

just to clarify, I’ve preferred the “as-you-type” output in every case, so it’s working great for me in that regard

pez22:06:18

I am glad to hear! It gives me guidance when re-implementing the feature in Calva Formatter, where it will be done in clojurescript instead. Also, we will need to figure out if we can get cljfmt to not undo it, right @pedrorgirardi ?

pedrorgirardi04:06:33

Hi there! And @pez, I’m a little bit out of context reading this from the thread, let me check the conversation on the channel.

pez04:06:07

This particular thread is about where the conversation started in the channel: that Calva and Calva Formatter disagrees about some formatting. So, when Calva has helped you get all new lines indented and carefully tried to honor some one-indent-form guidelines, and you format the document, things are reindented differently, and in particular, one-indents are thrown out the window.

pedrorgirardi04:06:17

Looking at cljfmt documentation, it looks like we could try to tweak a few knobs, like this one for example :cljfmt {:indents ^:replace {#".*" [[:inner 0]]}}

pedrorgirardi04:06:41

> If you want to override all existing indents, instead of just supplying new indents that are merged with the defaults, you can use the :replace hint:

pedrorgirardi04:06:57

Changing some cljfmt (that’s the formatting library that Calva uses) knobs might work, but it would even better to actually use cljfmt APIs to format as you type.

pedrorgirardi04:06:17

But I don’t know if there’s such API, I need to check the code.

pez06:06:29

I was actually going to try use cljfmt for ontype formatting for this very reason, I am sick of the formatting wars and it does not feel good to be publishing extensions that engage in this war against each other! 😃

pez06:06:29

So if we can pour the one-indent-forms from Calva into that regexp, that will be very awesome. I might try today!

😁 1
pez21:06:57

Very nice article, @cfleming !

cfleming22:06:14

@pez yeah, it’s great - it comes up on Hacker News from time to time, and generally has interesting comments too.

cfleming22:06:27

@nick652 Also, note that the style guide is contentious in some areas, and particularly around 1-space indents: https://groups.google.com/d/topic/clojure/ihiEerO3trA/discussion

dpsutton22:06:58

there was a big kerfluffle about indentation of (-> ...) as well

dpsutton22:06:23

those are good reads

cfleming22:06:23

Yeah, that one is in there 🙂

dpsutton22:06:28

my coworkers go back and forth whether arguments on the next line are aligned with the first argument or indented two spaces. lots of ways to argue about indentation

cfleming22:06:31

I tend to argue these things a lot, because it’s often the case that what people think of as “what lisp needs to do” often boils down to “that’s what emacs does, and it’s hard to change”

dpsutton22:06:55

also, at some point your codebase looks like what it looks like. can't just go changing it whenever you open a file

dpsutton22:06:14

i think the single comment ; is brutal in emacs. i think that is hard coded to go over to the right margin

cfleming22:06:30

Right. I’m actually totally in favour of prescribed language formatting these days ala gofmt

cfleming22:06:43

That ship has sailed for Clojure, but any new language should totally do that.

dpsutton22:06:50

yeah starting with that is the ticket

dpsutton22:06:01

i think elm is like that as well.

cfleming22:06:22

Oh yeah, I had that ; discussion recently too. Again, it started out as “Cursive is clearly wrong” and ended up as “Emacs is hard to change” 🙂

cfleming22:06:45

I’m going to have to support that just for mixed teams’ sanity.

dpsutton22:06:41

well i hope your customers appreciate what you do for them. it is not particularly fun. but you sure do make a professional environment for them

cfleming22:06:06

Yeah, generally they’re very appreciative - I’m lucky!

cfleming22:06:25

Lots of Cursive is really fun to work on, but lots is not too, you’re right (leiningen, I’m looking at you)

cfleming22:06:30

A funny story about customer appreciation was when I finished the current lein integration, which was something like 3 months of blood sweat and tears. I think I closed 25 issues with that change. At the last moment, I also added the CLJS community icon for CLJS files, a change which literally took 5 minutes.

dpsutton22:06:59

cljs was fast for you?

cfleming22:06:00

After the release, everyone was like: OMG! CLJS FILES NOW HAVE THE ICON!!!>!>!>!

dpsutton22:06:06

hahaha ohhh

cfleming22:06:21

There was something like a 30-tweet thread on twitter about it.

dpsutton22:06:22

little polish can go a long way ha

dpsutton22:06:38

i thought you meant you enabled cljs support and "it took 5 minutes" lol

cfleming22:06:39

I’m going to change an icon on every release 🙂

dpsutton22:06:49

make it animated while the repl is processing

cfleming22:06:51

Haha, no, that was more blood sweat and tears

dpsutton22:06:59

yeah. so many different ways things can work

cfleming22:06:00

I like that

cfleming22:06:14

Yeah, CLJS is tough, and it’s hard to keep up with

cfleming22:06:24

It’s really complex now.

dpsutton22:06:26

i wish we could all standardize on one thing and that was what cljs looked like. like figwheel is blessed. that's what we use. or everyone only uses shadow-cljs and the compiler will always have a front end on it

cfleming22:06:08

Yeah, again more closed ecosystems like Elm get some wins there

Nick Cabral23:06:46

@cfleming yeah there are a lot probably a few things in that style guide where I might disagree if pressed for an opinion. But as for Calva, I was only wondering what the goal was for parity between the vscode features it provides: realtime formatting, and the “Format Document” implementation.