Fork me on GitHub
#vim
<
2019-12-04
>
niclasnilsson11:12:23

Formatting: does anyone know of a vim way/plugin to format forms into tables? For example looks like this:

{:deps
 {camel-snake-kebab {:mvn/version "0.4.0"}
  com.microsoft.sqlserver/mssql-jdbc {:mvn/version "7.4.1.jre8"}
  org.clojure/tools.namespace {:mvn/version "0.3.1"}}}
into something like
{:deps
 {camel-snake-kebab                  {:mvn/version "0.4.0"}
  com.microsoft.sqlserver/mssql-jdbc {:mvn/version "7.4.1.jre8"}
  org.clojure/tools.namespace        {:mvn/version "0.3.1"}}}
Not just for deps.edn obviously, but for any forms. I got inspiration from this talk, and would like some support for this in (neo)vim: https://www.youtube.com/watch?v=b5UK-VHbJlQ

herald11:12:02

https://github.com/junegunn/vim-easy-align It can be complcated to use imo. You can align the forms in your example with gaip (note that the last character is pressing the space key)

👍 8
tmt11:12:18

if you don't want a dedicated plugin you can use column -t https://stackoverflow.com/questions/1229900/reformat-in-vim-for-a-nice-column-layout

👍 4
dave15:12:58

i've been desperate to do this sort of thing in a way that's fully knowledgeable about clojure, for a long time now vim alignment plugins get you most of the way there, but they can't possibly get it right 100% of the time without parsing EDN. there are edge cases like strings containing spaces that they can't really address, as far as i'm aware i haven't dug much into cljfmt and friends... it's possible that an existing formatter can provide this, but i also know that it's a controversial preference, so maybe it isn't supported 🙂 i'm half-tempted to take a stab at writing a little utility that does this, maybe by leveraging https://github.com/borkdude/edamame

walterl18:12:50

A job for tree-sitter? "Align according to type of element at <cword>, in the local sub-tree"

niclasnilsson15:12:21

@dave, I agree fully. I don’t think it’s possible to get it right without the consept of clojure forms. It’s probably controversial, but within a project, you decide for yourself, and I seriously think it would make reading some types of code/data much faster.

dominicm15:12:15

Cljfmt has an open PR to add this

dominicm15:12:43

I'm working on a formatter that removes this kind of alignment. Most places I see it make the code worse.

dave16:12:25

i'm well aware that dominic and i are adversaries in this debate 😄

dave16:12:00

it's great to hear that there is movement to add this in cljfmt. i'm sure it will be configurable, because some people love it, some people hate it

dominicm16:12:58

Just curious, how should this code look:

{:class
 [(when some-boolean? "class-a")
  (when other "class-b")
  (when xyza-fooo "class-c")]}
or
{:class
 [(when some-boolean? "class-a")
  (when other         "class-b")
  (when xyza-fooo     "class-c")]}
As far as I'm aware, there's no obvious way to write a tool that produces the latter. You have to inspect the form and somehow decide that nested things should be aligned.

dave16:12:28

i prefer the former

dave16:12:20

i like to do the vertical alignment thing in these cases: • bindings (doseq, let, etc.) • maps • namespace declarations and maybe others, too, that i'm not thinking of

dominicm16:12:57

oh, that doesn't work for this client. You get the point 👍 Those are pretty toolable places, with the exception of ns decls, I believe that cljfmt is targetting all of them.

dominicm16:12:57

there's going to be config for custom macros (e.g. manifold's let's)

dave17:12:54

excellent

dave17:12:25

i can see how the ns decl case would be difficult to support. it isn't clear-cut how to vertically align those, assuming you want to in the first place

dave17:12:26

like if some of the requires have :refer and some have :as and some have both, what do you do?

dave17:12:33

i'm not even consistent in the way that i do it

dave17:12:52

i basically just want the :as foo aliases to be vertically aligned

niclasnilsson21:12:54

I prefer the aligned one in quite a few cases, and the more “data” it is, the more natural it feels (think tables that happens to have code snippets in them for instance).

niclasnilsson21:12:46

I tried the examples above in VSCode with Calva and it’s new experimental formatting, and the result turned out like this:

niclasnilsson21:12:58

{:deps {camel-snake-kebab                  {:mvn/version "0.4.0"}
        com.microsoft.sqlserver/mssql-jdbc {:mvn/version "7.4.1.jre8"}
        org.clojure/tools.namespace        {:mvn/version "0.3.1"}}}
and
{:class [(when some-boolean? "class-a")
         (when other "class-b")
         (when xyza-fooo "class-c")]}

dominicm22:12:42

I believe they're using a cljfmt fork

dominicm22:12:05

Branched from the vertical alignment fork.