Skip to content

Instantly share code, notes, and snippets.

@SteveL-MSFT
Last active September 2, 2021 02:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteveL-MSFT/f4e977caa3c44be2ce35f8012b151b4c to your computer and use it in GitHub Desktop.
Save SteveL-MSFT/f4e977caa3c44be2ce35f8012b151b4c to your computer and use it in GitHub Desktop.
Insert space after closing space in C# code
# 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