Skip to content

Commit a44360d

Browse files
committed
Implement g:jsonpath_register option
1 parent 771bca0 commit a44360d

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ A Vim plugin which provides ways of navigating JSON document buffers.
88

99
More information is available via `:help jsonpath`.
1010

11+
## Quick Start
12+
13+
```vim
14+
" Install plugin (in this example using vim-plug)
15+
Plug 'mogelbrod/vim-jsonpath'
16+
17+
" Optionally copy path to a named register (* in this case) when calling :JsonPath
18+
let g:jsonpath_register = '*'
19+
20+
" Define mappings for json buffers
21+
au FileType json noremap <buffer> <silent> <expr> <leader>p jsonpath#echo()
22+
au FileType json noremap <buffer> <silent> <expr> <leader>g jsonpath#goto()
23+
```
24+
1125
## Mappings
1226

1327
Mappings are not provided by default but can easily be added to your `.vimrc`.
@@ -24,8 +38,17 @@ Mappings are not provided by default but can easily be added to your `.vimrc`.
2438
noremap <silent> <expr> <leader>g jsonpath#goto()
2539
```
2640

41+
## Configuration
42+
43+
See `:help jsonpath-configuration` for the available configuration options.
44+
2745
## Installation
2846

29-
Use [Vundle](https://github.com/VundleVim/Vundle.vim),
30-
[pathogen.vim](https://github.com/tpope/vim-pathogen) or another Vim package
31-
manager.
47+
Use [vim-plug](https://github.com/junegunn/vim-plug),
48+
[Vundle](https://github.com/VundleVim/Vundle.vim),
49+
[pathogen.vim](https://github.com/tpope/vim-pathogen)
50+
or another Vim package manager.
51+
52+
```vim
53+
Plug 'mogelbrod/vim-jsonpath' " example using vim-plug
54+
```

autoload/jsonpath.vim

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,15 @@ endfunction "}}}
210210
function! jsonpath#echo() "{{{
211211
echo 'Parsing buffer...' | redraw
212212
let path = jsonpath#scan_buffer([], line('.'), col('.'))
213-
echo len(path) ? 'Path: ' . join(path, g:jsonpath_delimeter) : 'Empty path'
213+
let joined = join(path, g:jsonpath_delimeter)
214+
if len(path)
215+
if exists('g:jsonpath_register')
216+
call setreg(g:jsonpath_register, joined)
217+
endif
218+
echo 'Path: ' . joined
219+
else
220+
echo 'Empty path'
221+
endif
214222
endfunction "}}}
215223

216224
" Entry point for the :JsonPath command

doc/jsonpath.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ Override using >
5353
let g:jsonpath_delimeter = ':'
5454
<
5555

56+
|g:jsonpath_register| *g:jsonpath_register*
57+
Type: |string|
58+
Default: None
59+
60+
If assigned to a valid register name (for example `*` or `+`) that register
61+
will be set to the path output whenever `jsonpath#echo()` is called.
62+
Set using >
63+
let g:jsonpath_register = '*'
64+
<
65+
5666
==============================================================================
5767
COMMANDS *jsonpath-commands*
5868

@@ -62,6 +72,7 @@ COMMANDS *jsonpath-commands*
6272
placing the cursor on it if found.
6373
If omitted: |jsonpath-echo|
6474
Outputs the path to the identifier under the cursor using |:echo|.
75+
Also copies the path to a register if |g:jsonpath_register| is set.
6576

6677

6778
==============================================================================
@@ -76,6 +87,7 @@ The plugin provides two functions that can be invoked using |:JsonPath| or via
7687

7788
`jsonpath#echo()` *jsonpath-echo*
7889
Outputs the path to the identifier under the cursor using |:echo|.
90+
Also copies the path to a register if |g:jsonpath_register| is set.
7991

8092

8193
==============================================================================

0 commit comments

Comments
 (0)