Skip to content

Commit bbc9fe6

Browse files
committed
Create JSON parser that doesn't consider regex syntax
A better long-term idea might be to take "brackets" as input, but let's go with this for now and see if we have more edge cases.
1 parent c32b187 commit bbc9fe6

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

autoload/sj.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,12 @@ endfunction
673673
" start and end.
674674
"
675675
" Different languages have different rules for delimiters, so it might be a
676-
" better idea to write a specific parser. See autoload/sj/argparser/js.vim for
677-
" inspiration.
676+
" better idea to write a specific parser. See autoload/sj/argparser/json.vim
677+
" for inspiration.
678678
"
679679
function! sj#ParseJsonObjectBody(from, to)
680680
" Just use js object parser
681-
let parser = sj#argparser#js#Construct(a:from, a:to, getline('.'))
681+
let parser = sj#argparser#json#Construct(a:from, a:to, getline('.'))
682682
call parser.Process()
683683
return parser.args
684684
endfunction

autoload/sj/argparser/json.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function! sj#argparser#json#Construct(start_index, end_index, line)
2+
let parser = sj#argparser#common#Construct(a:start_index, a:end_index, a:line)
3+
4+
call extend(parser, {
5+
\ 'Process': function('sj#argparser#json#Process'),
6+
\ })
7+
8+
return parser
9+
endfunction
10+
11+
" Note: Differs from "js" parser by the fact that JSON doesn't have /regexes/
12+
" to skip through.
13+
function! sj#argparser#json#Process() dict
14+
while !self.Finished()
15+
if self.body[0] == ','
16+
call self.PushArg()
17+
call self.Next()
18+
continue
19+
elseif self.body[0] =~ "[\"'{\[(]"
20+
call self.JumpPair("\"'{[(", "\"'}])")
21+
endif
22+
23+
call self.PushChar()
24+
endwhile
25+
26+
if len(sj#Trim(self.current_arg)) > 0
27+
call self.PushArg()
28+
endif
29+
endfunction

spec/plugin/yaml_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,5 +686,20 @@
686686
- prop: { a: 'one', b: 'two' }
687687
EOF
688688
end
689+
690+
specify "splitting paths in maps" do
691+
set_file_contents <<~EOF
692+
- copy: { dest: /etc/default/locale, content: "LANG=en_US.UTF-8" }
693+
EOF
694+
695+
vim.search 'dest'
696+
split
697+
698+
assert_file_contents <<~EOF
699+
- copy:
700+
dest: /etc/default/locale
701+
content: "LANG=en_US.UTF-8"
702+
EOF
703+
end
689704
end
690705
end

0 commit comments

Comments
 (0)