summaryrefslogtreecommitdiffstats
path: root/autoload/vital/_crystal/Web/JSON.vim
diff options
context:
space:
mode:
Diffstat (limited to 'autoload/vital/_crystal/Web/JSON.vim')
-rw-r--r--autoload/vital/_crystal/Web/JSON.vim90
1 files changed, 80 insertions, 10 deletions
diff --git a/autoload/vital/_crystal/Web/JSON.vim b/autoload/vital/_crystal/Web/JSON.vim
index 0e42ee0d..e99fa733 100644
--- a/autoload/vital/_crystal/Web/JSON.vim
+++ b/autoload/vital/_crystal/Web/JSON.vim
@@ -1,5 +1,14 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1
+" ___vital___
+" NOTE: lines between '" ___vital___' is generated by :Vitalize.
+" Do not mofidify the code nor insert new lines before '" ___vital___'
+function! s:_SID() abort
+ return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
+endfunction
+execute join(['function! vital#_crystal#Web#JSON#import() abort', printf("return map({'decode': '', '_vital_depends': '', '_vital_created': '', 'encode': '', '_vital_loaded': ''}, \"vital#_crystal#function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
+delfunction s:_SID
+" ___vital___
let s:save_cpo = &cpo
set cpo&vim
@@ -15,11 +24,6 @@ function! s:_null() abort
return 0
endfunction
-let s:const = {}
-let s:const.true = function('s:_true')
-let s:const.false = function('s:_false')
-let s:const.null = function('s:_null')
-
function! s:_resolve(val, prefix) abort
let t = type(a:val)
if t == type('')
@@ -36,6 +40,13 @@ endfunction
function! s:_vital_created(module) abort
" define constant variables
+ if !exists('s:const')
+ let s:const = {}
+ let s:const.true = function('s:_true')
+ let s:const.false = function('s:_false')
+ let s:const.null = function('s:_null')
+ lockvar s:const
+ endif
call extend(a:module, s:const)
endfunction
@@ -55,7 +66,7 @@ function! s:decode(json, ...) abort
let settings = extend({
\ 'use_token': 0,
\}, get(a:000, 0, {}))
- let json = iconv(a:json, "utf-8", &encoding)
+ let json = iconv(a:json, 'utf-8', &encoding)
let json = join(split(json, "\n"), '')
let json = substitute(json, '\\u34;', '\\"', 'g')
let json = substitute(json, '\\u\(\x\x\x\x\)', '\=s:string.nr2enc_char("0x".submatch(1))', 'g')
@@ -75,7 +86,11 @@ endfunction
" @vimlint(EVL102, 0, l:true)
" @vimlint(EVL102, 0, l:false)
-function! s:encode(val) abort
+function! s:encode(val, ...) abort
+ let settings = extend({
+ \ 'indent': 0,
+ \}, get(a:000, 0, {})
+ \)
if type(a:val) == 0
return a:val
elseif type(a:val) == 1
@@ -83,7 +98,7 @@ function! s:encode(val) abort
let json = substitute(json, "\r", '\\r', 'g')
let json = substitute(json, "\n", '\\n', 'g')
let json = substitute(json, "\t", '\\t', 'g')
- return iconv(json, &encoding, "utf-8")
+ return iconv(json, &encoding, 'utf-8')
elseif type(a:val) == 2
if s:const.true == a:val
return 'true'
@@ -96,14 +111,69 @@ function! s:encode(val) abort
return string(a:val)
endif
elseif type(a:val) == 3
- return '[' . join(map(copy(a:val), 's:encode(v:val)'), ',') . ']'
+ return s:_encode_list(a:val, settings)
elseif type(a:val) == 4
- return '{' . join(map(keys(a:val), 's:encode(v:val).":".s:encode(a:val[v:val])'), ',') . '}'
+ return s:_encode_dict(a:val, settings)
else
return string(a:val)
endif
endfunction
+" @vimlint(EVL102, 1, l:ns)
+function! s:_encode_list(val, settings) abort
+ if empty(a:val)
+ return '[]'
+ elseif !a:settings.indent
+ let encoded_candidates = map(copy(a:val), 's:encode(v:val, a:settings)')
+ return printf('[%s]', join(encoded_candidates, ','))
+ else
+ let previous_indent = get(a:settings, '_previous_indent')
+ let indent = previous_indent + a:settings.indent
+ let ns = extend(copy(a:settings), {
+ \ '_previous_indent': indent,
+ \})
+ let encoded_candidates = map(
+ \ copy(a:val),
+ \ printf('''%s'' . s:encode(v:val, ns)', repeat(' ', indent)),
+ \)
+ return printf(
+ \ "[\n%s\n%s]",
+ \ join(encoded_candidates, ",\n"),
+ \ repeat(' ', previous_indent)
+ \)
+ endif
+endfunction
+" @vimlint(EVL102, 0, l:ns)
+
+" @vimlint(EVL102, 1, l:ns)
+function! s:_encode_dict(val, settings) abort
+ if empty(a:val)
+ return '{}'
+ elseif !a:settings.indent
+ let encoded_candidates = map(keys(a:val),
+ \ 's:encode(v:val, a:settings) . '':'' . s:encode(a:val[v:val], a:settings)'
+ \)
+ return printf('{%s}', join(encoded_candidates, ','))
+ else
+ let previous_indent = get(a:settings, '_previous_indent')
+ let indent = previous_indent + a:settings.indent
+ let ns = extend(copy(a:settings), {
+ \ '_previous_indent': indent,
+ \})
+ let encoded_candidates = map(keys(a:val),
+ \ printf(
+ \ '''%s'' . s:encode(v:val, ns) . '': '' . s:encode(a:val[v:val], ns)',
+ \ repeat(' ', indent),
+ \ ),
+ \)
+ return printf("{\n%s\n%s}",
+ \ join(encoded_candidates, ",\n"),
+ \ repeat(' ', previous_indent),
+ \)
+ endif
+endfunction
+" @vimlint(EVL102, 0, l:ns)
+
let &cpo = s:save_cpo
unlet s:save_cpo