11" autoload/jsonpath.vim
22" Author: Victor Hallberg <https://hallberg.cc>
33
4- if exists (" g:autoloaded_jsonpath" )
4+ if exists (' g:autoloaded_jsonpath' )
55 finish
66endif
77let g: autoloaded_jsonpath = 1
88
9+ let s: plugin_dir = expand (' <sfile>:p:h:h' )
10+
911let s: escapes = {
1012 \ ' b' : " \b " ,
1113 \ ' f' : " \f " ,
@@ -45,12 +47,27 @@ function! jsonpath#scan_buffer(search_for, ...) "{{{
4547 endif
4648 let is_searching = ! empty (search_for)
4749
50+ let to_column = max ([0 , get (a: , 2 )])
51+
4852 let to_line = get (a: , 1 )
4953 if to_line < 1 || to_line > line (' $' )
5054 let to_line = line (' $' )
55+ let to_column = strchars (getline (' $' ))
5156 endif
5257
53- let to_column = max ([0 , get (a: , 2 )])
58+ if g: jsonpath_use_python
59+ if has (' python3' )
60+ return jsonpath#scan_buffer_python (search_for, to_line, to_column)
61+ endif
62+
63+ echom ' g:jsonpath_use_python set but python not found, falling back to vimscript'
64+ endif
65+
66+ return jsonpath#scan_buffer_vimscript (search_for, to_line, to_column)
67+ endfunction " }}}
68+
69+ function ! jsonpath#scan_buffer_vimscript (search_for, to_line, to_column) " {{{
70+ let is_searching = ! empty (a: search_for )
5471
5572 " Parser state
5673 let stack = []
@@ -63,7 +80,7 @@ function! jsonpath#scan_buffer(search_for, ...) "{{{
6380
6481 try
6582 let lnr = 1
66- while lnr <= to_line " {{{
83+ while lnr <= a: to_line " {{{
6784 let line = getline (lnr )
6885 let line_length = len (line )
6986 let cnr = 1
@@ -129,13 +146,13 @@ function! jsonpath#scan_buffer(search_for, ...) "{{{
129146 \} )
130147
131148 " Check if the sought search_for path has been reached?
132- if stack_modified == 1 && is_searching && s: is_equal_lists (stack, search_for)
149+ if stack_modified == 1 && is_searching && s: is_equal_lists (stack, a: search_for )
133150 return [bufnr (' %' ), lnr , cnr, 0 ]
134151 endif
135152 endif
136153
137154 " Abort if end position has been reached
138- if ! parsing_key && lnr >= to_line && cnr + 1 >= to_column
155+ if ! parsing_key && lnr >= a: to_line && cnr + 1 >= a: to_column
139156 let finished = ! is_searching " search failed if we reached end
140157 break
141158 endif
@@ -185,6 +202,32 @@ function! jsonpath#scan_buffer(search_for, ...) "{{{
185202 return []
186203endfunction " }}}
187204
205+ function ! jsonpath#scan_buffer_python (search_for, to_line, to_column) " {{{
206+ py3 << EOF
207+ import sys
208+ import vim
209+ sys.path .insert (0 , vim .eval (' s:plugin_dir' ))
210+ import jsonpath
211+
212+ stream = jsonpath.CountingLines (vim .current.buffer )
213+ result = jsonpath.scan_stream (
214+ stream,
215+ path = vim .eval (' a:search_for' ),
216+ line = int (vim .eval (' a:to_line' )),
217+ column= int (vim .eval (' a:to_column' )),
218+ )
219+ EOF
220+
221+ let result = py3eval (' result' )
222+ if empty (result)
223+ return []
224+ elseif ! empty (a: search_for )
225+ return [bufnr (' %' ), result[0 ], result[1 ], 0 ]
226+ endif
227+
228+ return result
229+ endfunction " }}}
230+
188231" Attempts to place the cursor on identifier for the given path
189232function ! jsonpath#goto (... ) " {{{
190233 let search_for = get (a: , 1 )
0 commit comments