|
| 1 | +function Edit-FunctionBody { |
| 2 | + param( |
| 3 | + [Parameter(Mandatory)][string]$Content, |
| 4 | + [Parameter(Mandatory)][string]$FunctionName, |
| 5 | + [Parameter(Mandatory)][ScriptBlock]$Converter |
| 6 | + ) |
| 7 | + |
| 8 | + $methodPattern = "$FunctionName\s*\([^)]*\)\s*(const\s*)?{" |
| 9 | + |
| 10 | + $match = [regex]::Match($Content, $methodPattern) |
| 11 | + if (-not $match.Success) { |
| 12 | + Write-Warning "Editing function '$FunctionName' is not found." |
| 13 | + return $Content |
| 14 | + } |
| 15 | + |
| 16 | + $startIndex = $match.Index + $match.Length - 1 |
| 17 | + $braceCount = 1 |
| 18 | + $i = $startIndex + 1 |
| 19 | + while ($i -lt $Content.Length -and $braceCount -gt 0) { |
| 20 | + if ($Content[$i] -eq '{') { $braceCount++ } |
| 21 | + elseif ($Content[$i] -eq '}') { $braceCount-- } |
| 22 | + $i++ |
| 23 | + } |
| 24 | + $endIndex = $i |
| 25 | + |
| 26 | + $originalBody = $Content.Substring($startIndex + 1, $endIndex - $startIndex - 2).Trim() |
| 27 | + $newBody = & $Converter $originalBody |
| 28 | + $newContent = $Content.Substring(0, $startIndex + 1) + "`n$newBody`n" + $Content.Substring($endIndex - 1) |
| 29 | + return $newContent |
| 30 | +} |
| 31 | + |
| 32 | +function Add-BeforeReturn { |
| 33 | + param( |
| 34 | + [Parameter(Mandatory)][string]$Body, |
| 35 | + [Parameter(Mandatory)][string]$Insert |
| 36 | + ) |
| 37 | + |
| 38 | + $lines = $Body -split "`r?`n" |
| 39 | + $newLines = @() |
| 40 | + |
| 41 | + foreach ($line in $lines) { |
| 42 | + if ($line -match '^\s*return\s+.+;$') { |
| 43 | + $newLines += $Insert |
| 44 | + } |
| 45 | + $newLines += $line |
| 46 | + } |
| 47 | + |
| 48 | + return ($newLines -join "`n") |
| 49 | +} |
| 50 | + |
| 51 | +function Add-LineBelow { |
| 52 | + [CmdletBinding()] |
| 53 | + param( |
| 54 | + [Parameter(Mandatory)][string]$Content, |
| 55 | + [Parameter(Mandatory)][string[]]$Patterns, |
| 56 | + [Parameter(Mandatory)][string]$Insert |
| 57 | + ) |
| 58 | + |
| 59 | + $eol = if ($Content -match "`r`n") { "`r`n" } else { "`n" } |
| 60 | + $lines = $Content -split "`r?`n" |
| 61 | + |
| 62 | + $searchStart = 0 |
| 63 | + $lastMatchIndex = -1 |
| 64 | + foreach ($pattern in $Patterns) { |
| 65 | + $found = $false |
| 66 | + for ($i = $searchStart; $i -lt $lines.Count; $i++) { |
| 67 | + if ($lines[$i] -match $pattern) { |
| 68 | + $lastMatchIndex = $i |
| 69 | + $searchStart = $i + 1 |
| 70 | + $found = $true |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + if (-not $found) { |
| 75 | + return $Content |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + $insertAt = $lastMatchIndex + 1 |
| 80 | + $before = if ($lastMatchIndex -ge 0) { $lines[0..$lastMatchIndex] } else { @() } |
| 81 | + $after = if ($insertAt -lt $lines.Count) { $lines[$insertAt..($lines.Count-1)] } else { @() } |
| 82 | + $insertLines = $Insert -split "`r?`n" |
| 83 | + |
| 84 | + $newLines = @() |
| 85 | + $newLines += $before |
| 86 | + $newLines += $insertLines |
| 87 | + $newLines += $after |
| 88 | + |
| 89 | + return ($newLines -join $eol) |
| 90 | +} |
| 91 | + |
| 92 | +function Set-CommentLine { |
| 93 | + [CmdletBinding()] |
| 94 | + param( |
| 95 | + [Parameter(Mandatory)][string]$Content, |
| 96 | + [Parameter(Mandatory)][string]$Pattern |
| 97 | + ) |
| 98 | + |
| 99 | + $eol = if ($Content -match "`r`n") { "`r`n" } else { "`n" } |
| 100 | + $lines = $Content -split "`r?`n" |
| 101 | + for ($i = 0; $i -lt $lines.Count; $i++) { |
| 102 | + if ($lines[$i] -match $Pattern) { |
| 103 | + $lines[$i] = $lines[$i] -replace '^( *)', '${1}// ' |
| 104 | + break |
| 105 | + } |
| 106 | + } |
| 107 | + return ($lines -join $eol) |
| 108 | +} |
0 commit comments