From 1f63eb8bb71292e02434d99b2407171c65f4220b Mon Sep 17 00:00:00 2001 From: yarick Date: Sat, 1 Feb 2020 18:04:03 +0100 Subject: [PATCH] possibility to reduce standard code formatting to just trimming of the trailing spaces --- src/vbaDeveloper.xlam/Build.bas | 10 +++-- src/vbaDeveloper.xlam/Formatter.bas | 64 ++++++++++++++++------------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/vbaDeveloper.xlam/Build.bas b/src/vbaDeveloper.xlam/Build.bas index 268229e..c24d78d 100644 --- a/src/vbaDeveloper.xlam/Build.bas +++ b/src/vbaDeveloper.xlam/Build.bas @@ -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 ''' diff --git a/src/vbaDeveloper.xlam/Formatter.bas b/src/vbaDeveloper.xlam/Formatter.bas index f487c72..8fa9a1f 100644 --- a/src/vbaDeveloper.xlam/Formatter.bas +++ b/src/vbaDeveloper.xlam/Formatter.bas @@ -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 " @@ -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: