diff --git a/autoload/jdaddy.vim b/autoload/jdaddy.vim index d35cafa..d3778d6 100644 --- a/autoload/jdaddy.vim +++ b/autoload/jdaddy.vim @@ -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: diff --git a/doc/jdaddy.txt b/doc/jdaddy.txt index 5879e50..ca8f7bb 100644 --- a/doc/jdaddy.txt +++ b/doc/jdaddy.txt @@ -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: diff --git a/plugin/jdaddy.vim b/plugin/jdaddy.vim index ffe9475..b576eb0 100644 --- a/plugin/jdaddy.vim +++ b/plugin/jdaddy.vim @@ -16,4 +16,6 @@ nnoremap gqaj :exe jdaddy#reformat('jdaddy#outer_pos', v:count1) nnoremap gwij :exe jdaddy#reformat('jdaddy#inner_pos', v:count1, v:register) nnoremap gwaj :exe jdaddy#reformat('jdaddy#outer_pos', v:count1, v:register) +nnoremap gsj :exe jdaddy#stringify('jdaddy#outer_pos', v:count1) + " vim:set et sw=2: