Components/PS.Util.CommentHelp/Public/Convert-PSUtilCommentHelpToInside.ps1
|
function Convert-PSUtilCommentHelpToInside { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$FilePath, [Parameter(Mandatory = $true)] [string]$FunctionName ) # 1. Locate targets $Target = Get-AstHelpPosition -FilePath $FilePath | Where-Object { $_.FunctionName -eq $FunctionName } if ($null -eq $Target -or $Target.HelpPosition -ne 'Outside') { Write-Warning "Function '$FunctionName' help is already Inside or does not exist." return } $RawContent = [System.IO.File]::ReadAllText($FilePath) $CommentText = $Target.CommentToken.Text # 2. Extract bounds $CommentStart = $Target.CommentToken.Extent.StartOffset $CommentLength = $Target.CommentToken.Extent.EndOffset - $CommentStart # Find the precise opening brace position inside the body AST block $BodyStartOffset = $Target.FuncAst.Body.Extent.StartOffset # 3. Surgical String Manipulation # Remove the comment text completely from outside the header $StrippedContent = $RawContent.Remove($CommentStart, $CommentLength) # Re-calculate index alignment since deleting elements shifted the remaining text backwards $ShiftedBodyStart = $BodyStartOffset - $CommentLength # Safely insert the help comment inside the open brace block (adding a clean indentation spacing newline) $FinalContent = $StrippedContent.Insert($ShiftedBodyStart + 1, "`n $CommentText") [System.IO.File]::WriteAllText($FilePath, $FinalContent) Write-Verbose "Moved comment-based help for '$FunctionName' to the INSIDE." } |