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 final 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 # Without -*

    # switch ($filter_input_arr[-1]) {
    #     'add' {
    #         $list += $PSCompletions.return_completion('aaa', "Add aaa")
    #     }
    # }

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

    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: !