kotlin.vim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. if exists('b:did_indent')
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. setlocal cinoptions& cinoptions+=j1,L0
  12. setlocal indentexpr=GetKotlinIndent()
  13. setlocal indentkeys=0},0),!^F,o,O,e,<CR>
  14. setlocal autoindent " TODO ?
  15. let b:undo_indent = "setlocal autoindent< cinoptions< indentexpr< indentkeys<"
  16. " TODO teach it to count bracket balance, etc.
  17. function! GetKotlinIndent()
  18. if v:lnum == 0
  19. return 0
  20. endif
  21. let prev_num = prevnonblank(v:lnum - 1)
  22. let prev = getline(prev_num)
  23. let prev_indent = indent(prev_num)
  24. let cur = getline(v:lnum)
  25. if cur =~ '^\s*\*'
  26. return cindent(v:lnum)
  27. endif
  28. if prev =~ '^\s*\*/'
  29. let st = prev
  30. while st > 1
  31. if getline(st) =~ '^\s*/\*'
  32. break
  33. endif
  34. let st = st - 1
  35. endwhile
  36. return indent(st)
  37. endif
  38. let prev_open_paren = prev =~ '^.*(\s*$'
  39. let cur_close_paren = cur =~ '^\s*).*$'
  40. let prev_open_brace = prev =~ '^.*\({\|->\)\s*$'
  41. let cur_close_brace = cur =~ '^\s*}.*$'
  42. if prev_open_paren && !cur_close_paren || prev_open_brace && !cur_close_brace
  43. return prev_indent + shiftwidth()
  44. endif
  45. if cur_close_paren && !prev_open_paren || cur_close_brace && !prev_open_brace
  46. return prev_indent - shiftwidth()
  47. endif
  48. return prev_indent
  49. endfunction