runtime(vb): update vb indent plugin as vim9script

Include an updated vb indent script using vim9script.
Also update the runtime indent test files

Signed-off-by: Michael Soyka <mssr953@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Michael Soyka
2024-05-20 14:37:50 +02:00
committed by Christian Brabandt
parent 155583a5c3
commit af2254d2f3
3 changed files with 241 additions and 119 deletions

View File

@ -1,6 +1,16 @@
' vim: filetype=vb shiftwidth=4 expandtab ' vim: filetype=vb shiftwidth=4 expandtab
' '
' START_INDENT ' START_INDENT
#Const Debug = False
#If Win64 Then
' Win64=true, Win32=true, Win16=false
#ElseIf Win32 Then
' Win32=true, Win16=false
#Else
' Win16=true
#End If
Public Type GEmployeeRecord ' Create user-defined type. Public Type GEmployeeRecord ' Create user-defined type.
ID As Integer ' Define elements of data type. ID As Integer ' Define elements of data type.
Name As String * 20 Name As String * 20
@ -95,6 +105,9 @@ Public Sub TestMultiline (cellAddr As String, rowNbr As Long)
Dim rng As Range Dim rng As Range
Set rng = Range(cellAddr) Set rng = Range(cellAddr)
' Line continuation is implemented as a two-character sequence-
' whitespace followed by underscore.
With rng With rng
.Cells(1,1).Value = _ .Cells(1,1).Value = _
"Line 1 of multiline string; " & _ "Line 1 of multiline string; " & _
@ -102,14 +115,25 @@ With rng
"Line 3 of multiline string" "Line 3 of multiline string"
End With End With
' The following lines have whitespace after the underscore character ' This code block omits the leading whitespace character and so
' and therefore do not form a valid multiline statement. The indent ' the trailing underscore will not be treated as line continuation.
' script correctly treats them as four single line statements contrary With rng
' to the author's obvious indent. .Cells(1,1).Value =_
rng..Cells(1,1).Value = _ "Line 1 of multiline string; " &_
"Line 2 of multiline string; " &_
"Line 3 of multiline string"
End With
' The following lines have whitespace after the underscore character.
' This is contrary to Microsoft documentation but it is reported that
' some Microsoft editors allow it and will still treat the statement
' as line-continued.
With rng
rng.Cells(1,1).Value = _
"Line 1 of multiline string; " & _ "Line 1 of multiline string; " & _
"Line 2 of multiline string; " & _ "Line 2 of multiline string; " & _
"Line 3 of multiline string" "Line 3 of multiline string"
End With
End Sub End Sub
@ -121,6 +145,18 @@ stmtLabel:
End Sub End Sub
Public Static Function TestStatic(addend As Integer)
Dim Integer accumulator
accumulator = accumulator + addend
TestStatic = accumulator
End Function
Friend Function TestFriend(addend As Integer)
Static Integer accumulator
accumulator = accumulator + addend
TestFriend = accumulator
End Function
Sub TestTypeKeyword() Sub TestTypeKeyword()
Type EmployeeRecord ' Create user-defined type. Type EmployeeRecord ' Create user-defined type.
ID As Integer ' Define elements of data type. ID As Integer ' Define elements of data type.
@ -131,4 +167,10 @@ HireDate As Date
End Type End Type
Dim varType As EmployeeRecord Dim varType As EmployeeRecord
End Sub End Sub
Sub TestDateLiteralAfterLineContinuation
Dim birthday as Date
birthday = _
#January 1, 1901#
End Sub
' END_INDENT ' END_INDENT

View File

@ -1,6 +1,16 @@
' vim: filetype=vb shiftwidth=4 expandtab ' vim: filetype=vb shiftwidth=4 expandtab
' '
' START_INDENT ' START_INDENT
#Const Debug = False
#If Win64 Then
' Win64=true, Win32=true, Win16=false
#ElseIf Win32 Then
' Win32=true, Win16=false
#Else
' Win16=true
#End If
Public Type GEmployeeRecord ' Create user-defined type. Public Type GEmployeeRecord ' Create user-defined type.
ID As Integer ' Define elements of data type. ID As Integer ' Define elements of data type.
Name As String * 20 Name As String * 20
@ -95,6 +105,9 @@ Public Sub TestMultiline (cellAddr As String, rowNbr As Long)
Dim rng As Range Dim rng As Range
Set rng = Range(cellAddr) Set rng = Range(cellAddr)
' Line continuation is implemented as a two-character sequence-
' whitespace followed by underscore.
With rng With rng
.Cells(1,1).Value = _ .Cells(1,1).Value = _
"Line 1 of multiline string; " & _ "Line 1 of multiline string; " & _
@ -102,14 +115,25 @@ Public Sub TestMultiline (cellAddr As String, rowNbr As Long)
"Line 3 of multiline string" "Line 3 of multiline string"
End With End With
' The following lines have whitespace after the underscore character ' This code block omits the leading whitespace character and so
' and therefore do not form a valid multiline statement. The indent ' the trailing underscore will not be treated as line continuation.
' script correctly treats them as four single line statements contrary With rng
' to the author's obvious indent. .Cells(1,1).Value =_
rng..Cells(1,1).Value = _ "Line 1 of multiline string; " &_
"Line 1 of multiline string; " & _ "Line 2 of multiline string; " &_
"Line 2 of multiline string; " & _ "Line 3 of multiline string"
"Line 3 of multiline string" End With
' The following lines have whitespace after the underscore character.
' This is contrary to Microsoft documentation but it is reported that
' some Microsoft editors allow it and will still treat the statement
' as line-continued.
With rng
rng.Cells(1,1).Value = _
"Line 1 of multiline string; " & _
"Line 2 of multiline string; " & _
"Line 3 of multiline string"
End With
End Sub End Sub
@ -121,6 +145,18 @@ stmtLabel:
End Sub End Sub
Public Static Function TestStatic(addend As Integer)
Dim Integer accumulator
accumulator = accumulator + addend
TestStatic = accumulator
End Function
Friend Function TestFriend(addend As Integer)
Static Integer accumulator
accumulator = accumulator + addend
TestFriend = accumulator
End Function
Sub TestTypeKeyword() Sub TestTypeKeyword()
Type EmployeeRecord ' Create user-defined type. Type EmployeeRecord ' Create user-defined type.
ID As Integer ' Define elements of data type. ID As Integer ' Define elements of data type.
@ -131,4 +167,10 @@ Sub TestTypeKeyword()
End Type End Type
Dim varType As EmployeeRecord Dim varType As EmployeeRecord
End Sub End Sub
Sub TestDateLiteralAfterLineContinuation
Dim birthday as Date
birthday = _
#January 1, 1901#
End Sub
' END_INDENT ' END_INDENT

View File

@ -1,155 +1,193 @@
" Vim indent file vim9script
" Language: VisualBasic (ft=vb) / Basic (ft=basic) / SaxBasic (ft=vb)
" Author: Johannes Zellner <johannes@zellner.org> # Vim indent file
" Maintainer: Michael Soyka (mssr953@gmail.com) # Language: VisualBasic (ft=vb) / Basic (ft=basic) / SaxBasic (ft=vb)
" Last Change: Fri, 18 Jun 2004 07:22:42 CEST # Author: Johannes Zellner <johannes@zellner.org>
" Small update 2010 Jul 28 by Maxim Kim # Maintainer: Michael Soyka (mssr953@gmail.com)
" 2022/12/15: add support for multiline statements. # Contributors: Doug Kearns (dougkearns@gmail.com)
" 2022/12/21: move VbGetIndent from global to script-local scope # Last Change: Fri, 18 Jun 2004 07:22:42 CEST
" 2022/12/26: recognize "Type" keyword # Small update 2010 Jul 28 by Maxim Kim
# 2022/12/15: add support for multiline statements.
# 2022/12/21: move VbGetIndent from global to script-local scope
# 2022/12/26: recognize "Type" keyword
# 2023/07/13: correct/extend line continuation pattern (Doug Kearns)
# 2023/07/14: add more keywords; various optimizations (Doug Kearns)
# 2023/07/20: convert to Vim9 script
# 2023/07/23: improve detection of preproc directives (Doug Kearns)
if exists("b:did_indent") if exists("b:did_indent")
finish finish
endif endif
let b:did_indent = 1 b:did_indent = v:true
setlocal autoindent setlocal autoindent
setlocal indentexpr=s:VbGetIndent(v:lnum) setlocal indentexpr=VbGetIndent()
setlocal indentkeys& setlocal indentkeys&
setlocal indentkeys+==~else,=~elseif,=~end,=~wend,=~case,=~next,=~select,=~loop setlocal indentkeys+==~else,=~elseif,=~end,=~wend,=~case,=~next,=~select,=~loop
let b:undo_indent = "set ai< indentexpr< indentkeys<" b:undo_indent = "setlocal autoindent< indentexpr< indentkeys<"
" Only define the function once. # Only define the function once.
if exists("*s:VbGetIndent") if exists("*VbGetIndent")
finish finish
endif endif
function s:VbGetIndent(lnum) # These regular expressions identify statement labels and preprocessor
let this_lnum = a:lnum # directives.
let this_line = getline(this_lnum) #
const RE_LABEL: string = '^\s*\k\+:\s*$'
const RE_PREPROC: string =
'^\s*#\%(const\|if\|elseif\|else\|end\|region\|enable\|disable\)\>'
" labels and preprocessor get zero indent immediately # Microsoft documentation states that line continuation is indicated by a
let LABELS_OR_PREPROC = '^\s*\(\<\k\+\>:\s*$\|#.*\)' # two-character sequence at end-of-line: a space character followed by an
if this_line =~? LABELS_OR_PREPROC # underscore. Nonetheless, it has been reported that additional
return 0 # whitespace after the underscore is also allowed. We will support both.
# However, VB 16.0 also permits a comment after the underscore which,
# for simplicity, we do not support.
#
const RE_LINE_CONTINUATION: string = '\s_\s*$'
# The following regular expressions are used to increase the indent
# after statements that open a new scope.
#
const RE_INCR_INDENT_1: string =
'^\s*\%(begin\|select\|case\|default\|if\|else\|elseif\|do\|for\|while\|with\)\>'
const RE_INCR_INDENT_2: string =
'^\s*\%(\%(private\|public\|friend\)\s\+\)\=\%(static\s\+\)\=\%(function\|sub\|property\)\>'
const RE_INCR_INDENT_3: string =
'^\s*\%(\%(private\|public\)\s\+\)\=\%(enum\|type\)\>'
def VbGetIndent(): number
var this_lnum: number = v:lnum
var this_line: string = getline(this_lnum)
var this_indent: number = 0
# labels and preprocessor statements get zero indent immediately
if (this_line =~? RE_LABEL) || (this_line =~? RE_PREPROC)
return this_indent
endif endif
" Get the current value of "shiftwidth"
let bShiftwidth = shiftwidth()
" Find a non-blank line above the current line. # Get the current value of 'shiftwidth'
" Skip over labels and preprocessor directives. const SHIFTWIDTH: number = shiftwidth()
let lnum = this_lnum
# Find a non-blank line above the current line.
# Skip over labels and preprocessor directives.
var lnum: number = this_lnum
var previous_line: string
while lnum > 0 while lnum > 0
let lnum = prevnonblank(lnum - 1) lnum = prevnonblank(lnum - 1)
let previous_line = getline(lnum) previous_line = getline(lnum)
if previous_line !~? LABELS_OR_PREPROC if (previous_line !~? RE_LABEL) || (previous_line !~? RE_PREPROC)
break break
endif endif
endwhile endwhile
" Hit the start of the file, use zero indent. # Hit the start of the file, use zero indent.
if lnum == 0 if lnum == 0
return 0 return this_indent
endif endif
" Variable "previous_line" now contains the text in buffer line "lnum". # Variable "previous_line" now contains the text in buffer line "lnum".
" Multi-line statements have the underscore character at end-of-line: # Multi-line statements have the underscore character at end-of-line:
" #
" object.method(arguments, _ # object.method(arguments, _
" arguments, _ # arguments, _
" arguments) # arguments)
" #
" and require extra logic to determine the correct indentation. # and require extra logic to determine the correct indentation.
" #
" Case 1: Line "lnum" is the first line of a multiline statement. # Case 1: Line "lnum" is the first line of a multiline statement.
" Line "lnum" will have a trailing underscore character # Line "lnum" will have a trailing underscore character
" but the preceding non-blank line does not. # but the preceding non-blank line does not.
" Line "this_lnum" will be indented relative to "lnum". # Line "this_lnum" will be indented relative to "lnum".
" #
" Case 2: Line "lnum" is the last line of a multiline statement. # Case 2: Line "lnum" is the last line of a multiline statement.
" Line "lnum" will not have a trailing underscore character # Line "lnum" will not have a trailing underscore character
" but the preceding non-blank line will. # but the preceding non-blank line will.
" Line "this_lnum" will have the same indentation as the starting # Line "this_lnum" will have the same indentation as the starting
" line of the multiline statement. # line of the multiline statement.
" #
" Case 3: Line "lnum" is neither the first nor last line. # Case 3: Line "lnum" is neither the first nor last line.
" Lines "lnum" and "lnum-1" will have a trailing underscore # Lines "lnum" and "lnum-1" will have a trailing underscore
" character. # character.
" Line "this_lnum" will have the same indentation as the preceding # Line "this_lnum" will have the same indentation as the preceding
" line. # line.
" #
" No matter which case it is, the starting line of the statement must be # No matter which case it is, the starting line of the statement must be
" found. It will be assumed that multiline statements cannot have # found. It will be assumed that multiline statements cannot have
" intermingled comments, statement labels, preprocessor directives or # intermingled comments, statement labels, preprocessor directives or
" blank lines. # blank lines.
" #
let lnum_is_continued = (previous_line =~ '_$') var lnum_is_continued: bool = (previous_line =~? RE_LINE_CONTINUATION)
var before_lnum: number
var before_previous_line: string
if lnum > 1 if lnum > 1
let before_lnum = prevnonblank(lnum-1) before_lnum = prevnonblank(lnum - 1)
let before_previous_line = getline(before_lnum) before_previous_line = getline(before_lnum)
else else
let before_lnum = 0 before_lnum = 0
let before_previous_line = "" before_previous_line = ""
endif endif
if before_previous_line !~ '_$' if before_previous_line !~? RE_LINE_CONTINUATION
" Variable "previous_line" contains the start of a statement. # Variable "previous_line" contains the start of a statement.
" #
let ind = indent(lnum) this_indent = indent(lnum)
if lnum_is_continued if lnum_is_continued
let ind += bShiftwidth this_indent += SHIFTWIDTH
endif endif
elseif ! lnum_is_continued elseif ! lnum_is_continued
" Line "lnum" contains the last line of a multiline statement. # Line "lnum" contains the last line of a multiline statement.
" Need to find where this multiline statement begins # Need to find where this multiline statement begins
" #
while before_lnum > 0 while before_lnum > 0
let before_lnum -= 1 before_lnum -= 1
if getline(before_lnum) !~ '_$' if getline(before_lnum) !~? RE_LINE_CONTINUATION
let before_lnum += 1 before_lnum += 1
break break
endif endif
endwhile endwhile
if before_lnum == 0 if before_lnum == 0
let before_lnum = 1 before_lnum = 1
endif endif
let previous_line = getline(before_lnum) previous_line = getline(before_lnum)
let ind = indent(before_lnum) this_indent = indent(before_lnum)
else else
" Line "lnum" is not the first or last line of a multiline statement. # Line "lnum" is not the first or last line of a multiline statement.
" #
let ind = indent(lnum) this_indent = indent(lnum)
endif endif
" Add # Increment indent
if previous_line =~? '^\s*\<\(begin\|\%(\%(private\|public\|friend\)\s\+\)\=\%(function\|sub\|property\|enum\|type\)\|select\|case\|default\|if\|else\|elseif\|do\|for\|while\|with\)\>' if (previous_line =~? RE_INCR_INDENT_1) ||
let ind = ind + bShiftwidth (previous_line =~? RE_INCR_INDENT_2) ||
(previous_line =~? RE_INCR_INDENT_3)
this_indent += SHIFTWIDTH
endif endif
" Subtract # Decrement indent
if this_line =~? '^\s*\<end\>\s\+\<select\>' if this_line =~? '^\s*end\s\+select\>'
if previous_line !~? '^\s*\<select\>' if previous_line !~? '^\s*select\>'
let ind = ind - 2 * bShiftwidth this_indent -= 2 * SHIFTWIDTH
else else
" this case is for an empty 'select' -- 'end select' # this case is for an empty 'select' -- 'end select'
" (w/o any case statements) like: # (w/o any case statements) like:
" #
" select case readwrite # select case readwrite
" end select # end select
let ind = ind - bShiftwidth this_indent -= SHIFTWIDTH
endif endif
elseif this_line =~? '^\s*\<\(end\|else\|elseif\|until\|loop\|next\|wend\)\>' elseif this_line =~? '^\s*\%(end\|else\|elseif\|until\|loop\|next\|wend\)\>'
let ind = ind - bShiftwidth this_indent -= SHIFTWIDTH
elseif this_line =~? '^\s*\<\(case\|default\)\>' elseif this_line =~? '^\s*\%(case\|default\)\>'
if previous_line !~? '^\s*\<select\>' if previous_line !~? '^\s*select\>'
let ind = ind - bShiftwidth this_indent -= SHIFTWIDTH
endif endif
endif endif
return ind return this_indent
endfunction enddef
" vim:sw=4 # vim:sw=4