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 containing 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 array|null |
||
43 | */ |
||
44 | protected $words = null; |
||
45 | |||
46 | /** |
||
47 | * The index in $this->words containing the word at the current cursor position. |
||
48 | * |
||
49 | * This is not set until $this->splitCommand() is called. |
||
50 | * |
||
51 | * Bash equivalent: COMP_CWORD |
||
52 | * |
||
53 | * @var int|null |
||
54 | */ |
||
55 | protected $wordIndex = null; |
||
56 | |||
57 | /** |
||
58 | * Characters that $this->commandLine should be split on to get a list of individual words |
||
59 | * |
||
60 | * Bash equivalent: COMP_WORDBREAKS |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $wordBreaks = "= \t\n"; |
||
65 | |||
66 | /** |
||
67 | * Set the whole contents of the command line as a string |
||
68 | * |
||
69 | * @param string $commandLine |
||
70 | */ |
||
71 | public function setCommandLine($commandLine) |
||
76 | |||
77 | /** |
||
78 | * Return the current command line verbatim as a string |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getCommandLine() |
||
86 | |||
87 | /** |
||
88 | * Return the word from the command line that the cursor is currently in |
||
89 | * |
||
90 | * Most of the time this will be a partial word. If the cursor has a space before it, |
||
91 | * this will return an empty string, indicating a new word. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getCurrentWord() |
||
103 | |||
104 | /** |
||
105 | * Return a word by index from the command line |
||
106 | * |
||
107 | * @see $words, $wordBreaks |
||
108 | * @param int $index |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getWordAtIndex($index) |
||
119 | |||
120 | /** |
||
121 | * Get the contents of the command line, exploded into words based on the configured word break characters |
||
122 | * |
||
123 | * @see $wordBreaks, setWordBreaks |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getWords() |
||
134 | |||
135 | /** |
||
136 | * Get the index of the word the cursor is currently in |
||
137 | * |
||
138 | * @see getWords, getCurrentWord |
||
139 | * @return int |
||
140 | */ |
||
141 | public function getWordIndex() |
||
149 | |||
150 | /** |
||
151 | * Get the character index of the user's cursor on the command line |
||
152 | * |
||
153 | * This is in the context of the full command line string, so includes word break characters. |
||
154 | * Note that some shells can only provide an approximation for character index. Under ZSH for |
||
155 | * example, this will always be the character at the start of the current word. |
||
156 | * |
||
157 | * @return int |
||
158 | */ |
||
159 | public function getCharIndex() |
||
163 | |||
164 | /** |
||
165 | * Set the cursor position as a character index relative to the start of the command line |
||
166 | * |
||
167 | * @param int $index |
||
168 | */ |
||
169 | public function setCharIndex($index) |
||
174 | |||
175 | /** |
||
176 | * Set characters to use as split points when breaking the command line into words |
||
177 | * |
||
178 | * This defaults to a sane value based on BASH's word break characters and shouldn't |
||
179 | * need to be changed unless your completions contain the default word break characters. |
||
180 | * |
||
181 | * @deprecated This is becoming an internal setting that doesn't make sense to expose publicly. |
||
182 | * |
||
183 | * @see wordBreaks |
||
184 | * @param string $charList - a single string containing all of the characters to break words on |
||
185 | */ |
||
186 | public function setWordBreaks($charList) |
||
192 | |||
193 | /** |
||
194 | * Split the command line into words using the configured word break characters |
||
195 | * |
||
196 | * @return string[] |
||
197 | */ |
||
198 | protected function splitCommand() |
||
236 | |||
237 | /** |
||
238 | * Return a token's value with escaping and quotes removed |
||
239 | * |
||
240 | * @see self::tokenizeString() |
||
241 | * @param array $token |
||
242 | * @return string |
||
243 | */ |
||
244 | protected function getTokenValue($token) |
||
258 | |||
259 | /** |
||
260 | * Break a string into words, quoted strings and non-words (breaks) |
||
261 | * |
||
262 | * Returns an array of unmodified segments of $string with offset and type information. |
||
263 | * |
||
264 | * @param string $string |
||
265 | * @return array as [ [type => string, value => string, offset => int], ... ] |
||
266 | */ |
||
267 | protected function tokenizeString($string) |
||
331 | |||
332 | /** |
||
333 | * Reset the computed words so that $this->splitWords is forced to run again |
||
334 | */ |
||
335 | protected function reset() |
||
340 | } |
||
341 |