About the hooks.ps1
- Each completion has a
hooks.ps1file, which is used to dynamically add completion items. - It needs to provide a
handleCompletionsfunction. - 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.jsondoes not definehooks, it will never be called. - Only when
config.jsondefineshooksand meets the following conditions will it be called:hooksistrue, and has not been disabled bypsc completion xxx enable_hooks 0hooksisfalse, but has been enabled bypsc 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_completionis 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:!