加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Vim:显示当前的json路径

发布时间:2020-12-15 17:02:06 所属栏目:安全 来源:网络整理
导读:我正在 vim中编辑一个非常大的,嵌套的 JSON doc(你感兴趣的版权所有),并且很想知道当前的json-path(比如json的xpath)类似于: 鉴于JSON: { "leve1": { "level2": { "level3": { "name": "goes here" } } } } 我的光标位于“name”:和“go here”之间,我想
我正在 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 /.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读