Module: Completion::Shell::Zsh

Defined in:
lib/completion/shell/zsh.rb

Overview

Zsh completion adapter generation.

Class Method Summary collapse

Class Method Details

.script(executable) ⇒ Object

Generate a Zsh completion adapter script.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/completion/shell/zsh.rb', line 14

def self.script(executable)
	command = Shell.command_name(executable)
	
	if executable
		return <<~SCRIPT
			#compdef #{command}
			
			local _completion_source="${(%):-%x}"
			local _completion_directory="${_completion_source:h}"
			source "${_completion_directory}/completion.zsh"
			__completion_complete "$@"
		SCRIPT
	end
	
	<<~SCRIPT
		#compdef -default-
		
		local _completion_source="${(%):-%x}"
		local _completion_directory="${_completion_source:h}"
		source "${_completion_directory}/completion.zsh"
		__completion_register_default
		__completion_complete default "$@"
	SCRIPT
end

.shared_scriptObject

Generate the shared Zsh helper script.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/completion/shell/zsh.rb', line 42

def self.shared_script
	<<~SCRIPT
		__completion_resolve() {
			local command="$1"
			local basename="${command:t}"
			
			if [[ "$command" == */* ]]; then
				printf "%s\\n" "${command:h}/completion-${basename}"
			else
				printf "%s\\n" "completion-${basename}"
			fi
		}
		
		__completion_complete() {
			local fallback="$1"
			if [[ "$fallback" == "default" ]]; then
				shift
			else
				fallback=""
			fi
			
			local command="${words[1]}"
			local completer="$(__completion_resolve "$command")"
			
			if [[ ! -x "$completer" ]]; then
				if [[ "$fallback" == "default" ]]; then
					_default "$@"
				fi
				
				return
			fi
			
			local -a argv
			argv=("${(@)words[2,CURRENT]}")
			
			local -a completions
			local paths=false
			local directories=false
			local executables=false
			while IFS=$'\\t' read -r type value description metadata; do
				case "$type" in
					path)
						paths=true
						;;
					directory)
						directories=true
						;;
					executable)
						executables=true
						;;
					delegate)
						if [[ "$metadata" == *index=* ]]; then
							local index="${metadata##*index=}"
							index="${index%%$'\\t'*}"
							words=("${(@)words[$((index + 2)),-1]}")
							CURRENT=$((CURRENT - index - 1))
							_normal
							return
						fi
						;;
					*)
						completions+=("${value}:${description}")
						;;
				esac
			done < <("$completer" "${argv[@]}")
			
			if [[ "$paths" == true ]]; then
				_path_files
			fi
			
			if [[ "$directories" == true ]]; then
				_files -/
			fi
			
			if [[ "$executables" == true ]]; then
				_path_commands
			fi
			
			_describe 'completion' completions
		}
		
		__completion_register_default() {
			(( $+functions[compdef] )) && compdef _completion -default-
		}
	SCRIPT
end