objc.vim 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. " Vim indent file
  2. " Language: Objective-C
  3. " Maintainer: Kazunobu Kuriyama <kazunobu.kuriyama@nifty.com>
  4. " Last Change: 2022 Apr 06
  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. " Set the function to do the work.
  12. setlocal indentexpr=GetObjCIndent()
  13. " To make a colon (:) suggest an indentation other than a goto/switch label,
  14. setlocal indentkeys-=:
  15. setlocal indentkeys+=<:>
  16. let b:undo_indent = "setl cin< inde< indk<"
  17. " Only define the function once.
  18. if exists("*GetObjCIndent")
  19. finish
  20. endif
  21. function s:GetWidth(line, regexp)
  22. let end = matchend(a:line, a:regexp)
  23. let width = 0
  24. let i = 0
  25. while i < end
  26. if a:line[i] != "\t"
  27. let width = width + 1
  28. else
  29. let width = width + &ts - (width % &ts)
  30. endif
  31. let i = i + 1
  32. endwhile
  33. return width
  34. endfunction
  35. function s:LeadingWhiteSpace(line)
  36. let end = strlen(a:line)
  37. let width = 0
  38. let i = 0
  39. while i < end
  40. let char = a:line[i]
  41. if char != " " && char != "\t"
  42. break
  43. endif
  44. if char != "\t"
  45. let width = width + 1
  46. else
  47. let width = width + &ts - (width % &ts)
  48. endif
  49. let i = i + 1
  50. endwhile
  51. return width
  52. endfunction
  53. function GetObjCIndent()
  54. let theIndent = cindent(v:lnum)
  55. let prev_line = getline(v:lnum - 1)
  56. let cur_line = getline(v:lnum)
  57. if prev_line !~# ":" || cur_line !~# ":"
  58. return theIndent
  59. endif
  60. if prev_line !~# ";"
  61. let prev_colon_pos = s:GetWidth(prev_line, ":")
  62. let delta = s:GetWidth(cur_line, ":") - s:LeadingWhiteSpace(cur_line)
  63. let theIndent = prev_colon_pos - delta
  64. endif
  65. return theIndent
  66. endfunction