lzybkr / TabExpansionPlusPlus

A V3 PowerShell module to improve tab expansion and Intellisense
BSD 2-Clause "Simplified" License
196 stars 33 forks source link

Support for exe with double hyphen commands #45

Closed willyd closed 9 years ago

willyd commented 9 years ago

Hi,

I am trying to add some completion to cmake.exe which has some options that are normal powershell parameter with a single hyphen:

cmake -G "Generator"

but some others have double hyphens li

cmake --build .

I can't seem to find how to support the double hyphen type. Any ideas on how this can be acheived? Here is the code I have now:

function CMakeCompletion {
    [ArgumentCompleter(
    Native,
    Command = 'cmake',
    Description = 'Complete arguments to cmake.exe'
    )]

    param(
    $wordToComplete, 
    $commandAst
    )        
    $tryParameters = $true
    $last = if ($wordToComplete) { -2 } else { -1 }
    $parameterAst = $commandAst.CommandElements[$last] -as [System.Management.Automation.Language.CommandParameterAst]    
    if ($parameterAst -ne $null)
    {
        # remove the quotes from word to complete if any
        # this is necessary as some of the generators have 
        # white spaces in them.
        $wordToComplete = $wordToComplete.Replace("'", "")        

        $completions = $null

        if ("G".StartsWith($parameterAst.ParameterName, "OrdinalIgnoreCase")) {
            $VisualStudioVersion = '10 2010', '11 2012', '12 2013', '14 2015'
            $VSGenerators = $VisualStudioVersion | % {"Visual Studio $_", "Visual Studio $_ Win64"}
            $CMakeGenerators = $VSGenerators + @('NMake Makefiles', 'MinGW Makefiles', 'Ninja', 'Sublime Text 2 - Ninja', 'Sublime Text 2 - Ninja', 'Sublime Text 2 - MinGW Makefiles')
            $completions = $CMakeGenerators
        }

        if("D".StartsWith($parameterAst.ParameterName, "OrdinalIgnoreCase")) {
            $CMakeCommonDefinitions = @('BUILD_SHARED_LIBS=OFF', 'BUILD_SHARED_LIBS=ON', 'CMAKE_BUILD_TYPE=Release', 'CMAKE_BUILD_TYPE=Debug')
            $completions = $CMakeCommonDefinitions
        }

        # I can't get powershell to get here with the --build parameter!

        $tryParameters = ($completions.Length -eq 0)

        $completions | Where-Object { $_ -match "^$wordToComplete"} | ForEach-Object { New-CompletionResult $_ }
    }

    if ($tryParameters -and ($wordToComplete.StartsWith("-") -or "" -eq $wordToComplete))
    {
        echo -- -G -D --build |
                    Where-Object { $_ -like "$wordToComplete*" } |
                    Sort-Object |
                    ForEach-Object {
                        New-CompletionResult $_
                    }
    }
}
lzybkr commented 9 years ago

PowerShell parses tokens starting with -- as an argument, not a parameter. So you'll have to also look at command elements of type StringConstantExpression where the Value starts with --.