Last active
September 2, 2021 02:41
-
-
Save SteveL-MSFT/f4e977caa3c44be2ce35f8012b151b4c to your computer and use it in GitHub Desktop.
Insert space after closing space in C# code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage example for one commit per directory: | |
# dir -Recurse -Path . -Directory | % { $dir = $_; dir -path $dir.fullname *.cs | % { ~/test/update-code.ps1 -path $_.fullname; git add $_.fullname }; git commit -m "Update $($dir.name)" } | |
param($path) | |
$src = Get-Content $path -Raw | |
$skip = " get; set; " | |
# this matches a closing brace that is not followed by: | |
# catch, else, finally, another closing brace, newline, space, hash, slash (comment), * (comment) | |
# case, default, while, double quote, closing parens | |
$pattern = "}\n[ ]+(?!catch|else|finally|case|default|while|[}\n #/\*`"\{}\)])" | |
$result = $src | select-string -pattern $pattern -AllMatches | |
for ($i = $result.Matches.Count - 1; $i -ge 0; $i--) { | |
$m = $result.matches[$i] | |
if ($src.Substring($m.Index - $skip.Length, $skip.Length) -ne $skip) { | |
$src = $src.Insert($m.Index + 1, [System.Environment]::NewLine) | |
} | |
} | |
Set-Content -Path $path -Value $src -NoNewline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment