vb.vim 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. " Vim indent file
  2. " Language: VisualBasic (ft=vb) / Basic (ft=basic) / SaxBasic (ft=vb)
  3. " Author: Johannes Zellner <johannes@zellner.org>
  4. " Maintainer: Michael Soyka (mssr953@gmail.com)
  5. " Last Change: Fri, 18 Jun 2004 07:22:42 CEST
  6. " Small update 2010 Jul 28 by Maxim Kim
  7. " 2022/12/15: add support for multiline statements.
  8. " 2022/12/21: move VbGetIndent from global to script-local scope
  9. " 2022/12/26: recognize "Type" keyword
  10. if exists("b:did_indent")
  11. finish
  12. endif
  13. let b:did_indent = 1
  14. setlocal autoindent
  15. setlocal indentexpr=s:VbGetIndent(v:lnum)
  16. setlocal indentkeys&
  17. setlocal indentkeys+==~else,=~elseif,=~end,=~wend,=~case,=~next,=~select,=~loop
  18. let b:undo_indent = "set ai< indentexpr< indentkeys<"
  19. " Only define the function once.
  20. if exists("*s:VbGetIndent")
  21. finish
  22. endif
  23. function s:VbGetIndent(lnum)
  24. let this_lnum = a:lnum
  25. let this_line = getline(this_lnum)
  26. " labels and preprocessor get zero indent immediately
  27. let LABELS_OR_PREPROC = '^\s*\(\<\k\+\>:\s*$\|#.*\)'
  28. if this_line =~? LABELS_OR_PREPROC
  29. return 0
  30. endif
  31. " Get the current value of "shiftwidth"
  32. let bShiftwidth = shiftwidth()
  33. " Find a non-blank line above the current line.
  34. " Skip over labels and preprocessor directives.
  35. let lnum = this_lnum
  36. while lnum > 0
  37. let lnum = prevnonblank(lnum - 1)
  38. let previous_line = getline(lnum)
  39. if previous_line !~? LABELS_OR_PREPROC
  40. break
  41. endif
  42. endwhile
  43. " Hit the start of the file, use zero indent.
  44. if lnum == 0
  45. return 0
  46. endif
  47. " Variable "previous_line" now contains the text in buffer line "lnum".
  48. " Multi-line statements have the underscore character at end-of-line:
  49. "
  50. " object.method(arguments, _
  51. " arguments, _
  52. " arguments)
  53. "
  54. " and require extra logic to determine the correct indentation.
  55. "
  56. " Case 1: Line "lnum" is the first line of a multiline statement.
  57. " Line "lnum" will have a trailing underscore character
  58. " but the preceding non-blank line does not.
  59. " Line "this_lnum" will be indented relative to "lnum".
  60. "
  61. " Case 2: Line "lnum" is the last line of a multiline statement.
  62. " Line "lnum" will not have a trailing underscore character
  63. " but the preceding non-blank line will.
  64. " Line "this_lnum" will have the same indentation as the starting
  65. " line of the multiline statement.
  66. "
  67. " Case 3: Line "lnum" is neither the first nor last line.
  68. " Lines "lnum" and "lnum-1" will have a trailing underscore
  69. " character.
  70. " Line "this_lnum" will have the same indentation as the preceding
  71. " line.
  72. "
  73. " No matter which case it is, the starting line of the statement must be
  74. " found. It will be assumed that multiline statements cannot have
  75. " intermingled comments, statement labels, preprocessor directives or
  76. " blank lines.
  77. "
  78. let lnum_is_continued = (previous_line =~ '_$')
  79. if lnum > 1
  80. let before_lnum = prevnonblank(lnum-1)
  81. let before_previous_line = getline(before_lnum)
  82. else
  83. let before_lnum = 0
  84. let before_previous_line = ""
  85. endif
  86. if before_previous_line !~ '_$'
  87. " Variable "previous_line" contains the start of a statement.
  88. "
  89. let ind = indent(lnum)
  90. if lnum_is_continued
  91. let ind += bShiftwidth
  92. endif
  93. elseif ! lnum_is_continued
  94. " Line "lnum" contains the last line of a multiline statement.
  95. " Need to find where this multiline statement begins
  96. "
  97. while before_lnum > 0
  98. let before_lnum -= 1
  99. if getline(before_lnum) !~ '_$'
  100. let before_lnum += 1
  101. break
  102. endif
  103. endwhile
  104. if before_lnum == 0
  105. let before_lnum = 1
  106. endif
  107. let previous_line = getline(before_lnum)
  108. let ind = indent(before_lnum)
  109. else
  110. " Line "lnum" is not the first or last line of a multiline statement.
  111. "
  112. let ind = indent(lnum)
  113. endif
  114. " Add
  115. if previous_line =~? '^\s*\<\(begin\|\%(\%(private\|public\|friend\)\s\+\)\=\%(function\|sub\|property\|enum\|type\)\|select\|case\|default\|if\|else\|elseif\|do\|for\|while\|with\)\>'
  116. let ind = ind + bShiftwidth
  117. endif
  118. " Subtract
  119. if this_line =~? '^\s*\<end\>\s\+\<select\>'
  120. if previous_line !~? '^\s*\<select\>'
  121. let ind = ind - 2 * bShiftwidth
  122. else
  123. " this case is for an empty 'select' -- 'end select'
  124. " (w/o any case statements) like:
  125. "
  126. " select case readwrite
  127. " end select
  128. let ind = ind - bShiftwidth
  129. endif
  130. elseif this_line =~? '^\s*\<\(end\|else\|elseif\|until\|loop\|next\|wend\)\>'
  131. let ind = ind - bShiftwidth
  132. elseif this_line =~? '^\s*\<\(case\|default\)\>'
  133. if previous_line !~? '^\s*\<select\>'
  134. let ind = ind - bShiftwidth
  135. endif
  136. endif
  137. return ind
  138. endfunction
  139. " vim:sw=4