robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8cbe100fcc
master
1" Vim indent file 2" Language: Meson 3" License: VIM License 4" Maintainer: Nirbheek Chauhan <nirbheek.chauhan@gmail.com> 5" Liam Beguin <liambeguin@gmail.com> 6" Original Authors: David Bustos <bustos@caltech.edu> 7" Bram Moolenaar <Bram@vim.org> 8" Last Change: 2019 Oct 18 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 16" Some preliminary settings 17setlocal nolisp " Make sure lisp indenting doesn't supersede us 18setlocal autoindent " indentexpr isn't much help otherwise 19 20setlocal indentexpr = GetMesonIndent (v:lnum) 21setlocal indentkeys += =elif,=else,=endforeach,=endif,0) 22 23let b: undo_indent = "setl ai< inde< indk< lisp<" 24 25" Only define the function once. 26if exists ( "*GetMesonIndent" ) 27finish 28endif 29let s: keepcpo = & cpo 30set cpo & vim 31 32" Come here when loading the script the first time. 33 34let s: maxoff = 50 " maximum number of lines to look backwards for () 35 36function GetMesonIndent ( lnum ) 37echom getline ( line ( "." )) 38 39" If this line is explicitly joined: If the previous line was also joined, 40" line it up with that one, otherwise add two 'shiftwidth' 41if getline ( a: lnum - 1 ) =~ '\\$' 42if a: lnum > 1 && getline ( a: lnum - 2 ) =~ '\\$' 43return indent ( a: lnum - 1 ) 44endif 45return indent ( a: lnum - 1 ) + ( exists ( "g:mesonindent_continue" ) ? eval ( g: mesonindent_continue ) : ( shiftwidth () * 2 )) 46endif 47 48" If the start of the line is in a string don't change the indent. 49if has ( 'syntax_items' ) 50 \&& synIDattr ( synID ( a: lnum , 1 , 1 ), "name" ) =~ "String$" 51return -1 52endif 53 54" Search backwards for the previous non-empty line. 55let plnum = prevnonblank ( v: lnum - 1 ) 56 57if plnum == 0 58" This is the first non-empty line, use zero indent. 59return 0 60endif 61 62" If the previous line is inside parenthesis, use the indent of the starting 63" line. 64" Trick: use the non-existing "dummy" variable to break out of the loop when 65" going too far back. 66call cursor ( plnum , 1 ) 67let parlnum = searchpair ( '(\|{\|\[' , '' , ')\|}\|\]' , 'nbW' , 68 \"line('.') < " . ( plnum - s: maxoff ) . " ? dummy :" 69 \. " synIDattr(synID(line('.'), col('.'), 1), 'name')" 70 \. " =~ '\\(Comment\\|Todo\\|String\\)$'" ) 71if parlnum > 0 72let plindent = indent ( parlnum ) 73let plnumstart = parlnum 74else 75let plindent = indent ( plnum ) 76let plnumstart = plnum 77endif 78 79 80" When inside parenthesis: If at the first line below the parenthesis add 81" a 'shiftwidth', otherwise same as previous line. 82" i = (a 83" + b 84" + c) 85call cursor ( a: lnum , 1 ) 86let p = searchpair ( '(\|{\|\[' , '' , ')\|}\|\]' , 'bW' , 87 \"line('.') < " . ( a: lnum - s: maxoff ) . " ? dummy :" 88 \. " synIDattr(synID(line('.'), col('.'), 1), 'name')" 89 \. " =~ '\\(Comment\\|Todo\\|String\\)$'" ) 90if p > 0 91if p == plnum 92" When the start is inside parenthesis, only indent one 'shiftwidth'. 93let pp = searchpair ( '(\|{\|\[' , '' , ')\|}\|\]' , 'bW' , 94 \"line('.') < " . ( a: lnum - s: maxoff ) . " ? dummy :" 95 \. " synIDattr(synID(line('.'), col('.'), 1), 'name')" 96 \. " =~ '\\(Comment\\|Todo\\|String\\)$'" ) 97if pp > 0 98return indent ( plnum ) + ( exists ( "g:pyindent_nested_paren" ) ? eval ( g: pyindent_nested_paren ) : shiftwidth ()) 99endif 100return indent ( plnum ) + ( exists ( "g:pyindent_open_paren" ) ? eval ( g: pyindent_open_paren ) : shiftwidth ()) 101endif 102if plnumstart == p 103return indent ( plnum ) 104endif 105return plindent 106endif 107 108 109" Get the line and remove a trailing comment. 110" Use syntax highlighting attributes when possible. 111let pline = getline ( plnum ) 112let pline_len = strlen ( pline ) 113if has ( 'syntax_items' ) 114" If the last character in the line is a comment, do a binary search for 115" the start of the comment. synID() is slow, a linear search would take 116" too long on a long line. 117if synIDattr ( synID ( plnum , pline_len , 1 ), "name" ) =~ "\\(Comment\\|Todo\\)$" 118let min = 1 119let max = pline_len 120while min < max 121let col = ( min + max ) / 2 122if synIDattr ( synID ( plnum , col , 1 ), "name" ) =~ "\\(Comment\\|Todo\\)$" 123let max = col 124else 125let min = col + 1 126endif 127endwhile 128let pline = strpart ( pline , 0 , min - 1 ) 129endif 130else 131let col = 0 132while col < pline_len 133if pline [ col ] == '#' 134let pline = strpart ( pline , 0 , col ) 135break 136endif 137let col = col + 1 138endwhile 139endif 140 141" If the previous line ended the conditional/loop 142if getline ( plnum ) =~ '^\s*\(endif\|endforeach\)\>\s*' 143" Maintain indent 144return -1 145endif 146 147" If the previous line ended with a builtin, indent this line 148if pline =~ '^\s*\(foreach\|if\|else\|elif\)\>\s*' 149return plindent + shiftwidth () 150endif 151 152" If the current line begins with a header keyword, deindent 153if getline ( a: lnum ) =~ '^\s*\(else\|elif\|endif\|endforeach\)' 154 155" Unless the previous line was a one-liner 156if getline ( plnumstart ) =~ '^\s*\(foreach\|if\)\>\s*' 157return plindent 158endif 159 160" Or the user has already dedented 161if indent ( a: lnum ) <= plindent - shiftwidth () 162return -1 163endif 164 165return plindent - shiftwidth () 166endif 167 168" When after a () construct we probably want to go back to the start line. 169" a = (b 170" + c) 171" here 172if parlnum > 0 173return plindent 174endif 175 176return -1 177 178endfunction 179 180let & cpo = s: keepcpo 181unlet s: keepcpo 182 183" vim:sw=2