Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/vbaDeveloper.xlam/Build.bas
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ Attribute VB_Name = "Build"
' 7. In VB Editor, menu File-->Save Book1; Save as vbaDeveloper.xlam in the same directory as 'src'
' 8. Close excel. Open excel with a new workbook, then open the just saved vbaDeveloper.xlam
' 9. Let vbaDeveloper import its own code. Put the cursor in the function 'testImport' and press F5
' 10.Right click on 'vbaDeveloper', Import File for:
' 10. To reduce default devDeveloper code formatting to just trailing spaces trimming, go to the module Formatter and replace
' "SIMPLIFIED_FORMAT = False" with "SIMPLIFIED_FORMAT = True"
' 11.Right click on 'vbaDeveloper', Import File for:
' * CustomActions.cls
' * EventListener.cls
' * MyCustomActions.cls
' 11.Read the instructions in ThisWorkbook in vbaDeveloper Project and uncomment the code if you want automatic import and export enabled
' 12.If necessary rename module 'Build1' to Build. Menu File-->Save vbaDeveloper.xlam
' 13.Open the Excel workbook where you want to use vbaDeveloper and add vbaDeveloper.xlam as reference to load the Add-In with the workbook:
' 12.Read the instructions in ThisWorkbook in vbaDeveloper Project and uncomment the code if you want automatic import and export enabled
' 13.If necessary rename module 'Build1' to Build. Menu File-->Save vbaDeveloper.xlam
' 14.Open the Excel workbook where you want to use vbaDeveloper and add vbaDeveloper.xlam as reference to load the Add-In with the workbook:
' In VB Editor -> Tools -> References -> Browse and select vbaDeveloper.xlam
' Save the workbook, close it and reopen the workbook, now in the menu ribbon the ADD-INS tab is available with the VbaDeveloper menu
'''
Expand Down
64 changes: 35 additions & 29 deletions src/vbaDeveloper.xlam/Formatter.bas
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Attribute VB_Name = "Formatter"
Option Explicit

Private Const SIMPLIFIED_FORMAT = False
Private Const BEG_SUB = "Sub "
Private Const END_SUB = "End Sub"
Private Const BEG_PB_SUB = "Public Sub "
Expand Down Expand Up @@ -199,43 +200,48 @@ Public Sub formatCode(codePane As codeModule)
Dim lineCount As Integer
lineCount = codePane.CountOfLines

Dim indentLevel As Integer, nextLevel As Integer, levelChange As Integer, isPrevLineContinuated as Boolean
Dim indentLevel As Integer, nextLevel As Integer, levelChange As Integer, isPrevLineContinuated As Boolean
indentLevel = 0
isPrevLineContinuated = False
Dim lineNr As Integer
For lineNr = 1 To lineCount
Dim line As String
line = Trim(codePane.lines(lineNr, 1))
If Not line = "" Then
If isEqual(ONEWORD_ELSE, line) _
Or lineStartsWith(BEG_END_ELSEIF, line) _
Or lineStartsWith(BEG_END_CASE, line) Then
' Case, Else, ElseIf need to jump to the left
levelChange = 1
indentLevel = -1 + indentLevel
ElseIf isLabel(line) Then
' Labels don't have indentation
levelChange = indentLevel
indentLevel = 0
' check for oneline If statemts
ElseIf isOneLineIfStatemt(line) Then
levelChange = 0
Else
levelChange = indentChange(line)
End If

nextLevel = indentLevel + levelChange
If levelChange <= -1 Then
If SIMPLIFIED_FORMAT Then
line = RTrim(codePane.lines(lineNr, 1))
Call codePane.ReplaceLine(lineNr, line)
Else
line = Trim(codePane.lines(lineNr, 1))
If Not line = "" Then
If isEqual(ONEWORD_ELSE, line) _
Or lineStartsWith(BEG_END_ELSEIF, line) _
Or lineStartsWith(BEG_END_CASE, line) Then
' Case, Else, ElseIf need to jump to the left
levelChange = 1
indentLevel = -1 + indentLevel
ElseIf isLabel(line) Then
' Labels don't have indentation
levelChange = indentLevel
indentLevel = 0
' check for oneline If statemts
ElseIf isOneLineIfStatemt(line) Then
levelChange = 0
Else
levelChange = indentChange(line)
End If

nextLevel = indentLevel + levelChange
If levelChange <= -1 Then
indentLevel = nextLevel
End If

line = indentation(indentLevel) + line
indentLevel = nextLevel
End If

line = indentation(indentLevel) + line
indentLevel = nextLevel
If Not isPrevLineContinuated Then
Call codePane.ReplaceLine(lineNr, line)
EndIf
isPrevLineContinuated = isLineContinuated(line)
End If
If Not isPrevLineContinuated Then
Call codePane.ReplaceLine(lineNr, line)
EndIf
isPrevLineContinuated = isLineContinuated(line)
Next
Exit Sub
formatCodeError:
Expand Down