codekingpro/portable-devtools
114k
1" Vim indent file2" Language: Meson3" License: VIM License4" 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 189 10" Only load this indent file when no other was loaded.11if exists("b:did_indent")12 finish13endif14let b:did_indent = 115 16" Some preliminary settings17setlocal nolisp " Make sure lisp indenting doesn't supersede us18setlocal autoindent " indentexpr isn't much help otherwise19 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")27 finish28endif29let s:keepcpo= &cpo30set cpo&vim31 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)37 echom 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'41 if getline(a:lnum - 1) =~ '\\$'42 if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'43 return indent(a:lnum - 1)44 endif45 return indent(a:lnum - 1) + (exists("g:mesonindent_continue") ? eval(g:mesonindent_continue) : (shiftwidth() * 2))46 endif47 48 " If the start of the line is in a string don't change the indent.49 if has('syntax_items')50 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"51 return -152 endif53 54 " Search backwards for the previous non-empty line.55 let plnum = prevnonblank(v:lnum - 1)56 57 if plnum == 058 " This is the first non-empty line, use zero indent.59 return 060 endif61 62 " If the previous line is inside parenthesis, use the indent of the starting63 " line.64 " Trick: use the non-existing "dummy" variable to break out of the loop when65 " going too far back.66 call cursor(plnum, 1)67 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',68 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"69 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"70 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")71 if parlnum > 072 let plindent = indent(parlnum)73 let plnumstart = parlnum74 else75 let plindent = indent(plnum)76 let plnumstart = plnum77 endif78 79 80 " When inside parenthesis: If at the first line below the parenthesis add81 " a 'shiftwidth', otherwise same as previous line.82 " i = (a83 " + b84 " + c)85 call cursor(a:lnum, 1)86 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',87 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"88 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"89 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")90 if p > 091 if p == plnum92 " When the start is inside parenthesis, only indent one 'shiftwidth'.93 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',94 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"95 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"96 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")97 if pp > 098 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())99 endif100 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : shiftwidth())101 endif102 if plnumstart == p103 return indent(plnum)104 endif105 return plindent106 endif107 108 109 " Get the line and remove a trailing comment.110 " Use syntax highlighting attributes when possible.111 let pline = getline(plnum)112 let pline_len = strlen(pline)113 if has('syntax_items')114 " If the last character in the line is a comment, do a binary search for115 " the start of the comment. synID() is slow, a linear search would take116 " too long on a long line.117 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$"118 let min = 1119 let max = pline_len120 while min < max121 let col = (min + max) / 2122 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$"123 let max = col124 else125 let min = col + 1126 endif127 endwhile128 let pline = strpart(pline, 0, min - 1)129 endif130 else131 let col = 0132 while col < pline_len133 if pline[col] == '#'134 let pline = strpart(pline, 0, col)135 break136 endif137 let col = col + 1138 endwhile139 endif140 141 " If the previous line ended the conditional/loop142 if getline(plnum) =~ '^\s*\(endif\|endforeach\)\>\s*'143 " Maintain indent144 return -1145 endif146 147 " If the previous line ended with a builtin, indent this line148 if pline =~ '^\s*\(foreach\|if\|else\|elif\)\>\s*'149 return plindent + shiftwidth()150 endif151 152 " If the current line begins with a header keyword, deindent153 if getline(a:lnum) =~ '^\s*\(else\|elif\|endif\|endforeach\)'154 155 " Unless the previous line was a one-liner156 if getline(plnumstart) =~ '^\s*\(foreach\|if\)\>\s*'157 return plindent158 endif159 160 " Or the user has already dedented161 if indent(a:lnum) <= plindent - shiftwidth()162 return -1163 endif164 165 return plindent - shiftwidth()166 endif167 168 " When after a () construct we probably want to go back to the start line.169 " a = (b170 " + c)171 " here172 if parlnum > 0173 return plindent174 endif175 176 return -1177 178endfunction179 180let &cpo = s:keepcpo181unlet s:keepcpo182 183" vim:sw=2184 