robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0b7e1730bc
master
1" Vim indent file 2" Language: Solidity 3" Maintainer: Cothi (jiungdev@gmail.com) 4" Original Author: tomlion (https://github.com/tomlion/vim-solidity) 5" Last Change: 2022 Sep 27 6" 2023 Aug 22 Vim Project (undo_indent) 7" 8" Acknowledgement: Based off of vim-javascript 9" 10" 0. Initialization {{{1 11" ================= 12 13" Only load this indent file when no other was loaded. 14if exists ( "b:did_indent" ) 15finish 16endif 17let b: did_indent = 1 18 19setlocal nosmartindent 20 21" Now, set up our indentation expression and keys that trigger it. 22setlocal indentexpr = GetSolidityIndent () 23setlocal indentkeys = 0{,0},0),0],0\,,!^F,o,O,e 24 25let b: undo_indent = "setlocal indentexpr< indentkeys< smartindent<" 26 27" Only define the function once. 28if exists ( "*GetSolidityIndent" ) 29finish 30endif 31 32let s: cpo_save = & cpo 33set cpo & vim 34 35" 1. Variables {{{1 36" ============ 37 38let s: js_keywords = '^\s*\(break\|case\|catch\|continue\|debugger\|default\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)' 39 40" Regex of syntax group names that are or delimit string or are comments. 41let s: syng_strcom = 'string\|regex\|comment\c' 42 43" Regex of syntax group names that are strings. 44let s: syng_string = 'regex\c' 45 46" Regex of syntax group names that are strings or documentation. 47let s: syng_multiline = 'comment\c' 48 49" Regex of syntax group names that are line comment. 50let s: syng_linecom = 'linecomment\c' 51 52" Expression used to check whether we should skip a match with searchpair(). 53let s: skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '" . s: syng_strcom . "'" 54 55let s: line_term = '\s*\%(\%(\/\/\).*\)\=$' 56 57" Regex that defines continuation lines, not including (, {, or [. 58let s: continuation_regex = '\%([\\*+/.:]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)' . s: line_term 59 60" Regex that defines continuation lines. 61" TODO: this needs to deal with if ...: and so on 62let s: msl_regex = '\%([\\*+/.:([]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)' . s: line_term 63 64let s: one_line_scope_regex = '\<\%(if\|else\|for\|while\)\>[^{;]*' . s: line_term 65 66" Regex that defines blocks. 67let s: block_regex = '\%([{[]\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s: line_term 68 69let s: var_stmt = '^\s*var' 70 71let s: comma_first = '^\s*,' 72let s: comma_last = ',\s*$' 73 74let s: ternary = '^\s\+[?|:]' 75let s: ternary_q = '^\s\+?' 76 77" 2. Auxiliary Functions {{{1 78" ====================== 79 80" Check if the character at lnum:col is inside a string, comment, or is ascii. 81function s: IsInStringOrComment ( lnum , col ) 82return synIDattr ( synID ( a: lnum , a: col , 1 ), 'name' ) =~ s: syng_strcom 83endfunction 84 85" Check if the character at lnum:col is inside a string. 86function s: IsInString ( lnum , col ) 87return synIDattr ( synID ( a: lnum , a: col , 1 ), 'name' ) =~ s: syng_string 88endfunction 89 90" Check if the character at lnum:col is inside a multi-line comment. 91function s: IsInMultilineComment ( lnum , col ) 92return ! s: IsLineComment ( a: lnum , a: col ) && synIDattr ( synID ( a: lnum , a: col , 1 ), 'name' ) =~ s: syng_multiline 93endfunction 94 95" Check if the character at lnum:col is a line comment. 96function s: IsLineComment ( lnum , col ) 97return synIDattr ( synID ( a: lnum , a: col , 1 ), 'name' ) =~ s: syng_linecom 98endfunction 99 100" Find line above 'lnum' that isn't empty, in a comment, or in a string. 101function s: PrevNonBlankNonString ( lnum ) 102let in_block = 0 103let lnum = prevnonblank ( a: lnum ) 104while lnum > 0 105" Go in and out of blocks comments as necessary. 106" If the line isn't empty (with opt. comment) or in a string, end search. 107let line = getline ( lnum ) 108if line =~ '/\*' 109if in_block 110let in_block = 0 111else 112break 113endif 114elseif ! in_block && line =~ '\*/' 115let in_block = 1 116elseif ! in_block && line !~ '^\s*\%(//\).*$' && ! ( s: IsInStringOrComment ( lnum , 1 ) && s: IsInStringOrComment ( lnum , strlen ( line ))) 117break 118endif 119let lnum = prevnonblank ( lnum - 1 ) 120endwhile 121return lnum 122endfunction 123 124" Find line above 'lnum' that started the continuation 'lnum' may be part of. 125function s: GetMSL ( lnum , in_one_line_scope ) 126" Start on the line we're at and use its indent. 127let msl = a: lnum 128let lnum = s: PrevNonBlankNonString ( a: lnum - 1 ) 129while lnum > 0 130" If we have a continuation line, or we're in a string, use line as MSL. 131" Otherwise, terminate search as we have found our MSL already. 132let line = getline ( lnum ) 133let col = match ( line , s: msl_regex ) + 1 134if ( col > 0 && ! s: IsInStringOrComment ( lnum , col )) || s: IsInString ( lnum , strlen ( line )) 135let msl = lnum 136else 137" Don't use lines that are part of a one line scope as msl unless the 138" flag in_one_line_scope is set to 1 139" 140if a: in_one_line_scope 141break 142end 143let msl_one_line = s: Match ( lnum , s: one_line_scope_regex ) 144if msl_one_line == 0 145break 146endif 147endif 148let lnum = s: PrevNonBlankNonString ( lnum - 1 ) 149endwhile 150return msl 151endfunction 152 153function s: RemoveTrailingComments ( content ) 154let single = '\/\/\(.*\)\s*$' 155let multi = '\/\*\(.*\)\*\/\s*$' 156return substitute ( substitute ( a: content , single , '' , '' ), multi , '' , '' ) 157endfunction 158 159" Find if the string is inside var statement (but not the first string) 160function s: InMultiVarStatement ( lnum ) 161let lnum = s: PrevNonBlankNonString ( a: lnum - 1 ) 162 163" let type = synIDattr(synID(lnum, indent(lnum) + 1, 0), 'name') 164 165" loop through previous expressions to find a var statement 166while lnum > 0 167let line = getline ( lnum ) 168 169" if the line is a js keyword 170if ( line =~ s: js_keywords ) 171" check if the line is a var stmt 172" if the line has a comma first or comma last then we can assume that we 173" are in a multiple var statement 174if ( line =~ s: var_stmt ) 175return lnum 176endif 177 178" other js keywords, not a var 179return 0 180endif 181 182let lnum = s: PrevNonBlankNonString ( lnum - 1 ) 183endwhile 184 185" beginning of program, not a var 186return 0 187endfunction 188 189" Find line above with beginning of the var statement or returns 0 if it's not 190" this statement 191function s: GetVarIndent ( lnum ) 192let lvar = s: InMultiVarStatement ( a: lnum ) 193let prev_lnum = s: PrevNonBlankNonString ( a: lnum - 1 ) 194 195if lvar 196let line = s: RemoveTrailingComments ( getline ( prev_lnum )) 197 198" if the previous line doesn't end in a comma, return to regular indent 199if ( line !~ s: comma_last ) 200return indent ( prev_lnum ) - & sw 201else 202return indent ( lvar ) + & sw 203endif 204endif 205 206return -1 207endfunction 208 209 210" Check if line 'lnum' has more opening brackets than closing ones. 211function s: LineHasOpeningBrackets ( lnum ) 212let open_0 = 0 213let open_2 = 0 214let open_4 = 0 215let line = getline ( a: lnum ) 216let pos = match ( line , '[][(){}]' , 0 ) 217while pos != -1 218if ! s: IsInStringOrComment ( a: lnum , pos + 1 ) 219let idx = stridx ( '(){}[]' , line [ pos ]) 220if idx % 2 == 0 221let open_ { idx } = open_ { idx } + 1 222else 223let open_ { idx - 1 } = open_ { idx - 1 } - 1 224endif 225endif 226let pos = match ( line , '[][(){}]' , pos + 1 ) 227endwhile 228return ( open_0 > 0 ) . ( open_2 > 0 ) . ( open_4 > 0 ) 229endfunction 230 231function s: Match ( lnum , regex ) 232let col = match ( getline ( a: lnum ), a: regex ) + 1 233return col > 0 && ! s: IsInStringOrComment ( a: lnum , col ) ? col : 0 234endfunction 235 236function s: IndentWithContinuation ( lnum , ind , width ) 237" Set up variables to use and search for MSL to the previous line. 238let p_lnum = a: lnum 239let lnum = s: GetMSL ( a: lnum , 1 ) 240let line = getline ( lnum ) 241 242" If the previous line wasn't a MSL and is continuation return its indent. 243" TODO: the || s:IsInString() thing worries me a bit. 244if p_lnum != lnum 245if s: Match ( p_lnum , s: continuation_regex ) || s: IsInString ( p_lnum , strlen ( line )) 246return a: ind 247endif 248endif 249 250" Set up more variables now that we know we aren't continuation bound. 251let msl_ind = indent ( lnum ) 252 253" If the previous line ended with [*+/.-=], start a continuation that 254" indents an extra level. 255if s: Match ( lnum , s: continuation_regex ) 256if lnum == p_lnum 257return msl_ind + a: width 258else 259return msl_ind 260endif 261endif 262 263return a: ind 264endfunction 265 266function s: InOneLineScope ( lnum ) 267let msl = s: GetMSL ( a: lnum , 1 ) 268if msl > 0 && s: Match ( msl , s: one_line_scope_regex ) 269return msl 270endif 271return 0 272endfunction 273 274function s: ExitingOneLineScope ( lnum ) 275let msl = s: GetMSL ( a: lnum , 1 ) 276if msl > 0 277" if the current line is in a one line scope .. 278if s: Match ( msl , s: one_line_scope_regex ) 279return 0 280else 281let prev_msl = s: GetMSL ( msl - 1 , 1 ) 282if s: Match ( prev_msl , s: one_line_scope_regex ) 283return prev_msl 284endif 285endif 286endif 287return 0 288endfunction 289 290" 3. GetSolidityIndent Function {{{1 291" ========================= 292 293function GetSolidityIndent () 294" 3.1. Setup {{{2 295" ---------- 296 297" Set up variables for restoring position in file. Could use v:lnum here. 298let vcol = col ( '.' ) 299 300" 3.2. Work on the current line {{{2 301" ----------------------------- 302 303let ind = -1 304" Get the current line. 305let line = getline ( v: lnum ) 306" previous nonblank line number 307let prevline = prevnonblank ( v: lnum - 1 ) 308 309" If we got a closing bracket on an empty line, find its match and indent 310" according to it. For parentheses we indent to its column - 1, for the 311" others we indent to the containing line's MSL's level. Return -1 if fail. 312let col = matchend ( line , '^\s*[],})]' ) 313if col > 0 && ! s: IsInStringOrComment ( v: lnum , col ) 314call cursor ( v: lnum , col ) 315 316let lvar = s: InMultiVarStatement ( v: lnum ) 317if lvar 318let prevline_contents = s: RemoveTrailingComments ( getline ( prevline )) 319 320" check for comma first 321if ( line [ col - 1 ] =~ ',' ) 322" if the previous line ends in comma or semicolon don't indent 323if ( prevline_contents =~ '[;,]\s*$' ) 324return indent ( s: GetMSL ( line ( '.' ), 0 )) 325" get previous line indent, if it's comma first return prevline indent 326elseif ( prevline_contents =~ s: comma_first ) 327return indent ( prevline ) 328" otherwise we indent 1 level 329else 330return indent ( lvar ) + & sw 331endif 332endif 333endif 334 335 336let bs = strpart ( '(){}[]' , stridx ( ')}]' , line [ col - 1 ]) * 2 , 2 ) 337if searchpair ( escape ( bs [ 0 ], '\[' ), '' , bs [ 1 ], 'bW' , s: skip_expr ) > 0 338if line [ col - 1 ] == ')' && col ( '.' ) != col ( '$' ) - 1 339let ind = virtcol ( '.' ) - 1 340else 341let ind = indent ( s: GetMSL ( line ( '.' ), 0 )) 342endif 343endif 344return ind 345endif 346 347" If the line is comma first, dedent 1 level 348if ( getline ( prevline ) =~ s: comma_first ) 349return indent ( prevline ) - & sw 350endif 351 352if ( line =~ s: ternary ) 353if ( getline ( prevline ) =~ s: ternary_q ) 354return indent ( prevline ) 355else 356return indent ( prevline ) + & sw 357endif 358endif 359 360" If we are in a multi-line comment, cindent does the right thing. 361if s: IsInMultilineComment ( v: lnum , 1 ) && ! s: IsLineComment ( v: lnum , 1 ) 362return cindent ( v: lnum ) 363endif 364 365" Check for multiple var assignments 366" let var_indent = s:GetVarIndent(v:lnum) 367" if var_indent >= 0 368" return var_indent 369" endif 370 371" 3.3. Work on the previous line. {{{2 372" ------------------------------- 373 374" If the line is empty and the previous nonblank line was a multi-line 375" comment, use that comment's indent. Deduct one char to account for the 376" space in ' */'. 377if line =~ '^\s*$' && s: IsInMultilineComment ( prevline , 1 ) 378return indent ( prevline ) - 1 379endif 380 381" Find a non-blank, non-multi-line string line above the current line. 382let lnum = s: PrevNonBlankNonString ( v: lnum - 1 ) 383 384" If the line is empty and inside a string, use the previous line. 385if line =~ '^\s*$' && lnum != prevline 386return indent ( prevnonblank ( v: lnum )) 387endif 388 389" At the start of the file use zero indent. 390if lnum == 0 391return 0 392endif 393 394" Set up variables for current line. 395let line = getline ( lnum ) 396let ind = indent ( lnum ) 397 398" If the previous line ended with a block opening, add a level of indent. 399if s: Match ( lnum , s: block_regex ) 400return indent ( s: GetMSL ( lnum , 0 )) + & sw 401endif 402 403" If the previous line contained an opening bracket, and we are still in it, 404" add indent depending on the bracket type. 405if line =~ '[[({]' 406let counts = s: LineHasOpeningBrackets ( lnum ) 407if counts [ 0 ] == '1' && searchpair ( '(' , '' , ')' , 'bW' , s: skip_expr ) > 0 408if col ( '.' ) + 1 == col ( '$' ) 409return ind + & sw 410else 411return virtcol ( '.' ) 412endif 413elseif counts [ 1 ] == '1' || counts [ 2 ] == '1' 414return ind + & sw 415else 416call cursor ( v: lnum , vcol ) 417end 418endif 419 420" 3.4. Work on the MSL line. {{{2 421" -------------------------- 422 423let ind_con = ind 424let ind = s: IndentWithContinuation ( lnum , ind_con , & sw ) 425 426" }}}2 427" 428" 429let ols = s: InOneLineScope ( lnum ) 430if ols > 0 431let ind = ind + & sw 432else 433let ols = s: ExitingOneLineScope ( lnum ) 434while ols > 0 && ind > 0 435let ind = ind - & sw 436let ols = s: InOneLineScope ( ols - 1 ) 437endwhile 438endif 439 440return ind 441endfunction 442 443" }}}1 444 445let & cpo = s: cpo_save 446unlet s: cpo_save