ChaseKnowlden | bedc69f | 2023-08-20 19:08:28 +0200 | [diff] [blame] | 1 | " 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? |
| 9 | if exists("b:did_indent") |
| 10 | finish |
| 11 | endif |
| 12 | let b:did_indent = 1 |
| 13 | let b:undo_indent = "setlocal indentexpr< indentkeys<" |
| 14 | |
| 15 | setlocal indentexpr=s:GetQmlIndent() |
| 16 | setlocal indentkeys=0{,0},0),0],:,!^F,o,O,e,*<Return>,=*/ |
| 17 | |
| 18 | " Only define functions once per session |
| 19 | if exists("*s:GetQmlIndent") |
| 20 | finish |
| 21 | endif |
| 22 | |
| 23 | " Clean up a line of code by removing trailing '//' and '/* */' comments, and trimming |
| 24 | " whitespace |
| 25 | function! s:Trim(line) |
| 26 | return substitute(substitute(substitute(a:line, '// .*', '', ''), '/\* .* \*/', '', ''), '^\s*\|\s*$', '', 'g') |
| 27 | endfunction |
| 28 | |
| 29 | function! 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 |
| 59 | endfunction |