Skip to content

Commit c2f2ff1

Browse files
committed
Small improvements in the code formatter
1 parent 23acd24 commit c2f2ff1

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

src/vbaDeveloper.xlam/Build.bas

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Public vbaProjectToImport As VBProject
2828

2929
Public Sub testImport()
3030
Dim proj_name As String
31-
proj_name = "vbaDeveloper"
31+
proj_name = "VbaDeveloper"
3232

3333
Dim vbaProject As Object
3434
Set vbaProject = Application.VBE.VBProjects(proj_name)
@@ -38,7 +38,7 @@ End Sub
3838

3939
Public Sub testExport()
4040
Dim proj_name As String
41-
proj_name = "vbaDeveloper"
41+
proj_name = "VbaDeveloper"
4242

4343
Dim vbaProject As Object
4444
Set vbaProject = Application.VBE.VBProjects(proj_name)
@@ -218,7 +218,8 @@ Private Sub checkHowToImport(file As Object)
218218
fileName = file.name
219219
Dim componentName As String
220220
componentName = left(fileName, InStr(fileName, ".") - 1)
221-
If componentName = "Build" Then '"don't remove or import ourself
221+
If componentName = "Build" Then
222+
'"don't remove or import ourself
222223
Exit Sub
223224
End If
224225

@@ -356,3 +357,4 @@ Public Function addSheetToWorkbook(sheetName As String, workbookFilePath As Stri
356357
addSheetToWorkbook = ""
357358
End If
358359
End Function
360+

src/vbaDeveloper.xlam/ErrorHandling.bas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Attribute VB_Name = "ErrorHandling"
22
Option Explicit
33

44
Public Sub RaiseError(errNumber As Integer, Optional errSource As String = "", Optional errDescription As String = "")
5-
If errSource = "" Then 'set default values
5+
If errSource = "" Then
6+
'set default values
67
errSource = Err.Source
78
errDescription = Err.Description
89
End If

src/vbaDeveloper.xlam/EventListener.cls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Private Sub App_WorkbookAfterSave(ByVal wb As Workbook, ByVal success As Boolean
2424

2525
'Export all the modules for this work book if save was successful
2626
If success Then
27+
Formatter.formatProject wb.VBProject
2728
Build.exportVbaCode wb.VBProject
29+
NamedRanges.exportNamedRanges wb
2830
MsgBox "Finished saving workbook: " & wb.name & " . Code is exported."
2931
Else
3032
MsgBox "Saving workbook: " & wb.name & " was not successful. Code is not exported."
@@ -48,6 +50,7 @@ Private Sub App_WorkbookOpen(ByVal wb As Workbook)
4850
importNow = MsgBox("Import the code for " & wb.name & " now?", vbYesNo, "EventListener Workbook open event")
4951
If importNow = vbYes Then
5052
Build.importVbaCode wb.VBProject
53+
NamedRanges.importNamedRanges wb
5154
End If
5255

5356
On Error GoTo 0

src/vbaDeveloper.xlam/Formatter.bas

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Private Const BEG_FOR = "For "
3333
Private Const END_FOR = "Next "
3434
Private Const BEG_DOWHILE = "Do While "
3535
Private Const BEG_DOUNTIL = "Do Until "
36+
Private Const BEG_WHILE = "While "
37+
Private Const END_WHILE = "Wend"
3638

3739
Private Const BEG_TYPE = "Type "
3840
Private Const END_TYPE = "End Type"
@@ -41,11 +43,14 @@ Private Const BEG_PV_TYPE = "Private Type "
4143

4244
' Single words that need to be handled separately
4345
Private Const ONEWORD_END_FOR = "Next"
46+
Private Const ONEWORD_DO = "Do"
4447
Private Const ONEWORD_END_LOOP = "Loop"
4548
Private Const ONEWORD_ELSE = "Else"
4649
Private Const BEG_END_ELSEIF = "ElseIf"
4750
Private Const BEG_END_CASE = "Case "
4851

52+
Private Const THEN_KEYWORD = "Then"
53+
Private Const LINE_CONTINUATION = "_"
4954

5055
Private Const INDENT = " "
5156

@@ -93,7 +98,8 @@ Private Sub initializeWords()
9398

9499
w.Add BEG_IF, 1
95100
w.Add END_IF, -1
96-
w.Add BEG_SELECT, 2 'because any following 'Case' indents to the left
101+
'because any following 'Case' indents to the left we jump two
102+
w.Add BEG_SELECT, 2
97103
w.Add END_SELECT, -2
98104
w.Add BEG_WITH, 1
99105
w.Add END_WITH, -1
@@ -102,6 +108,8 @@ Private Sub initializeWords()
102108
w.Add END_FOR, -1
103109
w.Add BEG_DOWHILE, 1
104110
w.Add BEG_DOUNTIL, 1
111+
w.Add BEG_WHILE, 1
112+
w.Add END_WHILE, -1
105113

106114
w.Add BEG_TYPE, 1
107115
w.Add END_TYPE, -1
@@ -170,11 +178,16 @@ Public Sub formatCode(codePane As codeModule)
170178
If isEqual(ONEWORD_ELSE, line) _
171179
Or lineStartsWith(BEG_END_ELSEIF, line) _
172180
Or lineStartsWith(BEG_END_CASE, line) Then
181+
' Case, Else, ElseIf need to jump to the left
173182
levelChange = 1
174183
indentLevel = -1 + indentLevel
175184
ElseIf isLabel(line) Then
185+
' Labels don't have indentation
176186
levelChange = indentLevel
177187
indentLevel = 0
188+
' check for oneline If statemts
189+
ElseIf isOneLineIfStatemt(line) Then
190+
levelChange = 0
178191
Else
179192
levelChange = indentChange(line)
180193
End If
@@ -221,6 +234,10 @@ Private Function indentChange(ByVal line As String) As Integer
221234
indentChange = -1
222235
GoTo hell
223236
End If
237+
If isEqual(ONEWORD_DO, line) Then
238+
indentChange = 1
239+
GoTo hell
240+
End If
224241
Dim word As String
225242
Dim vord As Variant
226243
For Each vord In w.Keys
@@ -249,7 +266,23 @@ Private Function lineStartsWith(begin As String, strToCheck As String) As Boolea
249266
End Function
250267

251268

252-
Public Function isLabel(line As String) As Boolean
269+
' Returns True if strToCheck ends with ending, ignoring case
270+
Private Function lineEndsWith(ending As String, strToCheck As String) As Boolean
271+
lineEndsWith = False
272+
Dim length As Integer
273+
length = Len(ending)
274+
If Len(strToCheck) >= length Then
275+
lineEndsWith = isEqual(ending, right(strToCheck, length))
276+
End If
277+
End Function
278+
279+
280+
Private Function isLabel(line As String) As Boolean
253281
'it must end with a colon: and may not contain a space.
254282
isLabel = (right(line, 1) = ":") And (InStr(line, " ") < 1)
255283
End Function
284+
285+
286+
Private Function isOneLineIfStatemt(line As String) As Boolean
287+
isOneLineIfStatemt = (lineStartsWith(BEG_IF, line) And (Not lineEndsWith(THEN_KEYWORD, line)) And Not lineEndsWith(LINE_CONTINUATION, line))
288+
End Function

0 commit comments

Comments
 (0)