neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

kotlin.vim (1566B)


      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 
      8 if exists('b:did_indent')
      9    finish
     10 endif
     11 let b:did_indent = 1
     12 
     13 setlocal cinoptions& cinoptions+=j1,L0
     14 setlocal indentexpr=GetKotlinIndent()
     15 setlocal indentkeys=0},0),!^F,o,O,e,<CR>
     16 setlocal autoindent " TODO ?
     17 
     18 let b:undo_indent = "setlocal autoindent< cinoptions< indentexpr< indentkeys<"
     19 
     20 " TODO teach it to count bracket balance, etc.
     21 function! 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
     60 endfunction