Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 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) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | |
| 18 | if exists("b:did_indent") |
| 19 | finish |
| 20 | endif |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 21 | b:did_indent = v:true |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 23 | setlocal autoindent |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 24 | setlocal indentexpr=VbGetIndent() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | setlocal indentkeys& |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 26 | setlocal indentkeys+==~else,=~elseif,=~end,=~wend,=~case,=~next,=~select,=~loop |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 28 | b:undo_indent = "setlocal autoindent< indentexpr< indentkeys<" |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 29 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 30 | # Only define the function once. |
| 31 | if exists("*VbGetIndent") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 32 | finish |
| 33 | endif |
| 34 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 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\)\>' |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 41 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | endif |
| 70 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 78 | while lnum > 0 |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 79 | lnum = prevnonblank(lnum - 1) |
| 80 | previous_line = getline(lnum) |
| 81 | if (previous_line !~? RE_LABEL) || (previous_line !~? RE_PREPROC) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 82 | break |
| 83 | endif |
| 84 | endwhile |
| 85 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 86 | # Hit the start of the file, use zero indent. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | if lnum == 0 |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 88 | return this_indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 89 | endif |
| 90 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 91 | # Variable "previous_line" now contains the text in buffer line "lnum". |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 92 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 126 | if lnum > 1 |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 127 | before_lnum = prevnonblank(lnum - 1) |
| 128 | before_previous_line = getline(before_lnum) |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 129 | else |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 130 | before_lnum = 0 |
| 131 | before_previous_line = "" |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 132 | endif |
| 133 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 134 | if before_previous_line !~? RE_LINE_CONTINUATION |
| 135 | # Variable "previous_line" contains the start of a statement. |
| 136 | # |
| 137 | this_indent = indent(lnum) |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 138 | if lnum_is_continued |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 139 | this_indent += SHIFTWIDTH |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 140 | endif |
| 141 | elseif ! lnum_is_continued |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 142 | # Line "lnum" contains the last line of a multiline statement. |
| 143 | # Need to find where this multiline statement begins |
| 144 | # |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 145 | while before_lnum > 0 |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 146 | before_lnum -= 1 |
| 147 | if getline(before_lnum) !~? RE_LINE_CONTINUATION |
| 148 | before_lnum += 1 |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 149 | break |
| 150 | endif |
| 151 | endwhile |
| 152 | if before_lnum == 0 |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 153 | before_lnum = 1 |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 154 | endif |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 155 | previous_line = getline(before_lnum) |
| 156 | this_indent = indent(before_lnum) |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 157 | else |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 158 | # Line "lnum" is not the first or last line of a multiline statement. |
| 159 | # |
| 160 | this_indent = indent(lnum) |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 161 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 162 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 168 | endif |
| 169 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 170 | # Decrement indent |
| 171 | if this_line =~? '^\s*end\s\+select\>' |
| 172 | if previous_line !~? '^\s*select\>' |
| 173 | this_indent -= 2 * SHIFTWIDTH |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 174 | else |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 181 | endif |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | endif |
| 188 | endif |
| 189 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 190 | return this_indent |
| 191 | enddef |
Bram Moolenaar | 59c0395 | 2010-07-28 12:52:27 +0200 | [diff] [blame] | 192 | |
Michael Soyka | af2254d | 2024-05-20 14:37:50 +0200 | [diff] [blame] | 193 | # vim:sw=4 |