Fork me on GitHub
#vim
<
2020-10-06
>
walterl17:10:31

What do you guys use to format large EDN blobs, on a single line, in vim? Something like passing it through pprint

walterl17:10:57

Concrete example: I have a big Ring request, logged by timbre, so it's all on one long line

dominicm17:10:52

Def it, then eval it

walterl17:10:31

Some of the values are objects, so not readily deserializable 😞

Jan K17:10:46

I have a socket REPL with cljfmt dependency on port 5353 and filter lines through a hacky oneliner shell script

echo "(do (require 'cljfmt.core)(print (cljfmt.core/reformat-string \"$(sed 's/\\/\\\\\\\\/g;s/"/\\"/g')\" $opts)))" | nc -N localhost 5353 | awk 'BEGIN{RS="\n?(nil\n)?user=> (nil\n)?"} $0'

Jan K17:10:54

Actually as it is this probably woudln't help for individual EDN values, cljfmt doesn't break them up in lines, I use it for misformatted code

noisesmith17:10:08

yeah, the vim clojure mode already knows how to indent correctly (using =) the question is where to put the line breaks

noisesmith17:10:33

you do have to tweak settings a bit to make it agree with cljfmt

walterl18:10:08

Closest I got is :%s/, /<C-v><CR>/g<CR>==... but that replaces commas in strings too 🤷

walterl18:10:26

At least it's readable like this

defndaines18:10:28

Mine isn’t pretty either …

defndaines18:10:41

nnoremap <leader>e :call FormatEDN()<CR>1G=G<CR>

function! FormatEDN()
  if search('" "')
    execute '%s/" "/"\r"/g'
  endif
  " Break lists of maps onto separate lines.
  if search('} {')
    execute '%s/} {/}\r{/g'
  endif
  " Convert commas into newlines in maps.
  if search(', :')
    execute '%s/, :/\r:/g'
  endif
endfunction

walterl20:10:39

Better than what I got. Thanks, @defndaines!