1 | <?php |
||
11 | class CompletionContext |
||
12 | { |
||
13 | /** |
||
14 | * The current contents of the command line as a single string |
||
15 | * |
||
16 | * Bash equivalent: COMP_LINE |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $commandLine; |
||
21 | |||
22 | /** |
||
23 | * The index of the user's cursor relative to the start of the command line. |
||
24 | * |
||
25 | * If the current cursor position is at the end of the current command, |
||
26 | * the value of this variable is equal to the length of $this->commandLine |
||
27 | * |
||
28 | * Bash equivalent: COMP_POINT |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $charIndex = 0; |
||
33 | |||
34 | /** |
||
35 | * An array of the individual words in the current command line. |
||
36 | * |
||
37 | * This is not set until $this->splitCommand() is called, when it is populated by |
||
38 | * $commandLine exploded by $wordBreaks |
||
39 | * |
||
40 | * Bash equivalent: COMP_WORDS |
||
41 | * |
||
42 | * @var string[]|null |
||
43 | */ |
||
44 | protected $words = null; |
||
45 | |||
46 | /** |
||
47 | * Words from the currently command-line before quotes and escaping is processed |
||
48 | * |
||
49 | * This is indexed the same as $this->words, but in their raw input terms are in their input form, including |
||
50 | * quotes and escaping. |
||
51 | * |
||
52 | * @var string[]|null |
||
53 | */ |
||
54 | protected $rawWords = null; |
||
55 | |||
56 | /** |
||
57 | * The index in $this->words containing the word at the current cursor position. |
||
58 | * |
||
59 | * This is not set until $this->splitCommand() is called. |
||
60 | * |
||
61 | * Bash equivalent: COMP_CWORD |
||
62 | * |
||
63 | * @var int|null |
||
64 | */ |
||
65 | protected $wordIndex = null; |
||
66 | |||
67 | /** |
||
68 | * Characters that $this->commandLine should be split on to get a list of individual words |
||
69 | * |
||
70 | * Bash equivalent: COMP_WORDBREAKS |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $wordBreaks = "= \t\n"; |
||
75 | |||
76 | /** |
||
77 | * Set the whole contents of the command line as a string |
||
78 | * |
||
79 | * @param string $commandLine |
||
80 | */ |
||
81 | public function setCommandLine($commandLine) |
||
86 | |||
87 | /** |
||
88 | * Return the current command line verbatim as a string |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getCommandLine() |
||
96 | |||
97 | /** |
||
98 | * Return the word from the command line that the cursor is currently in |
||
99 | * |
||
100 | * Most of the time this will be a partial word. If the cursor has a space before it, |
||
101 | * this will return an empty string, indicating a new word. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getCurrentWord() |
||
113 | |||
114 | /** |
||
115 | * Return the unprocessed string for the word under the cursor |
||
116 | * |
||
117 | * This preserves any quotes and escaping that are present in the input command line. |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getRawCurrentWord() |
||
129 | |||
130 | /** |
||
131 | * Return a word by index from the command line |
||
132 | * |
||
133 | * @see $words, $wordBreaks |
||
134 | * @param int $index |
||
135 | * @return string |
||
136 | */ |
||
137 | public function getWordAtIndex($index) |
||
145 | |||
146 | /** |
||
147 | * Get the contents of the command line, exploded into words based on the configured word break characters |
||
148 | * |
||
149 | * @see $wordBreaks, setWordBreaks |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getWords() |
||
160 | |||
161 | /** |
||
162 | * Get the unprocessed/literal words from the command line |
||
163 | * |
||
164 | * This is indexed the same as getWords(), but preserves any quoting and escaping from the command line |
||
165 | * |
||
166 | * @return string[] |
||
167 | */ |
||
168 | public function getRawWords() |
||
176 | |||
177 | /** |
||
178 | * Get the index of the word the cursor is currently in |
||
179 | * |
||
180 | * @see getWords, getCurrentWord |
||
181 | * @return int |
||
182 | */ |
||
183 | public function getWordIndex() |
||
191 | |||
192 | /** |
||
193 | * Get the character index of the user's cursor on the command line |
||
194 | * |
||
195 | * This is in the context of the full command line string, so includes word break characters. |
||
196 | * Note that some shells can only provide an approximation for character index. Under ZSH for |
||
197 | * example, this will always be the character at the start of the current word. |
||
198 | * |
||
199 | * @return int |
||
200 | */ |
||
201 | public function getCharIndex() |
||
205 | |||
206 | /** |
||
207 | * Set the cursor position as a character index relative to the start of the command line |
||
208 | * |
||
209 | * @param int $index |
||
210 | */ |
||
211 | public function setCharIndex($index) |
||
216 | |||
217 | /** |
||
218 | * Set characters to use as split points when breaking the command line into words |
||
219 | * |
||
220 | * This defaults to a sane value based on BASH's word break characters and shouldn't |
||
221 | * need to be changed unless your completions contain the default word break characters. |
||
222 | * |
||
223 | * @deprecated This is becoming an internal setting that doesn't make sense to expose publicly. |
||
224 | * |
||
225 | * @see wordBreaks |
||
226 | * @param string $charList - a single string containing all of the characters to break words on |
||
227 | */ |
||
228 | public function setWordBreaks($charList) |
||
234 | |||
235 | /** |
||
236 | * Split the command line into words using the configured word break characters |
||
237 | * |
||
238 | * @return string[] |
||
239 | */ |
||
240 | protected function splitCommand() |
||
286 | |||
287 | /** |
||
288 | * Return a token's value with escaping and quotes removed |
||
289 | * |
||
290 | * @see self::tokenizeString() |
||
291 | * @param array $token |
||
292 | * @return string |
||
293 | */ |
||
294 | protected function getTokenValue($token) |
||
308 | |||
309 | /** |
||
310 | * Break a string into words, quoted strings and non-words (breaks) |
||
311 | * |
||
312 | * Returns an array of unmodified segments of $string with offset and type information. |
||
313 | * |
||
314 | * @param string $string |
||
315 | * @return array as [ [type => string, value => string, offset => int], ... ] |
||
316 | */ |
||
317 | protected function tokenizeString($string) |
||
381 | |||
382 | /** |
||
383 | * Reset the computed words so that $this->splitWords is forced to run again |
||
384 | */ |
||
385 | protected function reset() |
||
390 | } |
||
391 |