Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ SYNOPSIS

SYNTAX
Edit-DTWBeautifyScript [-SourcePath] <String> [[-DestinationPath] <String>] [[-IndentType] <String>]
[-StandardOutput] [[-NewLine] <String>] [<CommonParameters>]
[-SpaceAfterComma] [-TreatAllgroupsEqual] [[-AddSpaceAfter] <ScriptBlock>] [-StandardOutput] [[-NewLine] <String>]
[<CommonParameters>]

...more text...
Expand Down
56 changes: 54 additions & 2 deletions src/DTW.PS.Beautifier.Main.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand All @@ -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)
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down