Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions autoload/jdaddy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,37 @@ function! jdaddy#combine(one, two) abort
endif
endfunction

function! jdaddy#stringify(func, count) abort
let [lopen, copen, lclose, cclose] = call(a:func, [a:count])
if !lopen
return ''
endif
if lopen == lclose
let body = getline(lopen)[copen-1 : cclose-1]
else
let body = getline(lopen)[copen-1 : -1] . "\n" . join(map(getline(lopen+1, lclose-1), 'v:val."\n"'), '') . getline(lclose)[0 : cclose-1]
endif
if &filetype ==# 'vim'
let body = substitute(body, "\n\\s*\\\\", "\n", "g")
endif
try
if a:0
let json = jdaddy#combine(jdaddy#parse(body), jdaddy#parse(getreg(a:1)))
else
let json = jdaddy#parse(body)
endif
catch /^jdaddy:/
return 'echoerr '.string(v:exception)
endtry
let dump =
\ (copen == 1 ? '' : getline(lopen)[0 : copen-2]) .
\ jdaddy#dump(json) .
\ getline(lclose)[cclose : -1]
let dump = substitute(dump, "\\(:\\|,\\)[[:space:]]", "\\1", "g")
call append(lclose, split(dump, "\n"))
silent exe lopen.','.lclose.'delete _'
call setpos('.', [0, lopen, copen, 0])
return ''
endfunction

" vim:set et sw=2:
4 changes: 4 additions & 0 deletions doc/jdaddy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ gqaj Replace the outermost JSON with a pretty printed
["x]gwaj Merge the JSON from the register into the outermost
JSON.

*gsj*
gsj Convert the JSON into a string.


Pretty printing indents based on 'shiftwidth' and wraps at 'textwidth'.

vim:tw=78:et:ft=help:norl:
2 changes: 2 additions & 0 deletions plugin/jdaddy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ nnoremap <silent> gqaj :exe jdaddy#reformat('jdaddy#outer_pos', v:count1)<CR>
nnoremap <silent> gwij :exe jdaddy#reformat('jdaddy#inner_pos', v:count1, v:register)<CR>
nnoremap <silent> gwaj :exe jdaddy#reformat('jdaddy#outer_pos', v:count1, v:register)<CR>

nnoremap <silent> gsj :exe jdaddy#stringify('jdaddy#outer_pos', v:count1)<CR>

" vim:set et sw=2: