diff --git a/src/TcBlack/VariableDeclaration.cs b/src/TcBlack/VariableDeclaration.cs index 960f204..de1f777 100644 --- a/src/TcBlack/VariableDeclaration.cs +++ b/src/TcBlack/VariableDeclaration.cs @@ -94,11 +94,9 @@ public TcDeclaration Tokenize() public string RemoveWhiteSpaceIfPossible(string str) { - // https://stackoverflow.com/a/49386152/6329629 - string spacesRemoved = Regex.Replace(str, @"(?!\b\s+\b)\s+", ""); - string spaceAfterArray = spacesRemoved.Replace("]OF", "] OF"); + string pattern = @"\s+(?=[^[\]]*\])|\s+(?=[^()]*\))"; - return spaceAfterArray; + return Regex.Replace(str, pattern, "").Trim(); } } diff --git a/src/TcBlackTests/VariableDeclarationTests.cs b/src/TcBlackTests/VariableDeclarationTests.cs index 051f010..19456e4 100644 --- a/src/TcBlackTests/VariableDeclarationTests.cs +++ b/src/TcBlackTests/VariableDeclarationTests.cs @@ -89,6 +89,7 @@ public void VarAllocationAndTypeVariousWhitespaceArangements [InlineData("pid_controller ", "", "ST_Struct ", " (nVar1:=1, nVar2:=4)")] [InlineData("Light", "", "photons ", "2.4 ")] [InlineData("SomeWords ", "", "T_MaxString ", " 'Black quartz watch my vow.'")] + [InlineData(" Character ", "", " STRING(1)", "' '")] [InlineData( "aSample_3 ", "", "ARRAY[1..2, 2..3, 3..4] OF INT ", "[2(0),4(4),2,3]" )] @@ -123,7 +124,7 @@ public void VarAllocationTypeAndInitializationVariousWhitespaceArangements( TcDeclaration expectedDecl = new TcDeclaration( variable.Trim(), _allocation.Trim(), - varDecl.RemoveWhiteSpaceIfPossible(type.Trim()), + varDecl.RemoveWhiteSpaceIfPossible(type), varDecl.RemoveWhiteSpaceIfPossible(initialization), "" ); @@ -184,8 +185,10 @@ private void AssertEquals(TcDeclaration expected, TcDeclaration actual) "fbSample : FB_Sample(nId_Init:=11, fIn_Init:=33.44);" )] [InlineData( - "var1 : REAL := 8 ; // Comment", - "var1 : REAL := 8; // Comment" + "var1 : REAL := 8 ; // Comment", "var1 : REAL := 8; // Comment" + )] + [InlineData( + "character : STRING(1) := ' ' ; ", "character : STRING(1) := ' ';" )] public void FormatVariableDeclaration(string unformattedCode, string expected) {