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