微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Vim:显示当前的json路径

我正在 vim中编辑一个非常大的,嵌套的 JSON doc(你感兴趣的版权所有),并且很想知道当前的json-path(比如json的xpath)类似于:

鉴于JSON:

{                                    
     "leve1": {                       
         "level2": {                  
             "level3": {              
                 "name": "goes here"  
             }                        
         }                            
     }                                
}

我的光标位于“name”:和“go here”之间,我想要一个显示我的命令(或状态行):

level1/level2/level3/name

或类似的.

有这样的事吗?

我编写了两个使用折叠信息的映射(因此它们应该适用于任何结构,而不仅仅是JSON).对于你的例子,他们输出
{ / "leve1": { / "level2": { / "level3": {

和(长版):

1  {
2      "leve1": {
3          "level2": {
4              "level3": {

这是小脚本.这取决于我的ingo-library plugin.

" [count]z?     Print all lines (with numbers) that start a fold where
"           the current line is contained in (for [count] upper
"           levels). When a line consists of just a symbol like "{","           the preceding non-empty line is printed,too.
" [count]z/     Like z?,but use a short output format with all line
"           contents concatenated,and without line numbers and
"           symbols.
if ! exists('g:PrintFoldHierarchySymbolLinePattern')
    let g:PrintFoldHierarchySymbolLinePattern = '^\s*{\s*$'
endif
function! s:PrintFoldHierarchy( count,isJoin )
    if foldclosed('.') != -1
        return 0
    endif

    let l:save_view = winsaveview()
        let l:levels = []
        let l:lnum = line('.')
        while (a:count ? len(l:levels) < a:count : 1)
            silent! normal! [z
            if line('.') == l:lnum
                break
            endif
            let l:lnum = line('.')
            call insert(l:levels,l:lnum)
            if getline(l:lnum) =~# g:PrintFoldHierarchySymbolLinePattern
                let l:precedingLnum = prevnonblank(l:lnum - 1)
                if l:precedingLnum > 0
                    if a:isJoin
                        let l:levels[0] = l:precedingLnum
                    else
                        call insert(l:levels,l:precedingLnum)
                    endif
                endif
            endif
        endwhile
    call winrestview(l:save_view)

    if a:isJoin
        echo
        let l:isFirst = 1
        for l:lnum in l:levels
            if l:isFirst
                let l:isFirst = 0
            else
                echohl SpecialKey
                echon ' / '
                echohl None
            endif
            echon ingo#str#Trim(getline(l:lnum))
        endfor
    else
        for l:lnum in l:levels
            echohl LineNr
            echo printf('%' . (ingo#window#dimensions#GetNumberWidth(1) - 1) . 'd ',l:lnum)
            echohl None
            echon getline(l:lnum)
        endfor
    endif

    return 1
endfunction
nnoremap <silent> z? :<C-u>if ! <SID>PrintFoldHierarchy(v:count,0)<Bar>execute "normal! \<lt>C-\>\<lt>C-n>\<lt>Esc>"<Bar>endif<CR>
nnoremap <silent> z/ :<C-u>if ! <SID>PrintFoldHierarchy(v:count,1)<Bar>execute "normal! \<lt>C-\>\<lt>C-n>\<lt>Esc>"<Bar>endif<CR>

你可以把它放到〜/ .vimrc(或单独的〜/ .vim / plugin / PrintFoldHierarchy.vim)中,并通过z调用普通模式的映射?和z /.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐