From 2640cf5300303b950d4c928aa59979bd73f474fe Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 11 Feb 2020 15:22:54 +0100 Subject: [PATCH 1/3] Add a parameter controlling space after comma Having a space after commas is a rather common style so make it configurable. Note a spurious [] in the README has been removed. --- README.md | 2 +- src/DTW.PS.Beautifier.Main.psm1 | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 489df1c..9cef9b1 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ SYNOPSIS SYNTAX Edit-DTWBeautifyScript [-SourcePath] [[-DestinationPath] ] [[-IndentType] ] - [-StandardOutput] [[-NewLine] ] [] + [-SpaceAfterComma] [-StandardOutput] [[-NewLine] ] [] ...more text... diff --git a/src/DTW.PS.Beautifier.Main.psm1 b/src/DTW.PS.Beautifier.Main.psm1 index 68a3355..9834074 100644 --- a/src/DTW.PS.Beautifier.Main.psm1 +++ b/src/DTW.PS.Beautifier.Main.psm1 @@ -53,6 +53,9 @@ function Initialize-ProcessVariables { # indent text, value is overridden with param [string]$script:IndentText = '' + # space after comma, value is overridden with param + [bool]$script:SpaceAfterComma = $false + # ouput clean script to standard output instead of source or destination path [bool]$script:StandardOutput = $false @@ -1058,8 +1061,12 @@ function Test-AddSpaceFollowingToken { if ((($TokenIndex + 1) -lt $SourceTokens.Count) -and $SourceTokens[$TokenIndex].Type -eq 'Variable' -and $SourceTokens[$TokenIndex + 1].Type -eq 'Operator' -and $SourceTokens[$TokenIndex + 1].Content -eq '[') { return $false } #endregion - #region Don't add space after Operators: , ! - if ($SourceTokens[$TokenIndex].Type -eq 'Operator' -and ($SourceTokens[$TokenIndex].Content -eq ',' -or $SourceTokens[$TokenIndex].Content -eq '!')) { return $false } + #region Space after Operators: , + if ($SourceTokens[$TokenIndex].Type -eq 'Operator' -and ($SourceTokens[$TokenIndex].Content -eq ',')) { return $script:SpaceAfterComma } + #endregion + + #region Don't add space after Operators: ! + if ($SourceTokens[$TokenIndex].Type -eq 'Operator' -and ($SourceTokens[$TokenIndex].Content -eq '!')) { return $false } #endregion #region Don't add space if next Operator token is: , ++ ; (except if it's after return keyword) @@ -1154,6 +1161,8 @@ Path to write reformatted PowerShell. If not specified rewrites file in place. .PARAMETER IndentType Type of indent to use: TwoSpaces, FourSpaces or Tabs +.PARAMETER SpaceAfterComma +Whether to add a space after a comma (,). Default = $false. .PARAMETER StandardOutput If specified, cleaned script is only written to stdout, not any file, and any errors will be written to stderror using concise format (not Write-Error). @@ -1191,6 +1200,8 @@ function Edit-DTWBeautifyScript { [Parameter(Mandatory = $false,ValueFromPipeline = $false)] [ValidateSet("TwoSpaces","FourSpaces","Tabs")] [string]$IndentType = "TwoSpaces", + [switch] + $SpaceAfterComma, [Alias('StdOut')] [switch]$StandardOutput, [Parameter(Mandatory = $false,ValueFromPipeline = $false)] @@ -1241,6 +1252,9 @@ function Edit-DTWBeautifyScript { $script:IndentText = $IndentText #endregion + #region Other parameters + $script:SpaceAfterComma = $SpaceAfterComma + #region Set script-level variable StandardOutput $script:StandardOutput = $StandardOutput #endregion From 275d42c0f7447dd7f8a3e0b5c1a74c2a3a38cc87 Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 11 Feb 2020 14:55:24 +0100 Subject: [PATCH 2/3] Add a parameter for overriding whitespace logic This makes it easy to tweak 'add space after' behavior. --- README.md | 2 +- src/DTW.PS.Beautifier.Main.psm1 | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9cef9b1..1f33252 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ SYNOPSIS SYNTAX Edit-DTWBeautifyScript [-SourcePath] [[-DestinationPath] ] [[-IndentType] ] - [-SpaceAfterComma] [-StandardOutput] [[-NewLine] ] + [-SpaceAfterComma] [[-AddSpaceAfter] ] [-StandardOutput] [[-NewLine] ] [] ...more text... diff --git a/src/DTW.PS.Beautifier.Main.psm1 b/src/DTW.PS.Beautifier.Main.psm1 index 9834074..9905c75 100644 --- a/src/DTW.PS.Beautifier.Main.psm1 +++ b/src/DTW.PS.Beautifier.Main.psm1 @@ -56,6 +56,9 @@ function Initialize-ProcessVariables { # space after comma, value is overridden with param [bool]$script:SpaceAfterComma = $false + # function for tweaking space behavior, overridden with param + [ScriptBlock]$script:AddSpaceAfter = $null + # ouput clean script to standard output instead of source or destination path [bool]$script:StandardOutput = $false @@ -1003,6 +1006,14 @@ function Test-AddSpaceFollowingToken { # all the way through rules, return true. To speed up this functioning, the rules that are # most likely to be useful are at the top. + # User-defined custom processing goes first + if ($script:AddSpaceAfter) { + $OverrideValue = $script:AddSpaceAfter.InvokeReturnAsIs($SourceTokens, $TokenIndex) + if ($OverrideValue -ne $null) { + return $OverrideValue + } + } + #region Don't write space after type NewLine if ($SourceTokens[$TokenIndex].Type -eq 'NewLine') { return $false } #endregion @@ -1163,6 +1174,13 @@ in place. Type of indent to use: TwoSpaces, FourSpaces or Tabs .PARAMETER SpaceAfterComma Whether to add a space after a comma (,). Default = $false. +.PARAMETER AddSpaceAfter +A ScriptBlock with custom code determining whether a space gets added after +a token. If specified, gets called for each token with the list of tokens +and current token index as arguments. It can either return $null in which +case the standard logic for adding spaces is used for the token, or it can +specify whether a space gets added or not by returning $true of $false and +the standard logic is then skipped. .PARAMETER StandardOutput If specified, cleaned script is only written to stdout, not any file, and any errors will be written to stderror using concise format (not Write-Error). @@ -1202,6 +1220,9 @@ function Edit-DTWBeautifyScript { [string]$IndentType = "TwoSpaces", [switch] $SpaceAfterComma, + [Parameter(Mandatory = $false,ValueFromPipeline = $false)] + [ScriptBlock] + $AddSpaceAfter = $null, [Alias('StdOut')] [switch]$StandardOutput, [Parameter(Mandatory = $false,ValueFromPipeline = $false)] @@ -1254,6 +1275,7 @@ function Edit-DTWBeautifyScript { #region Other parameters $script:SpaceAfterComma = $SpaceAfterComma + $script:AddSpaceAfter = $AddSpaceAfter #region Set script-level variable StandardOutput $script:StandardOutput = $StandardOutput From 0685971e9e51cf4d7667b71d49232e83a2769894 Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 11 Feb 2020 15:21:07 +0100 Subject: [PATCH 3/3] Add a parameter for more consistent group spacing behavior This makes the spacing in all groups (arrays, hashtables, script blocks, ...) the same which is more consistent overall: when removing spaces in arrays like @(1) it makes sense doing that also for e.g. @{a = 1}. Default is still $false for backwards compatibility. --- README.md | 2 +- src/DTW.PS.Beautifier.Main.psm1 | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1f33252..f48bf2c 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ SYNOPSIS SYNTAX Edit-DTWBeautifyScript [-SourcePath] [[-DestinationPath] ] [[-IndentType] ] - [-SpaceAfterComma] [[-AddSpaceAfter] ] [-StandardOutput] [[-NewLine] ] + [-SpaceAfterComma] [-TreatAllgroupsEqual] [[-AddSpaceAfter] ] [-StandardOutput] [[-NewLine] ] [] ...more text... diff --git a/src/DTW.PS.Beautifier.Main.psm1 b/src/DTW.PS.Beautifier.Main.psm1 index 9905c75..0faf26e 100644 --- a/src/DTW.PS.Beautifier.Main.psm1 +++ b/src/DTW.PS.Beautifier.Main.psm1 @@ -56,6 +56,9 @@ function Initialize-ProcessVariables { # space after comma, value is overridden with param [bool]$script:SpaceAfterComma = $false + # no spaces in any groups, value is overridden with param + [bool]$script:TreatAllGroupsEqual = $false + # function for tweaking space behavior, overridden with param [ScriptBlock]$script:AddSpaceAfter = $null @@ -1051,6 +1054,13 @@ function Test-AddSpaceFollowingToken { if ($SourceTokens[$TokenIndex].Type -eq 'Operator' -and $SourceTokens[$TokenIndex].Content -eq '::') { return $false } #endregion + #region Don't write space inside any groups + if ($script:TreatAllGroupsEqual) { + if ($SourceTokens[$TokenIndex].Type -eq 'GroupStart') { return $false } + if ((($TokenIndex + 1) -lt $SourceTokens.Count) -and $SourceTokens[$TokenIndex + 1].Type -eq 'GroupEnd') { return $false } + } + #endregion + #region Don't write space inside ( ) or $( ) groups if ($SourceTokens[$TokenIndex].Type -eq 'GroupStart' -and ($SourceTokens[$TokenIndex].Content -eq '(' -or $SourceTokens[$TokenIndex].Content -eq '$(')) { return $false } if ((($TokenIndex + 1) -lt $SourceTokens.Count) -and $SourceTokens[$TokenIndex + 1].Type -eq 'GroupEnd' -and $SourceTokens[$TokenIndex + 1].Content -eq ')') { return $false } @@ -1174,6 +1184,9 @@ in place. Type of indent to use: TwoSpaces, FourSpaces or Tabs .PARAMETER SpaceAfterComma Whether to add a space after a comma (,). Default = $false. +.PARAMETER TreatAllGroupsEqual +Whether to treat all groups (() {} @() @{}) the same, and not add spaces in between them. +Default = $false = only remove spaces in () and @() groups. .PARAMETER AddSpaceAfter A ScriptBlock with custom code determining whether a space gets added after a token. If specified, gets called for each token with the list of tokens @@ -1220,6 +1233,8 @@ function Edit-DTWBeautifyScript { [string]$IndentType = "TwoSpaces", [switch] $SpaceAfterComma, + [switch] + $TreatAllGroupsEqual, [Parameter(Mandatory = $false,ValueFromPipeline = $false)] [ScriptBlock] $AddSpaceAfter = $null, @@ -1275,6 +1290,7 @@ function Edit-DTWBeautifyScript { #region Other parameters $script:SpaceAfterComma = $SpaceAfterComma + $script:TreatAllGroupsEqual = $TreatAllGroupsEqual $script:AddSpaceAfter = $AddSpaceAfter #region Set script-level variable StandardOutput