robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Christian Clasonvim-patch:e30d8e4ce01d1b74d2bf0a

master
1.5 KiB60 linesraw
1" Vim indent file
2" Language:     Kotlin
3" Maintainer:   Alexander Udalov
4" URL:          https://github.com/udalov/kotlin-vim
5" Last Change:  7 November 2021
6"               2023 Sep 17 by Vim Project (undo_indent)
7
8if exists('b:did_indent')
9    finish
10endif
11let b:did_indent = 1
12
13setlocal cinoptions& cinoptions+=j1,L0
14setlocal indentexpr=GetKotlinIndent()
15setlocal indentkeys=0},0),!^F,o,O,e,<CR>
16setlocal autoindent " TODO ?
17
18let b:undo_indent = "setlocal autoindent< cinoptions< indentexpr< indentkeys<"
19
20" TODO teach it to count bracket balance, etc.
21function! GetKotlinIndent()
22    if v:lnum == 0
23        return 0
24    endif
25
26    let prev_num = prevnonblank(v:lnum - 1)
27    let prev = getline(prev_num)
28    let prev_indent = indent(prev_num)
29    let cur = getline(v:lnum)
30
31    if cur =~ '^\s*\*'
32        return cindent(v:lnum)
33    endif
34
35    if prev =~ '^\s*\*/'
36        let st = prev
37        while st > 1
38            if getline(st) =~ '^\s*/\*'
39                break
40            endif
41            let st = st - 1
42        endwhile
43        return indent(st)
44    endif
45
46    let prev_open_paren = prev =~ '^.*(\s*$'
47    let cur_close_paren = cur =~ '^\s*).*$'
48    let prev_open_brace = prev =~ '^.*\({\|->\)\s*$'
49    let cur_close_brace = cur =~ '^\s*}.*$'
50
51    if prev_open_paren && !cur_close_paren || prev_open_brace && !cur_close_brace
52        return prev_indent + shiftwidth()
53    endif
54
55    if cur_close_paren && !prev_open_paren || cur_close_brace && !prev_open_brace
56        return prev_indent - shiftwidth()
57    endif
58
59    return prev_indent
60endfunction