<commit_before># make sure we stop on exceptions
$ErrorActionPreference = "Stop"

# This object reprents the result value for tab expansion functions when no result is returned.
# This is so that we can distinguish it from $null, which has different semantics
$NoResultValue = New-Object PSObject -Property @{ NoResult = $true }

# Hashtable that stores tab expansion definitions
$TabExpansionCommands = @{}

# This function allows 3rd parties to enable intellisense for arbitrary functions
function Register-TabExpansion {
    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true)]
        [string]$Name,
        [parameter(Mandatory = $true)]
        $Definition
    )
 
    $TabExpansionCommands[$Name] = $Definition 
}

Register-TabExpansion 'Install-Package' @{
    'Id' = {
        param($context)
        GetPackageIds (GetPackages $context)
    }
    'ProjectName' = {
        GetProjectNames
    }
    'Version' = {
        param($context)

        $parameters = @{}

        if ($context.Id) { $parameters.filter = $context.Id }
        if ($context.Source) { $parameters.source = $context.Source }

        GetPackageVersions (Get-Package @parameters -Remote -ErrorAction SilentlyContinue) $context 
    }
}

Register-TabExpansion 'Uninstall-Package' @{
    'Id' = {
        param($context)

        $parameters = @{}
        if ($context.id) { $parameters.filter = $context.id }

        GetPackageIds (Find-Package @parameters -ErrorAction SilentlyContinue)
    }
    'ProjectName' = {
        GetProjectNames
    }
    'Version' = {
        $parameters = @{}
        if ($context.id) { $parameters.filter = $context.id }

        GetPackageVersions (Get-Package @parameters) $context
    }
}

Register-TabExpansion 'Update-Package' @{
    'Id' = {
        param($context)

        $parameters = @{}
        if ($context.id) { $parameters.filter = $context.id }

        GetPackageIds (Find-Package @parameters -ErrorAction SilentlyContinue)
    }
    'ProjectName' = {
        GetProjectNames
    }
    'Version' = {
        param($context)

        $parameters = @{}
        if ($context.id) { $parameters.filter = $context.id }

        GetPackageVersions (Get-Package -Remote @parameters) $context
    }
}

Register-TabExpansion 'New-Package' @{ 'ProjectName' = { GetProjectNames } }
Register-TabExpansion 'Add-BindingRedirect' @{ 'ProjectName' = { GetProjectNames } }
Register-TabExpansion 'Get-Project' @{ 'Name' = { GetProjectNames } }

function GetPackages($context) {  
    $parameters = @{}

    if ($context.Id) { $parameters.filter = $context.Id }
    if ($context.Source) { $parameters.source = $context.Source }

    return Find-Package @parameters -Remote -ErrorAction SilentlyContinue
}

function GetProjectNames {
    $uniqueNames = @(Get-Project -All | Select-Object -ExpandProperty ProjectName)
    
    $simpleNames = Get-Project -All | Select-Object -ExpandProperty Name
    $safeNames = @($simpleNames | Group-Object | Where-Object { $_.Count -eq 1 } | Select-Object -ExpandProperty Name)

    ($uniqueNames + $safeNames) | Select-Object -Unique | Sort-Object
}

function GetPackageIds($packages) {
    $packages | Select -ExpandProperty Id
}

function GetPackageVersions($packages, $context) {
    $packages | Where-Object { $_.Id -eq $context.Id } | Select -ExpandProperty Version | Sort-Object -Descending
}

function NugetTabExpansion($line, $lastWord) {
    # Parse the command
    $parsedCommand = [NuGetConsole.Host.PowerShell.CommandParser]::Parse($line)

    # Get the command definition
    $definition = $TabExpansionCommands[$parsedCommand.CommandName]

    # See if we've registered a command for intellisense
    if($definition) {
        # Get the command that we're trying to show intellisense for
        $command = Get-Command $parsedCommand.CommandName -ErrorAction SilentlyContinue

        if($command) {            
            # We're trying to find out what parameter we're trying to show intellisense for based on <commit_msg>Convert to version object before sorting by version. This only matters for items from the feed, local repositories are fine. Work items: 678<commit_after># make sure we stop on exceptions
$ErrorActionPreference = "Stop"

# This object reprents the result value for tab expansion functions when no result is returned.
# This is so that we can distinguish it from $null, which has different semantics
$NoResultValue = New-Object PSObject -Property @{ NoResult = $true }

# Hashtable that stores tab expansion definitions
$TabExpansionCommands = @{}

# This function allows 3rd parties to enable intellisense for arbitrary functions
function Register-TabExpansion {
    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true)]
        [string]$Name,
        [parameter(Mandatory = $true)]
        $Definition
    )
 
    $TabExpansionCommands[$Name] = $Definition 
}

Register-TabExpansion 'Install-Package' @{
    'Id' = {
        param($context)
        GetPackageIds (GetPackages $context)
    }
    'ProjectName' = {
        GetProjectNames
    }
    'Version' = {
        param($context)

        $parameters = @{}

        if ($context.Id) { $parameters.filter = $context.Id }
        if ($context.Source) { $parameters.source = $context.Source }

        GetPackageVersions (Get-Package @parameters -Remote -ErrorAction SilentlyContinue) $context 
    }
}

Register-TabExpansion 'Uninstall-Package' @{
    'Id' = {
        param($context)

        $parameters = @{}
        if ($context.id) { $parameters.filter = $context.id }

        GetPackageIds (Find-Package @parameters -ErrorAction SilentlyContinue)
    }
    'ProjectName' = {
        GetProjectNames
    }
    'Version' = {
        $parameters = @{}
        if ($context.id) { $parameters.filter = $context.id }

        GetPackageVersions (Get-Package @parameters) $context
    }
}

Register-TabExpansion 'Update-Package' @{
    'Id' = {
        param($context)

        $parameters = @{}
        if ($context.id) { $parameters.filter = $context.id }

        GetPackageIds (Find-Package @parameters -ErrorAction SilentlyContinue)
    }
    'ProjectName' = {
        GetProjectNames
    }
    'Version' = {
        param($context)

        $parameters = @{}
        if ($context.id) { $parameters.filter = $context.id }

        GetPackageVersions (Get-Package -Remote @parameters) $context
    }
}

Register-TabExpansion 'New-Package' @{ 'ProjectName' = { GetProjectNames } }
Register-TabExpansion 'Add-BindingRedirect' @{ 'ProjectName' = { GetProjectNames } }
Register-TabExpansion 'Get-Project' @{ 'Name' = { GetProjectNames } }

function GetPackages($context) {  
    $parameters = @{}

    if ($context.Id) { $parameters.filter = $context.Id }
    if ($context.Source) { $parameters.source = $context.Source }

    return Find-Package @parameters -Remote -ErrorAction SilentlyContinue
}

function GetProjectNames {
    $uniqueNames = @(Get-Project -All | Select-Object -ExpandProperty ProjectName)
    
    $simpleNames = Get-Project -All | Select-Object -ExpandProperty Name
    $safeNames = @($simpleNames | Group-Object | Where-Object { $_.Count -eq 1 } | Select-Object -ExpandProperty Name)

    ($uniqueNames + $safeNames) | Select-Object -Unique | Sort-Object
}

function GetPackageIds($packages) {
    $packages | Select -ExpandProperty Id
}

function GetPackageVersions($packages, $context) {
    $packages | Where-Object { $_.Id -eq $context.Id } | Select -ExpandProperty Version | %{
        # Convert to version if the we're looking at the version as a string
        if($_ -is [string]) { 
            [Version]::Parse($_) 
        } else { 
            $_ 
        }  
    } | Sort-Object -Descending
}

function NugetTabExpansion($line, $lastWord) {
    # Parse the command
    $parsedCommand = [NuGetConsole.Host.PowerShell.CommandParser]::Parse($line)

    # Get the command definition
    $definition = $TabExpansionCommands[$parsedCommand.CommandName]

    # See if we've registered a command for intellisense
    if($definition) {
        # Get the command that we're trying to show intellisense for
        $command = Get-Command $parsedCommand.CommandName -ErrorAction SilentlyContinue

        if($command) {            
            # We're trying to find out what parameter we're trying to show intellisense for based on 
{'fraction_non_alphanumeric': 0.11412782316194138, 'fraction_numerical': 0.0008409418548774628, 'mean_word_length': 3.0004805382027873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7652059', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2313, 'n_words': 612}