diff --git a/README.md b/README.md index 489df1c..f48bf2c 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ SYNOPSIS SYNTAX Edit-DTWBeautifyScript [-SourcePath] [[-DestinationPath] ] [[-IndentType] ] - [-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 68a3355..0faf26e 100644 --- a/src/DTW.PS.Beautifier.Main.psm1 +++ b/src/DTW.PS.Beautifier.Main.psm1 @@ -53,6 +53,15 @@ 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 + + # 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 + # ouput clean script to standard output instead of source or destination path [bool]$script:StandardOutput = $false @@ -1000,6 +1009,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 @@ -1037,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 } @@ -1058,8 +1082,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 +1182,18 @@ 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 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 +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). @@ -1191,6 +1231,13 @@ function Edit-DTWBeautifyScript { [Parameter(Mandatory = $false,ValueFromPipeline = $false)] [ValidateSet("TwoSpaces","FourSpaces","Tabs")] [string]$IndentType = "TwoSpaces", + [switch] + $SpaceAfterComma, + [switch] + $TreatAllGroupsEqual, + [Parameter(Mandatory = $false,ValueFromPipeline = $false)] + [ScriptBlock] + $AddSpaceAfter = $null, [Alias('StdOut')] [switch]$StandardOutput, [Parameter(Mandatory = $false,ValueFromPipeline = $false)] @@ -1241,6 +1288,11 @@ function Edit-DTWBeautifyScript { $script:IndentText = $IndentText #endregion + #region Other parameters + $script:SpaceAfterComma = $SpaceAfterComma + $script:TreatAllGroupsEqual = $TreatAllGroupsEqual + $script:AddSpaceAfter = $AddSpaceAfter + #region Set script-level variable StandardOutput $script:StandardOutput = $StandardOutput #endregion