robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ae7ab90159
master
1" Vim indent file 2" Language: JSONC (JSON with Comments) 3" Original Author: Izhak Jakov <izhak724@gmail.com> 4" Acknowledgement: Based off of vim-json maintained by Eli Parra <eli@elzr.com> 5" https://github.com/elzr/vim-json 6" Last Change: 2021-07-01 7" 2023 Aug 28 by Vim Project (undo_indent) 8 9" 0. Initialization {{{1 10" ================= 11 12" Only load this indent file when no other was loaded. 13if exists ( "b:did_indent" ) 14finish 15endif 16let b: did_indent = 1 17 18setlocal nosmartindent 19 20" Now, set up our indentation expression and keys that trigger it. 21setlocal indentexpr = GetJSONCIndent () 22setlocal indentkeys = 0{,0},0),0[,0],!^F,o,O,e 23 24let b: undo_indent = "setlocal indentexpr< indentkeys< smartindent<" 25 26" Only define the function once. 27if exists ( "*GetJSONCIndent" ) 28finish 29endif 30 31let s: cpo_save = & cpo 32set cpo & vim 33 34" 1. Variables {{{1 35" ============ 36 37let s: line_term = '\s*\%(\%(\/\/\).*\)\=$' 38" Regex that defines blocks. 39let s: block_regex = '\%({\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s: line_term 40 41" 2. Auxiliary Functions {{{1 42" ====================== 43 44" Check if the character at lnum:col is inside a string. 45function s: IsInString ( lnum , col ) 46return synIDattr ( synID ( a: lnum , a: col , 1 ), 'name' ) == 'jsonString' 47endfunction 48 49" Find line above 'lnum' that isn't empty, or in a string. 50function s: PrevNonBlankNonString ( lnum ) 51let lnum = prevnonblank ( a: lnum ) 52while lnum > 0 53" If the line isn't empty or in a string, end search. 54let line = getline ( lnum ) 55if ! ( s: IsInString ( lnum , 1 ) && s: IsInString ( lnum , strlen ( line ))) 56break 57endif 58let lnum = prevnonblank ( lnum - 1 ) 59endwhile 60return lnum 61endfunction 62 63" Check if line 'lnum' has more opening brackets than closing ones. 64function s: LineHasOpeningBrackets ( lnum ) 65let open_0 = 0 66let open_2 = 0 67let open_4 = 0 68let line = getline ( a: lnum ) 69let pos = match ( line , '[][(){}]' , 0 ) 70while pos != -1 71let idx = stridx ( '(){}[]' , line [ pos ]) 72if idx % 2 == 0 73let open_ { idx } = open_ { idx } + 1 74else 75let open_ { idx - 1 } = open_ { idx - 1 } - 1 76endif 77let pos = match ( line , '[][(){}]' , pos + 1 ) 78endwhile 79return ( open_0 > 0 ) . ( open_2 > 0 ) . ( open_4 > 0 ) 80endfunction 81 82function s: Match ( lnum , regex ) 83let col = match ( getline ( a: lnum ), a: regex ) + 1 84return col > 0 && ! s: IsInString ( a: lnum , col ) ? col : 0 85endfunction 86 87" 3. GetJSONCIndent Function {{{1 88" ========================= 89 90function GetJSONCIndent () 91if ! exists ( "s:inside_comment" ) 92let s: inside_comment = 0 93endif 94 95" 3.1. Setup {{{2 96" ---------- 97 98" Set up variables for restoring position in file. Could use v:lnum here. 99let vcol = col ( '.' ) 100 101" 3.2. Work on the current line {{{2 102" ----------------------------- 103 104 105" Get the current line. 106let line = getline ( v: lnum ) 107let ind = -1 108if s: inside_comment == 0 109" TODO iterate through all the matches in a line 110let col = matchend ( line , '\/\*' ) 111if col > 0 && ! s: IsInString ( v: lnum , col ) 112let s: inside_comment = 1 113endif 114endif 115" If we're in the middle of a comment 116if s: inside_comment == 1 117let col = matchend ( line , '\*\/' ) 118if col > 0 && ! s: IsInString ( v: lnum , col ) 119let s: inside_comment = 0 120endif 121return ind 122endif 123if line =~ '^\s*//' 124return ind 125endif 126 127" If we got a closing bracket on an empty line, find its match and indent 128" according to it. 129let col = matchend ( line , '^\s*[]}]' ) 130 131if col > 0 && ! s: IsInString ( v: lnum , col ) 132call cursor ( v: lnum , col ) 133let bs = strpart ( '{}[]' , stridx ( '}]' , line [ col - 1 ]) * 2 , 2 ) 134 135let pairstart = escape ( bs [ 0 ], '[' ) 136let pairend = escape ( bs [ 1 ], ']' ) 137let pairline = searchpair ( pairstart , '' , pairend , 'bW' ) 138 139if pairline > 0 140let ind = indent ( pairline ) 141else 142let ind = virtcol ( '.' ) - 1 143endif 144 145return ind 146endif 147 148" If we are in a multi-line string, don't do anything to it. 149if s: IsInString ( v: lnum , matchend ( line , '^\s*' ) + 1 ) 150return indent ( '.' ) 151endif 152 153" 3.3. Work on the previous line. {{{2 154" ------------------------------- 155 156let lnum = prevnonblank ( v: lnum - 1 ) 157 158if lnum == 0 159return 0 160endif 161 162" Set up variables for current line. 163let line = getline ( lnum ) 164let ind = indent ( lnum ) 165 166" If the previous line ended with a block opening, add a level of indent. 167" if s:Match(lnum, s:block_regex) 168" return indent(lnum) + shiftwidth() 169" endif 170 171" If the previous line contained an opening bracket, and we are still in it, 172" add indent depending on the bracket type. 173if line =~ '[[({]' 174let counts = s: LineHasOpeningBrackets ( lnum ) 175if counts [ 0 ] == '1' || counts [ 1 ] == '1' || counts [ 2 ] == '1' 176return ind + shiftwidth () 177else 178call cursor ( v: lnum , vcol ) 179end 180endif 181 182" }}}2 183 184return ind 185endfunction 186 187" }}}1 188 189let & cpo = s: cpo_save 190unlet s: cpo_save 191 192" vim:set sw=2 sts=2 ts=8 noet: