thrift.vim 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. " Vim indent file
  2. " Language: Apache Thrift
  3. " Maintainer: Yinzuo Jiang <jiangyinzuo@foxmail.com>
  4. " Last Change: 2024/07/29
  5. " Only load this indent file when no other was loaded.
  6. if exists("b:did_indent")
  7. finish
  8. endif
  9. let b:did_indent = 1
  10. setlocal cindent
  11. setlocal indentexpr=GetThriftIndent()
  12. let b:undo_indent = "set cindent< indentexpr<"
  13. " Only define the function once.
  14. if exists("*GetThriftIndent")
  15. finish
  16. endif
  17. let s:keepcpo= &cpo
  18. set cpo&vim
  19. function! SkipThriftBlanksAndComments(startline)
  20. let lnum = a:startline
  21. while lnum > 1
  22. let lnum = prevnonblank(lnum)
  23. if getline(lnum) =~ '\*/\s*$'
  24. while getline(lnum) !~ '/\*' && lnum > 1
  25. let lnum = lnum - 1
  26. endwhile
  27. if getline(lnum) =~ '^\s*/\*'
  28. let lnum = lnum - 1
  29. else
  30. break
  31. endif
  32. elseif getline(lnum) =~ '^\s*\(//\|#\)'
  33. let lnum = lnum - 1
  34. else
  35. break
  36. endif
  37. endwhile
  38. return lnum
  39. endfunction
  40. function GetThriftIndent()
  41. " Thrift is just like C; use the built-in C indenting and then correct a few
  42. " specific cases.
  43. let theIndent = cindent(v:lnum)
  44. " If we're in the middle of a comment then just trust cindent
  45. if getline(v:lnum) =~ '^\s*\*'
  46. return theIndent
  47. endif
  48. let line = substitute(getline(v:lnum), '\(//\|#\).*$', '', '')
  49. let previousNum = SkipThriftBlanksAndComments(v:lnum - 1)
  50. let previous = substitute(getline(previousNum), '\(//\|#\).*$', '', '')
  51. let l:indent = indent(previousNum)
  52. if previous =~ "{" && previous !~ "}"
  53. let l:indent += shiftwidth()
  54. endif
  55. if line =~ "}" && line !~ "{"
  56. let l:indent -= shiftwidth()
  57. endif
  58. return l:indent
  59. endfunction
  60. let &cpo = s:keepcpo
  61. unlet s:keepcpo
  62. " vim: sw=2 sts=2 et