vb.vim (6397B)
1 vim9script 2 3 # Vim indent file 4 # Language: VisualBasic (ft=vb) / Basic (ft=basic) / SaxBasic (ft=vb) 5 # Author: Johannes Zellner <johannes@zellner.org> 6 # Maintainer: Michael Soyka (mssr953@gmail.com) 7 # Contributors: Doug Kearns (dougkearns@gmail.com) 8 # Last Change: Fri, 18 Jun 2004 07:22:42 CEST 9 # Small update 2010 Jul 28 by Maxim Kim 10 # 2022/12/15: add support for multiline statements. 11 # 2022/12/21: move VbGetIndent from global to script-local scope 12 # 2022/12/26: recognize "Type" keyword 13 # 2023/07/13: correct/extend line continuation pattern (Doug Kearns) 14 # 2023/07/14: add more keywords; various optimizations (Doug Kearns) 15 # 2023/07/20: convert to Vim9 script 16 # 2023/07/23: improve detection of preproc directives (Doug Kearns) 17 18 if exists("b:did_indent") 19 finish 20 endif 21 b:did_indent = v:true 22 23 setlocal autoindent 24 setlocal indentexpr=VbGetIndent() 25 setlocal indentkeys& 26 setlocal indentkeys+==~else,=~elseif,=~end,=~wend,=~case,=~next,=~select,=~loop 27 28 b:undo_indent = "setlocal autoindent< indentexpr< indentkeys<" 29 30 # Only define the function once. 31 if exists("*VbGetIndent") 32 finish 33 endif 34 35 # These regular expressions identify statement labels and preprocessor 36 # directives. 37 # 38 const RE_LABEL: string = '^\s*\k\+:\s*$' 39 const RE_PREPROC: string = 40 '^\s*#\%(const\|if\|elseif\|else\|end\|region\|enable\|disable\)\>' 41 42 # Microsoft documentation states that line continuation is indicated by a 43 # two-character sequence at end-of-line: a space character followed by an 44 # underscore. Nonetheless, it has been reported that additional 45 # whitespace after the underscore is also allowed. We will support both. 46 # However, VB 16.0 also permits a comment after the underscore which, 47 # for simplicity, we do not support. 48 # 49 const RE_LINE_CONTINUATION: string = '\s_\s*$' 50 51 # The following regular expressions are used to increase the indent 52 # after statements that open a new scope. 53 # 54 const RE_INCR_INDENT_1: string = 55 '^\s*\%(begin\|select\|case\|default\|if\|else\|elseif\|do\|for\|while\|with\)\>' 56 const RE_INCR_INDENT_2: string = 57 '^\s*\%(\%(private\|public\|friend\)\s\+\)\=\%(static\s\+\)\=\%(function\|sub\|property\)\>' 58 const RE_INCR_INDENT_3: string = 59 '^\s*\%(\%(private\|public\)\s\+\)\=\%(enum\|type\)\>' 60 61 def VbGetIndent(): number 62 var this_lnum: number = v:lnum 63 var this_line: string = getline(this_lnum) 64 var this_indent: number = 0 65 66 # labels and preprocessor statements get zero indent immediately 67 if (this_line =~? RE_LABEL) || (this_line =~? RE_PREPROC) 68 return this_indent 69 endif 70 71 # Get the current value of 'shiftwidth' 72 const SHIFTWIDTH: number = shiftwidth() 73 74 # Find a non-blank line above the current line. 75 # Skip over labels and preprocessor directives. 76 var lnum: number = this_lnum 77 var previous_line: string 78 while lnum > 0 79 lnum = prevnonblank(lnum - 1) 80 previous_line = getline(lnum) 81 if (previous_line !~? RE_LABEL) || (previous_line !~? RE_PREPROC) 82 break 83 endif 84 endwhile 85 86 # Hit the start of the file, use zero indent. 87 if lnum == 0 88 return this_indent 89 endif 90 91 # Variable "previous_line" now contains the text in buffer line "lnum". 92 93 # Multi-line statements have the underscore character at end-of-line: 94 # 95 # object.method(arguments, _ 96 # arguments, _ 97 # arguments) 98 # 99 # and require extra logic to determine the correct indentation. 100 # 101 # Case 1: Line "lnum" is the first line of a multiline statement. 102 # Line "lnum" will have a trailing underscore character 103 # but the preceding non-blank line does not. 104 # Line "this_lnum" will be indented relative to "lnum". 105 # 106 # Case 2: Line "lnum" is the last line of a multiline statement. 107 # Line "lnum" will not have a trailing underscore character 108 # but the preceding non-blank line will. 109 # Line "this_lnum" will have the same indentation as the starting 110 # line of the multiline statement. 111 # 112 # Case 3: Line "lnum" is neither the first nor last line. 113 # Lines "lnum" and "lnum-1" will have a trailing underscore 114 # character. 115 # Line "this_lnum" will have the same indentation as the preceding 116 # line. 117 # 118 # No matter which case it is, the starting line of the statement must be 119 # found. It will be assumed that multiline statements cannot have 120 # intermingled comments, statement labels, preprocessor directives or 121 # blank lines. 122 # 123 var lnum_is_continued: bool = (previous_line =~? RE_LINE_CONTINUATION) 124 var before_lnum: number 125 var before_previous_line: string 126 if lnum > 1 127 before_lnum = prevnonblank(lnum - 1) 128 before_previous_line = getline(before_lnum) 129 else 130 before_lnum = 0 131 before_previous_line = "" 132 endif 133 134 if before_previous_line !~? RE_LINE_CONTINUATION 135 # Variable "previous_line" contains the start of a statement. 136 # 137 this_indent = indent(lnum) 138 if lnum_is_continued 139 this_indent += SHIFTWIDTH 140 endif 141 elseif ! lnum_is_continued 142 # Line "lnum" contains the last line of a multiline statement. 143 # Need to find where this multiline statement begins 144 # 145 while before_lnum > 0 146 before_lnum -= 1 147 if getline(before_lnum) !~? RE_LINE_CONTINUATION 148 before_lnum += 1 149 break 150 endif 151 endwhile 152 if before_lnum == 0 153 before_lnum = 1 154 endif 155 previous_line = getline(before_lnum) 156 this_indent = indent(before_lnum) 157 else 158 # Line "lnum" is not the first or last line of a multiline statement. 159 # 160 this_indent = indent(lnum) 161 endif 162 163 # Increment indent 164 if (previous_line =~? RE_INCR_INDENT_1) || 165 (previous_line =~? RE_INCR_INDENT_2) || 166 (previous_line =~? RE_INCR_INDENT_3) 167 this_indent += SHIFTWIDTH 168 endif 169 170 # Decrement indent 171 if this_line =~? '^\s*end\s\+select\>' 172 if previous_line !~? '^\s*select\>' 173 this_indent -= 2 * SHIFTWIDTH 174 else 175 # this case is for an empty 'select' -- 'end select' 176 # (w/o any case statements) like: 177 # 178 # select case readwrite 179 # end select 180 this_indent -= SHIFTWIDTH 181 endif 182 elseif this_line =~? '^\s*\%(end\|else\|elseif\|until\|loop\|next\|wend\)\>' 183 this_indent -= SHIFTWIDTH 184 elseif this_line =~? '^\s*\%(case\|default\)\>' 185 if previous_line !~? '^\s*select\>' 186 this_indent -= SHIFTWIDTH 187 endif 188 endif 189 190 return this_indent 191 enddef 192 193 # vim:sw=4