robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
dfbbc6dd6b
master
1" Vim indent file 2" Language: Julia 3" Maintainer: Carlo Baldassi <carlobaldassi@gmail.com> 4" Homepage: https://github.com/JuliaEditorSupport/julia-vim 5" Last Change: 2022 Jun 14 6" 2023 Aug 28 by Vim Project (undo_indent) 7" 2025 Dec 08 by Vim Project (update indent script from upstream #18894) 8" Notes: originally based on Bram Moolenaar's indent file for vim 9 10" Only load this indent file when no other was loaded. 11if exists ( "b:did_indent" ) 12finish 13endif 14let b: did_indent = 1 15 16setlocal autoindent 17 18setlocal indentexpr = GetJuliaIndent () 19setlocal indentkeys += =end,=else,=catch,=finally,),],} 20setlocal indentkeys -= 0# 21setlocal indentkeys -= : 22setlocal indentkeys -= 0{ 23setlocal indentkeys -= 0} 24setlocal nosmartindent 25 26let b: undo_indent = "setlocal autoindent< indentexpr< indentkeys< smartindent<" 27 28" Only define the function once. 29if exists ( "*GetJuliaIndent" ) 30finish 31endif 32 33let s: skipPatternsBasic = '\<julia\%(Comment\%([LM]\|Delim\)\)\>' 34let s: skipPatterns = '\<julia\%(Comprehension\%(For\|If\)\|RangeKeyword\|Comment\%([LM]\|Delim\)\|\%([bs]\|Shell\|Printf\|Doc\)\?String\|StringPrefixed\|DocStringM\(Raw\)\?\|RegEx\|SymbolS\?\|Macro\|Dotted\)\>' 35 36function JuliaMatch ( lnum , str , regex , st , ...) 37let s = a: st 38let e = a: 0 > 0 ? a: 1 : -1 39let basic_skip = a: 0 > 1 ? a: 2 : 'all' 40let skip = basic_skip ==# 'basic' ? s: skipPatternsBasic : s: skipPatterns 41while 1 42let f = match ( a: str , '\C' . a: regex , s ) 43if e >= 0 && f >= e 44return -1 45endif 46if f >= 0 47let attr = synIDattr ( synID ( a: lnum , f + 1 , 1 ), "name" ) 48let attrT = synIDattr ( synID ( a: lnum , f + 1 , 0 ), "name" ) 49if attr =~# skip || attrT =~# skip 50let s = f + 1 51continue 52endif 53endif 54break 55endwhile 56return f 57endfunction 58 59function GetJuliaNestingStruct ( lnum , ...) 60" Auxiliary function to inspect the block structure of a line 61let line = getline ( a: lnum ) 62let s = a: 0 > 0 ? a: 1 : 0 63let e = a: 0 > 1 ? a: 2 : -1 64let blocks_stack = [] 65let num_closed_blocks = 0 66while 1 67let fb = JuliaMatch ( a: lnum , line , '\<\%(if\|else\%(if\)\?\|while\|for\|try\|catch\|finally\|\%(staged\)\?function\|macro\|begin\|mutable\s\+struct\|\%(mutable\s\+\)\@<!struct\|\%(abstract\|primitive\)\s\+type\|let\|\%(bare\)\?module\|quote\|do\)\>' , s , e ) 68let fe = JuliaMatch ( a: lnum , line , '\<end\>' , s , e ) 69 70if fb < 0 && fe < 0 71" No blocks found 72break 73end 74 75if fb >= 0 && ( fb < fe || fe < 0 ) 76" The first occurrence is an opening block keyword 77" Note: some keywords (elseif,else,catch,finally) are both 78" closing blocks and opening new ones 79 80let i = JuliaMatch ( a: lnum , line , '\<if\>' , s ) 81if i >= 0 && i == fb 82let s = i + 1 83call add ( blocks_stack , 'if' ) 84continue 85endif 86let i = JuliaMatch ( a: lnum , line , '\<elseif\>' , s ) 87if i >= 0 && i == fb 88let s = i + 1 89if len ( blocks_stack ) > 0 && blocks_stack [ -1 ] == 'if' 90let blocks_stack [ -1 ] = 'elseif' 91elseif ( len ( blocks_stack ) > 0 && blocks_stack [ -1 ] != 'elseif' ) || len ( blocks_stack ) == 0 92call add ( blocks_stack , 'elseif' ) 93let num_closed_blocks += 1 94endif 95continue 96endif 97let i = JuliaMatch ( a: lnum , line , '\<else\>' , s ) 98if i >= 0 && i == fb 99let s = i + 1 100if len ( blocks_stack ) > 0 && blocks_stack [ -1 ] =~# '\<\%(\%(else\)\=if\|catch\)\>' 101let blocks_stack [ -1 ] = 'else' 102else 103call add ( blocks_stack , 'else' ) 104let num_closed_blocks += 1 105endif 106continue 107endif 108 109let i = JuliaMatch ( a: lnum , line , '\<try\>' , s ) 110if i >= 0 && i == fb 111let s = i + 1 112call add ( blocks_stack , 'try' ) 113continue 114endif 115let i = JuliaMatch ( a: lnum , line , '\<catch\>' , s ) 116if i >= 0 && i == fb 117let s = i + 1 118if len ( blocks_stack ) > 0 && blocks_stack [ -1 ] =~# '\<\%(try\|finally\)\>' 119let blocks_stack [ -1 ] = 'catch' 120else 121call add ( blocks_stack , 'catch' ) 122let num_closed_blocks += 1 123endif 124continue 125endif 126let i = JuliaMatch ( a: lnum , line , '\<finally\>' , s ) 127if i >= 0 && i == fb 128let s = i + 1 129if len ( blocks_stack ) > 0 && blocks_stack [ -1 ] =~# '\<\%(try\|catch\|else\)\>' 130let blocks_stack [ -1 ] = 'finally' 131else 132call add ( blocks_stack , 'finally' ) 133let num_closed_blocks += 1 134endif 135continue 136endif 137 138let i = JuliaMatch ( a: lnum , line , '\<\%(bare\)\?module\>' , s ) 139if i >= 0 && i == fb 140let s = i + 1 141if i == 0 142call add ( blocks_stack , 'col1module' ) 143else 144call add ( blocks_stack , 'other' ) 145endif 146continue 147endif 148 149let i = JuliaMatch ( a: lnum , line , '\<\%(while\|for\|function\|macro\|begin\|\%(mutable\s\+\)\?struct\|\%(abstract\|primitive\)\s\+type\|let\|quote\|do\)\>' , s ) 150if i >= 0 && i == fb 151if match ( line , '\C\<\%(mutable\|abstract\|primitive\)' , i ) != -1 152let s = i + 11 153else 154let s = i + 1 155endif 156call add ( blocks_stack , 'other' ) 157continue 158endif 159 160" Note: it should be impossible to get here 161break 162 163else 164" The first occurrence is an 'end' 165 166let s = fe + 1 167if len ( blocks_stack ) == 0 168let num_closed_blocks += 1 169else 170call remove ( blocks_stack , -1 ) 171endif 172continue 173 174endif 175 176" Note: it should be impossible to get here 177break 178endwhile 179let num_open_blocks = len ( blocks_stack ) - count ( blocks_stack , 'col1module' ) 180return [ num_open_blocks , num_closed_blocks ] 181endfunction 182 183function GetJuliaNestingBrackets ( lnum , c ) 184" Auxiliary function to inspect the brackets structure of a line 185let line = getline ( a: lnum )[ 0 : ( a: c - 1 )] 186let s = 0 187let brackets_stack = [] 188let last_closed_bracket = -1 189while 1 190let fb = JuliaMatch ( a: lnum , line , '[([{]' , s ) 191let fe = JuliaMatch ( a: lnum , line , '[])}]' , s ) 192 193if fb < 0 && fe < 0 194" No brackets found 195break 196end 197 198if fb >= 0 && ( fb < fe || fe < 0 ) 199" The first occurrence is an opening bracket 200 201let i = JuliaMatch ( a: lnum , line , '(' , s ) 202if i >= 0 && i == fb 203let s = i + 1 204call add ( brackets_stack , [ 'par' , i ]) 205continue 206endif 207 208let i = JuliaMatch ( a: lnum , line , '\[' , s ) 209if i >= 0 && i == fb 210let s = i + 1 211call add ( brackets_stack , [ 'sqbra' , i ]) 212continue 213endif 214 215let i = JuliaMatch ( a: lnum , line , '{' , s ) 216if i >= 0 && i == fb 217let s = i + 1 218call add ( brackets_stack , [ 'curbra' , i ]) 219continue 220endif 221 222" Note: it should be impossible to get here 223break 224 225else 226" The first occurrence is a closing bracket 227 228let i = JuliaMatch ( a: lnum , line , ')' , s ) 229if i >= 0 && i == fe 230let s = i + 1 231if len ( brackets_stack ) > 0 && brackets_stack [ -1 ][ 0 ] == 'par' 232call remove ( brackets_stack , -1 ) 233else 234let last_closed_bracket = i + 1 235endif 236continue 237endif 238 239let i = JuliaMatch ( a: lnum , line , ']' , s ) 240if i >= 0 && i == fe 241let s = i + 1 242if len ( brackets_stack ) > 0 && brackets_stack [ -1 ][ 0 ] == 'sqbra' 243call remove ( brackets_stack , -1 ) 244else 245let last_closed_bracket = i + 1 246endif 247continue 248endif 249 250let i = JuliaMatch ( a: lnum , line , '}' , s ) 251if i >= 0 && i == fe 252let s = i + 1 253if len ( brackets_stack ) > 0 && brackets_stack [ -1 ][ 0 ] == 'curbra' 254call remove ( brackets_stack , -1 ) 255else 256let last_closed_bracket = i + 1 257endif 258continue 259endif 260 261" Note: it should be impossible to get here 262break 263 264endif 265 266" Note: it should be impossible to get here 267break 268endwhile 269let first_open_bracket = -1 270let last_open_bracket = -1 271let infuncargs = 0 272if len ( brackets_stack ) > 0 273let first_open_bracket = brackets_stack [ 0 ][ 1 ] 274let last_open_bracket = brackets_stack [ -1 ][ 1 ] 275if brackets_stack [ -1 ][ 0 ] == 'par' && IsFunctionArgPar ( a: lnum , last_open_bracket + 1 ) 276let infuncargs = 1 277endif 278endif 279return [ first_open_bracket , last_open_bracket , last_closed_bracket , infuncargs ] 280endfunction 281 282let s: bracketBlocks = '\<julia\%(\%(\%(Printf\)\?Par\|SqBra\%(Idx\)\?\|CurBra\)Block\|ParBlockInRange\|StringVars\%(Par\|SqBra\|CurBra\)\|Dollar\%(Par\|SqBra\)\|QuotedParBlockS\?\)\>' 283 284function IsInBrackets ( lnum , c ) 285let stack = map ( synstack ( a: lnum , a: c ), 'synIDattr(v:val, "name")' ) 286call filter ( stack , 'v:val =~# s:bracketBlocks' ) 287return len ( stack ) > 0 288endfunction 289 290function IsInDocString ( lnum ) 291let stack = map ( synstack ( a: lnum , 1 ), 'synIDattr(v:val, "name")' ) 292call filter ( stack , 'v:val =~# "\\<juliaDocString\\(Delim\\|M\\\(Raw\\)\\?\\)\\?\\>"' ) 293return len ( stack ) > 0 294endfunction 295 296function IsInContinuationImportLine ( lnum ) 297let stack = map ( synstack ( a: lnum , 1 ), 'synIDattr(v:val, "name")' ) 298call filter ( stack , 'v:val =~# "\\<juliaImportLine\\>"' ) 299if len ( stack ) == 0 300return 0 301endif 302return JuliaMatch ( a: lnum , getline ( a: lnum ), '\<\%(import\|using\|export\|public\)\>' , indent ( a: lnum )) == -1 303endfunction 304 305function IsFunctionArgPar ( lnum , c ) 306if a: c == 0 307return 0 308endif 309let stack = map ( synstack ( a: lnum , a: c - 1 ), 'synIDattr(v:val, "name")' ) 310return len ( stack ) >= 2 && stack [ -2 ] ==# 'juliaFunctionDef' 311endfunction 312 313function JumpToMatch ( lnum , last_closed_bracket ) 314" we use the % command to skip back (tries to use matchit if possible, 315" otherwise resorts to vim's default, which is buggy but better than 316" nothing) 317call cursor ( a: lnum , a: last_closed_bracket ) 318let percmap = maparg ( "%" , "n" ) 319if exists ( "g:loaded_matchit" ) && percmap =~# 'Match\%(it\|_wrapper\)' 320normal % 321else 322normal ! % 323end 324endfunction 325 326" Auxiliary function to find a line which does not start in the middle of a 327" multiline bracketed expression, to be used as reference for block 328" indentation. 329function LastBlockIndent ( lnum ) 330let lnum = a: lnum 331let ind = 0 332while lnum > 0 333let ind = indent ( lnum ) 334if ind == 0 335return [ lnum , 0 ] 336endif 337if ! IsInBrackets ( lnum , 1 ) 338break 339endif 340let lnum = prevnonblank ( lnum - 1 ) 341endwhile 342return [ max ([ lnum , 1 ]), ind ] 343endfunction 344 345function GetJuliaIndent () 346" Do not alter doctrings indentation 347if IsInDocString ( v: lnum ) 348return -1 349endif 350 351" Find a non-blank line above the current line. 352let lnum = prevnonblank ( v: lnum - 1 ) 353 354" At the start of the file use zero indent. 355if lnum == 0 356return 0 357endif 358 359let ind = -1 360let st = -1 361let lim = -1 362 363" Multiline bracketed expressions take precedence 364let align_brackets = get ( g:, "julia_indent_align_brackets" , 1 ) 365let align_funcargs = get ( g:, "julia_indent_align_funcargs" , 0 ) 366let c = len ( getline ( lnum )) + 1 367while IsInBrackets ( lnum , c ) 368let [ first_open_bracket , last_open_bracket , last_closed_bracket , infuncargs ] = GetJuliaNestingBrackets ( lnum , c ) 369 370" First scenario: the previous line has a hanging open bracket: 371" set the indentation to match the opening bracket (plus an extra space) 372" unless we're in a function arguments list or alignment is disabled, in 373" which case we just add an extra indent 374if last_open_bracket != -1 375if ( ! infuncargs && align_brackets ) || ( infuncargs && align_funcargs ) 376let st = last_open_bracket 377let ind = virtcol ([ lnum , st + 1 ]) 378else 379let ind = indent ( lnum ) + shiftwidth () 380endif 381 382" Second scenario: some multiline bracketed expression was closed in the 383" previous line. But since we know we are still in a bracketed expression, 384" we need to find the line where the bracket was opened 385elseif last_closed_bracket != -1 386call JumpToMatch ( lnum , last_closed_bracket ) 387if line ( "." ) == lnum 388" something wrong here, give up 389let ind = indent ( lnum ) 390else 391let lnum = line ( "." ) 392let c = col ( "." ) - 1 393if c == 0 394" uhm, give up 395let ind = 0 396else 397" we skipped a bracket set, keep searching for an opening bracket 398let lim = c 399continue 400endif 401endif 402 403" Third scenario: nothing special: keep the indentation 404else 405let ind = indent ( lnum ) 406endif 407 408" Does the current line start with a closing bracket? Then depending on 409" the situation we align it with the opening one, or we let the rest of 410" the code figure it out (the case in which we're closing a function 411" argument list is special-cased) 412if JuliaMatch ( v: lnum , getline ( v: lnum ), '[])}]' , indent ( v: lnum )) == indent ( v: lnum ) && ind > 0 413if ! align_brackets && ! align_funcargs 414call JumpToMatch ( v: lnum , indent ( v: lnum )) 415return indent ( line ( "." )) 416elseif ( align_brackets && getline ( v: lnum )[ indent ( v: lnum )] != ')' ) || align_funcargs 417return ind - 1 418else " must be a ')' and align_brackets==1 and align_funcargs==0 419call JumpToMatch ( v: lnum , indent ( v: lnum )) 420if IsFunctionArgPar ( line ( "." ), col ( "." )) 421let ind = -1 422else 423return ind - 1 424endif 425endif 426endif 427 428break 429endwhile 430 431if ind == -1 432" We are not in a multiline bracketed expression. Thus we look for a 433" previous line to use as a reference 434let [ lnum , ind ] = LastBlockIndent ( lnum ) 435let c = len ( getline ( lnum )) + 1 436if IsInBrackets ( lnum , c ) 437let [ first_open_bracket , last_open_bracket , last_closed_bracket , infuncargs ] = GetJuliaNestingBrackets ( lnum , c ) 438let lim = first_open_bracket 439endif 440end 441 442" Analyse the reference line 443let [ num_open_blocks , num_closed_blocks ] = GetJuliaNestingStruct ( lnum , st , lim ) 444" Increase indentation for each newly opened block in the reference line 445let ind += shiftwidth () * num_open_blocks 446 447" Analyse the current line 448let [ num_open_blocks , num_closed_blocks ] = GetJuliaNestingStruct ( v: lnum ) 449" Decrease indentation for each closed block in the current line 450let ind -= shiftwidth () * num_closed_blocks 451 452" Additional special case: multiline import/using/export/public statements 453 454let prevline = getline ( lnum ) 455" Are we in a multiline import/using/export/public statement, right below the 456" opening line? 457if IsInContinuationImportLine ( v: lnum ) && ! IsInContinuationImportLine ( lnum ) 458if get ( g:, 'julia_indent_align_import' , 1 ) 459" if the opening line has a colon followed by non-comments, use it as 460" reference point 461let cind = JuliaMatch ( lnum , prevline , ':' , indent ( lnum ), lim ) 462if cind >= 0 463let nonwhiteind = JuliaMatch ( lnum , prevline , '\S' , cind + 1 , -1 , 'basic' ) 464if nonwhiteind >= 0 465" return match(prevline, '\S', cind+1) " a bit overkill... 466return cind + 2 467endif 468else 469" if the opening line is not a naked import/using/export/public statement, use 470" it as reference 471let iind = JuliaMatch ( lnum , prevline , '\<import\|using\|export\|public\>' , indent ( lnum ), lim ) 472if iind >= 0 473" assuming whitespace after using... so no `using(XYZ)` please! 474let nonwhiteind = JuliaMatch ( lnum , prevline , '\S' , iind + 6 , -1 , 'basic' ) 475if nonwhiteind >= 0 476return match ( prevline , '\S' , iind + 6 ) 477endif 478endif 479endif 480endif 481let ind += shiftwidth () 482 483" Or did we just close a multiline import/using/export/public statement? 484elseif ! IsInContinuationImportLine ( v: lnum ) && IsInContinuationImportLine ( lnum ) 485" find the starting line of the statement 486let ilnum = 0 487for iln in range ( lnum - 1 , 1 , -1 ) 488if ! IsInContinuationImportLine ( iln ) 489let ilnum = iln 490break 491endif 492endfor 493if ilnum == 0 494" something went horribly wrong, give up 495let ind = indent ( lnum ) 496endif 497let ind = indent ( ilnum ) 498endif 499 500return ind 501endfunction