blob: 8c9fa9125061b621aa60b0be2d4fe277dfd79b85 [file] [log] [blame]
ChaseKnowldenbedc69f2023-08-20 19:08:28 +02001" Vim indent file
2" Language: QML
3" Maintainer: Chase Knowlden <haroldknowlden@gmail.com>
4" Last Change: 2023 Aug 16
5"
6" Improved JavaScript indent script.
7
8" Indent script in place for this already?
9if exists("b:did_indent")
10 finish
11endif
12let b:did_indent = 1
13let b:undo_indent = "setlocal indentexpr< indentkeys<"
14
15setlocal indentexpr=s:GetQmlIndent()
16setlocal indentkeys=0{,0},0),0],:,!^F,o,O,e,*<Return>,=*/
17
18" Only define functions once per session
19if exists("*s:GetQmlIndent")
20 finish
21endif
22
23" Clean up a line of code by removing trailing '//' and '/* */' comments, and trimming
24" whitespace
25function! s:Trim(line)
26 return substitute(substitute(substitute(a:line, '// .*', '', ''), '/\* .* \*/', '', ''), '^\s*\|\s*$', '', 'g')
27endfunction
28
29function! s:GetQmlIndent()
30 let num = v:lnum
31 let line = s:Trim(getline(num))
32
33 let pnum = prevnonblank(num - 1)
34 if pnum == 0
35 return 0
36 endif
37 let pline = s:Trim(getline(pnum))
38
39 let ind = indent(pnum)
40
41 " bracket/brace/paren blocks
42 if pline =~ '[{[(]$'
43 let ind += &sw
44 endif
45 if line =~ '^[}\])]'
46 let ind -= &sw
47 endif
48
49 " '/*' comments
50 if pline =~ '^/\*.*\*/'
51 " no indent for single-line form
52 elseif pline =~ '^/\*'
53 let ind += 1
54 elseif pline =~ '^\*/'
55 let ind -= 1
56 endif
57
58 return ind
59endfunction