Skip to content

About the hooks.ps1

  • Each completion has a hooks.ps1 file, which is used to dynamically add completion items.
  • It needs to provide a handleCompletions function.
  • The function will receive a parameter $completions, which is an array containing all completion items parsed from json.
  • The function needs to return a new array, which will be used as the completion data by PSCompletions.

  • If config.json does not define hooks, it will never be called.
  • Only when config.json defines hooks and meets the following conditions will it be called:
    • hooks is true, and has not been disabled by psc completion xxx enable_hooks 0
    • hooks is false, but has been enabled by psc completion xxx enable_hooks 1
powershell
function handleCompletions($completions) {
    $list = @()

    $input_arr = $PSCompletions.input_arr
    $filter_input_arr = $PSCompletions.filter_input_arr # Exclude options parameters
    $first_item = $filter_input_arr[0] # The first subcommand
    $last_item = $filter_input_arr[-1] # The last subcommand

    # switch ($first_item) {
    #     'add' {
    #         if ('aaa' -notin $input_arr) {
    #             $list += $PSCompletions.return_completion('aaa', "Add aaa")
    #         }
    #     }
    # }

    # switch ($last_item) {
    #     'add' {
    #         $list += $PSCompletions.return_completion('bbb', "Add bbb")
    #     }
    # }

    # $list += $PSCompletions.return_completion('example', "It's from hooks.ps1")

    return $list + $completions
}

TIP

  • The function $PSCompletions.return_completion is used to add new completion items.
  • The first parameter is the completion item name.
  • The second parameter is the completion item help(tip).
  • The third parameter is optional, used to specify the special symbol of the completion item.
    • SpaceTab: ~
    • OptionTab: ?
    • WriteSpaceTab: !