Functions/GenXdev.Coding.PowerShell.Modules/Open-GenXdevCmdletsContainingClipboardTextInIde.ps1

###############################################################################
<#
.SYNOPSIS
Opens files in IDE that contain clipboard text
 
.DESCRIPTION
The text in the clipboard is used to search in all GenXdev scripts and when found opens that file in Code or Visual Studio
###############################################################################>

function Open-GenXdevCmdletsContainingClipboardTextInIde {

    ############################################################################
    [CmdletBinding()]
    [Alias('vscodesearch')]
    param (
        ############################################################################
        [Parameter(
            Mandatory = $false,
            Position = 0,
            ValueFromPipeline = $true,
            HelpMessage = 'Search for clipboard text in all GenXdev scripts'
        )]
        [string] $InputObject,
        ########################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Add sourcefile to Copilot edit-session'
        )]
        [switch]$Copilot
    )

    ############################################################################
    begin {

        $clipboardText = Microsoft.PowerShell.Management\Get-Clipboard

        if ($Copilot) {
            $null = GenXdev.Coding\EnsureCopilotKeyboardShortCut
        }
    }

    ############################################################################

    process {

        if ($null -eq $inputObject) {
            $inputObject = $clipboardText
        }

        if ([string]::IsNullOrWhiteSpace($inputObject)) {
            Microsoft.PowerShell.Utility\Write-Error 'No input object provided. Please provide a string to search for or set a clipboard text.'
            return
        }

        $searchPattern = [System.Text.RegularExpressions.Regex]::Escape($inputObject)

        GenXdev.Helpers\Invoke-OnEachGenXdevModule {

            param($module)

            $filePaths = GenXdev.FileSystem\Find-Item `
                -SearchMask '*.ps1' `
                -Pattern $searchPattern `
                -PassThru | Microsoft.PowerShell.Core\ForEach-Object FullNames

            if ($filePaths) {

                $invocationArgs = GenXdev.Helpers\Copy-IdenticalParamValues `
                    -BoundParameters $PSBoundParameters `
                    -FunctionName 'GenXdev.Coding\Open-SourceFileInIde'

                $invocationArgs.Path = $filePaths
                $invocationArgs.Code = $true

                if ($Copilot) {

                    $invocationArgs.KeysToSend = @('^+%{F12}')
                }

                GenXdev.Coding\Open-SourceFileInIde @invocationArgs
            }
        }
    }

    ############################################################################
    end {

    }
}