Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Device Tree |
| 3 | " Maintainer: Roland Hieber, Pengutronix <rhi@pengutronix.de> |
| 4 | " |
| 5 | if exists("b:did_indent") |
| 6 | finish |
| 7 | endif |
| 8 | let b:did_indent = 1 |
| 9 | |
| 10 | setlocal autoindent |
| 11 | setlocal nosmartindent |
| 12 | setlocal indentkeys=o,O,0},0<>>,!<Ctrl-F> |
| 13 | setlocal indentexpr=GetDTSIndent() |
| 14 | setlocal nolisp |
| 15 | |
| 16 | let b:undo_indent = 'setl autoindent< smartindent< indentkeys< indentexpr< lisp<' |
| 17 | |
| 18 | function GetDTSIndent() |
| 19 | let sw = shiftwidth() |
| 20 | let lnum = v:lnum |
| 21 | let line = getline(lnum) |
| 22 | let prevline = getline(prevnonblank(lnum-1)) |
| 23 | let prevind = indent(prevnonblank(lnum-1)) |
| 24 | |
| 25 | if prevnonblank(lnum-1) < 1 |
| 26 | return 0 |
| 27 | endif |
| 28 | |
| 29 | " Don't indent header and preprocessor directives |
| 30 | if line =~ '^\s*\(/dts-\|#\(include\|define\|undef\|warn\(ing\)\?\|error\|if\(n\?def\)\?\|else\|elif\|endif\)\)' |
| 31 | return 0 |
| 32 | |
| 33 | " Don't indent /node and &label blocks |
| 34 | elseif line =~ '^\s*[/&].\+{\s*$' |
| 35 | return 0 |
| 36 | |
| 37 | " Indent to matching bracket or remove one shiftwidth if line begins with } or > |
| 38 | elseif line =~ '^\s*[}>]' |
| 39 | " set cursor to closing bracket on current line |
| 40 | let col = matchend(line, '^\s*[>}]') |
| 41 | call cursor(lnum, col) |
| 42 | |
| 43 | " determine bracket type, {} or <> |
| 44 | let pair = strpart('{}<>', stridx('}>', line[col-1]) * 2, 2) |
| 45 | |
| 46 | " find matching bracket pair |
| 47 | let pairline = searchpair(pair[0], '', pair[1], 'bW') |
| 48 | |
| 49 | if pairline > 0 |
| 50 | return indent(pairline) |
| 51 | else |
| 52 | return prevind - sw |
| 53 | endif |
| 54 | |
| 55 | " else, add one level of indent if line ends in { or < or = or , |
| 56 | elseif prevline =~ '[{<=,]$' |
| 57 | return prevind + sw |
| 58 | |
| 59 | else |
| 60 | return prevind |
| 61 | endif |
| 62 | |
| 63 | endfunction |