Skip to content

Commit c2e04e2

Browse files
authored
Merge pull request #9 from AccessCodeLib/fix-hrschupp
Code fixes - provided by hrschupp fixed: issue #2, #3, #4 + pull request #8 (line labels) Many thanks to @hrschupp
2 parents 1b2f48a + 1dcf6b3 commit c2e04e2

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

Tests/ACLibDeclarationDictCore/DeclarationDictTestCodemodule.cls

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ End Sub
104104

105105
Private Static Sub MyStaticSub(Optional ByVal Reset As Boolean = False)
106106
Static Counter2 As Integer
107+
End Sub
108+
109+
Private Sub TestPullrequest8_LineLabels()
110+
111+
On Error GoTo ErrHandler
112+
113+
114+
ExitHere:
115+
Exit Sub
116+
117+
ErrHandler:
118+
119+
120+
107121
End Sub
108122

109123
'---------------------------

Tests/ACLibDeclarationDictCore/DeclarationDictTests.cls

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ Public Sub ImportCode_InsertCodeLine_CheckKeys(ByVal Code As String, ByVal Expec
6363

6464
End Sub
6565

66+
'AccUnit:Row("Dim Abc(1) As String", "Abc")
67+
'AccUnit:Row("Dim Abc(1 To 2) As String", "Abc")
68+
'AccUnit: Row("Dim Abc(1, 2) As String", "Abc")
69+
'AccUnit: Row("Dim Abc(1, 2, 3) As String", "Abc")
70+
'AccUnit:Row("ReDim Abc(x, y) As String", "Abc")
71+
'AccUnit:Row("ReDim Abc(x, y, z) As String", "Abc")
72+
Public Sub ImportCode_Issue4_MultiDimArrayDeclaration(ByVal Code As String, ByVal Expected As String)
73+
74+
Dim Actual As String
75+
76+
m_DeclDict.ImportCode Code
77+
Actual = Join(m_DeclDict.WordsDict.Keys, "|")
78+
79+
Assert.That Actual, Iz.EqualTo(Expected)
80+
81+
End Sub
82+
83+
6684
'AccUnit:Row("Private Function Func1() As String" & Environment.NewLine & Environment.NewLine & " Dim X as String, Y As Long", "Func1|X|Y")
6785
'AccUnit:Row(Environment.NewLine & " Private abc " & Environment.NewLine & " Public X as String, Y As Long", "abc|X|Y")
6886
'AccUnit:Row("Private Function Func1() As String ' _" & Environment.NewLine & " Dim X as String, Y As Long", "Func1")
@@ -78,6 +96,31 @@ Public Sub ImportCode_InsertCodeLines_CheckKeys(ByVal Code As String, ByVal Expe
7896

7997
End Sub
8098

99+
'AccUnit:Row("Dim SomeFuncVar As Integer ' the next line should not be ignored" & Environment.NewLine & "Dim AnotherFuncVar As String", "SomeFuncVar|AnotherFuncVar")
100+
Public Sub ImportCode_Issue3_RemovingCommentCorruptsLineEndings(ByVal Code As String, ByVal Expected As String)
101+
102+
Dim Actual As String
103+
104+
m_DeclDict.ImportCode Code
105+
Actual = Join(m_DeclDict.WordsDict.Keys, "|")
106+
107+
Assert.That Actual, Iz.EqualTo(Expected)
108+
109+
End Sub
110+
111+
'AccUnit:Row(Environment.NewLine & "ExitHere:" & Environment.NewLine & "Dim Abc As String", "ExitHere|Abc")
112+
'AccUnit:Row(Environment.NewLine & "ExitHere: Dim Abc As String", "ExitHere|Abc")
113+
Public Sub ImportCode_Pullrequest8_LineLabel(ByVal Code As String, ByVal Expected As String)
114+
115+
Dim Actual As String
116+
117+
m_DeclDict.ImportCode Code
118+
Actual = Join(m_DeclDict.WordsDict.Keys, "|")
119+
120+
Assert.That Actual, Iz.EqualTo(Expected)
121+
122+
End Sub
123+
81124
'AccUnit:Row("Private Declare PtrSafe Sub Sleep Lib ""kernel32"" (ByVal dwMilliseconds As LongPtr)", "Sleep|dwMilliseconds")
82125
'AccUnit:Row("Private Declare PtrSafe Function CopyMemory Lib ""kernel32"" Alias ""RtlMoveMemory""(ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) As Long", "CopyMemory|Destination|Source|Length")
83126
Public Sub ImportCode_API_CheckKeys(ByVal Code As String, ByVal Expected As String)
@@ -164,7 +207,8 @@ Public Sub ImportClassCodeModule_CheckKeysExists()
164207
"PropertySet", "ObjRef", _
165208
"TestMe", _
166209
"VariableParams", "Args", _
167-
"MyStaticSub", "Reset", "Counter2")
210+
"MyStaticSub", "Reset", "Counter2", _
211+
"TestPullrequest8_LineLabels", "ExitHere", "ErrHandler")
168212

169213

170214
m_DeclDict.ImportVBComponent CurrentVbProject.VBComponents("DeclarationDictTestCodemodule")

source/modules/DeclarationDict.cls

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ Public Sub ImportVBComponent(ByVal VBComponent2Import As VBComponent)
162162
End Sub
163163

164164
Public Sub ImportCodeModule(ByVal CodeModule2Import As CodeModule)
165-
ImportCode CodeModule2Import.Lines(1, CodeModule2Import.CountOfLines)
165+
If CodeModule2Import.CountOfLines > 0 Then
166+
ImportCode CodeModule2Import.Lines(1, CodeModule2Import.CountOfLines)
167+
End If
166168
End Sub
167169

168170
Public Sub ImportCode(ByVal Code As String)
@@ -217,13 +219,17 @@ Private Function PrepareCode(ByVal Code As String, ByVal RegEx As RegExp) As Str
217219

218220
' remove comments
219221
'.Pattern = "'(.*)[\r\n]"
220-
.Pattern = "'(.*)(?:[\r\n]|$)"
221-
Code = .Replace(Code, "")
222+
.Pattern = "'(.*)([\r\n]|$)"
223+
Code = .Replace(Code, "$2")
222224

223225
#If DebugPrintEnabled Then
224226
DebugPrint Code, True, "PrepareCode- after remove comments"
225227
#End If
226228

229+
' treat line labels as dim (but not line numbers)
230+
.Pattern = "([\r\n]|^)([^0-9\r\n]\S*):(\s|[\r\n]|$)"
231+
Code = .Replace(Code, "$1Dim $2:$3")
232+
227233
' dim a as String: a = 5 => insert line break
228234
.Pattern = "(\:\s)"
229235
Code = .Replace(Code, vbNewLine)
@@ -305,6 +311,15 @@ Private Sub AddWordFromDeclaration(ByRef Declarations As String, ByVal IsProcedu
305311
Declarations = Replace(Declarations, " ", " ")
306312
Loop
307313

314+
If Not IsProcedure And Not IsEnumTypeBlock Then
315+
Do While Declarations Like "*(*,*)*"
316+
' prevent multi-dimensional Dim from transforming into new declarations (might be numeric)
317+
Pos = InStr(1, Declarations, "(")
318+
PosX = InStr(Pos, Declarations, ")")
319+
Declarations = Left(Declarations, Pos - 1) & " " & Mid(Declarations, PosX + 1)
320+
Loop
321+
End If
322+
308323
DeclArray = Split(Trim(Declarations), ",")
309324

310325
For i = LBound(DeclArray) To UBound(DeclArray)

0 commit comments

Comments
 (0)