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