blob: a2a81853636f5934fb1ab3171cd1e4b71d19c7be [file] [log] [blame]
Michael Soykaaf2254d2024-05-20 14:37:50 +02001vim9script
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)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017
18if exists("b:did_indent")
19 finish
20endif
Michael Soykaaf2254d2024-05-20 14:37:50 +020021b:did_indent = v:true
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
Bram Moolenaar582fd852005-03-28 20:58:01 +000023setlocal autoindent
Michael Soykaaf2254d2024-05-20 14:37:50 +020024setlocal indentexpr=VbGetIndent()
Bram Moolenaar071d4272004-06-13 20:20:40 +000025setlocal indentkeys&
Bram Moolenaarf1dcd142022-12-31 15:30:45 +000026setlocal indentkeys+==~else,=~elseif,=~end,=~wend,=~case,=~next,=~select,=~loop
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
Michael Soykaaf2254d2024-05-20 14:37:50 +020028b:undo_indent = "setlocal autoindent< indentexpr< indentkeys<"
Bram Moolenaar582fd852005-03-28 20:58:01 +000029
Michael Soykaaf2254d2024-05-20 14:37:50 +020030# Only define the function once.
31if exists("*VbGetIndent")
Bram Moolenaar071d4272004-06-13 20:20:40 +000032 finish
33endif
34
Michael Soykaaf2254d2024-05-20 14:37:50 +020035# These regular expressions identify statement labels and preprocessor
36# directives.
37#
38const RE_LABEL: string = '^\s*\k\+:\s*$'
39const RE_PREPROC: string =
40 '^\s*#\%(const\|if\|elseif\|else\|end\|region\|enable\|disable\)\>'
Bram Moolenaarf1dcd142022-12-31 15:30:45 +000041
Michael Soykaaf2254d2024-05-20 14:37:50 +020042# 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#
49const 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#
54const RE_INCR_INDENT_1: string =
55 '^\s*\%(begin\|select\|case\|default\|if\|else\|elseif\|do\|for\|while\|with\)\>'
56const RE_INCR_INDENT_2: string =
57 '^\s*\%(\%(private\|public\|friend\)\s\+\)\=\%(static\s\+\)\=\%(function\|sub\|property\)\>'
58const RE_INCR_INDENT_3: string =
59 '^\s*\%(\%(private\|public\)\s\+\)\=\%(enum\|type\)\>'
60
61def 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000069 endif
70
Michael Soykaaf2254d2024-05-20 14:37:50 +020071 # 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 while lnum > 0
Michael Soykaaf2254d2024-05-20 14:37:50 +020079 lnum = prevnonblank(lnum - 1)
80 previous_line = getline(lnum)
81 if (previous_line !~? RE_LABEL) || (previous_line !~? RE_PREPROC)
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 break
83 endif
84 endwhile
85
Michael Soykaaf2254d2024-05-20 14:37:50 +020086 # Hit the start of the file, use zero indent.
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 if lnum == 0
Michael Soykaaf2254d2024-05-20 14:37:50 +020088 return this_indent
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 endif
90
Michael Soykaaf2254d2024-05-20 14:37:50 +020091 # Variable "previous_line" now contains the text in buffer line "lnum".
Bram Moolenaarf1dcd142022-12-31 15:30:45 +000092
Michael Soykaaf2254d2024-05-20 14:37:50 +020093 # 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
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000126 if lnum > 1
Michael Soykaaf2254d2024-05-20 14:37:50 +0200127 before_lnum = prevnonblank(lnum - 1)
128 before_previous_line = getline(before_lnum)
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000129 else
Michael Soykaaf2254d2024-05-20 14:37:50 +0200130 before_lnum = 0
131 before_previous_line = ""
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000132 endif
133
Michael Soykaaf2254d2024-05-20 14:37:50 +0200134 if before_previous_line !~? RE_LINE_CONTINUATION
135 # Variable "previous_line" contains the start of a statement.
136 #
137 this_indent = indent(lnum)
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000138 if lnum_is_continued
Michael Soykaaf2254d2024-05-20 14:37:50 +0200139 this_indent += SHIFTWIDTH
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000140 endif
141 elseif ! lnum_is_continued
Michael Soykaaf2254d2024-05-20 14:37:50 +0200142 # Line "lnum" contains the last line of a multiline statement.
143 # Need to find where this multiline statement begins
144 #
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000145 while before_lnum > 0
Michael Soykaaf2254d2024-05-20 14:37:50 +0200146 before_lnum -= 1
147 if getline(before_lnum) !~? RE_LINE_CONTINUATION
148 before_lnum += 1
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000149 break
150 endif
151 endwhile
152 if before_lnum == 0
Michael Soykaaf2254d2024-05-20 14:37:50 +0200153 before_lnum = 1
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000154 endif
Michael Soykaaf2254d2024-05-20 14:37:50 +0200155 previous_line = getline(before_lnum)
156 this_indent = indent(before_lnum)
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000157 else
Michael Soykaaf2254d2024-05-20 14:37:50 +0200158 # Line "lnum" is not the first or last line of a multiline statement.
159 #
160 this_indent = indent(lnum)
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000161 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
Michael Soykaaf2254d2024-05-20 14:37:50 +0200163 # 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 endif
169
Michael Soykaaf2254d2024-05-20 14:37:50 +0200170 # Decrement indent
171 if this_line =~? '^\s*end\s\+select\>'
172 if previous_line !~? '^\s*select\>'
173 this_indent -= 2 * SHIFTWIDTH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
Michael Soykaaf2254d2024-05-20 14:37:50 +0200175 # 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 endif
Michael Soykaaf2254d2024-05-20 14:37:50 +0200182 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 endif
188 endif
189
Michael Soykaaf2254d2024-05-20 14:37:50 +0200190 return this_indent
191enddef
Bram Moolenaar59c03952010-07-28 12:52:27 +0200192
Michael Soykaaf2254d2024-05-20 14:37:50 +0200193# vim:sw=4