CompletionContext.php
6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
namespace Stecman\Component\Symfony\Console\BashCompletion;
/**
* Command line context for completion
*
* Represents the current state of the command line that is being completed
*/
class CompletionContext
{
/**
* The current contents of the command line as a single string
*
* Bash equivalent: COMP_LINE
*
* @var string
*/
protected $commandLine;
/**
* The index of the user's cursor relative to the start of the command line.
*
* If the current cursor position is at the end of the current command,
* the value of this variable is equal to the length of $this->commandLine
*
* Bash equivalent: COMP_POINT
*
* @var int
*/
protected $charIndex = 0;
/**
* An array containing the individual words in the current command line.
*
* This is not set until $this->splitCommand() is called, when it is populated by
* $commandLine exploded by $wordBreaks
*
* Bash equivalent: COMP_WORDS
*
* @var array|null
*/
protected $words = null;
/**
* The index in $this->words containing the word at the current cursor position.
*
* This is not set until $this->splitCommand() is called.
*
* Bash equivalent: COMP_CWORD
*
* @var int|null
*/
protected $wordIndex = null;
/**
* Characters that $this->commandLine should be split on to get a list of individual words
*
* Bash equivalent: COMP_WORDBREAKS
*
* @var string
*/
protected $wordBreaks = "'\"()= \t\n";
/**
* Set the whole contents of the command line as a string
*
* @param string $commandLine
*/
public function setCommandLine($commandLine)
{
$this->commandLine = $commandLine;
$this->reset();
}
/**
* Return the current command line verbatim as a string
*
* @return string
*/
public function getCommandLine()
{
return $this->commandLine;
}
/**
* Return the word from the command line that the cursor is currently in
*
* Most of the time this will be a partial word. If the cursor has a space before it,
* this will return an empty string, indicating a new word.
*
* @return string
*/
public function getCurrentWord()
{
if (isset($this->words[$this->wordIndex])) {
return $this->words[$this->wordIndex];
}
return '';
}
/**
* Return a word by index from the command line
*
* @see $words, $wordBreaks
* @param int $index
* @return string
*/
public function getWordAtIndex($index)
{
if (isset($this->words[$index])) {
return $this->words[$index];
}
return '';
}
/**
* Get the contents of the command line, exploded into words based on the configured word break characters
*
* @see $wordBreaks, setWordBreaks
* @return array
*/
public function getWords()
{
if ($this->words === null) {
$this->splitCommand();
}
return $this->words;
}
/**
* Get the index of the word the cursor is currently in
*
* @see getWords, getCurrentWord
* @return int
*/
public function getWordIndex()
{
if ($this->wordIndex === null) {
$this->splitCommand();
}
return $this->wordIndex;
}
/**
* Get the character index of the user's cursor on the command line
*
* This is in the context of the full command line string, so includes word break characters.
* Note that some shells can only provide an approximation for character index. Under ZSH for
* example, this will always be the character at the start of the current word.
*
* @return int
*/
public function getCharIndex()
{
return $this->charIndex;
}
/**
* Set the cursor position as a character index relative to the start of the command line
*
* @param int $index
*/
public function setCharIndex($index)
{
$this->charIndex = $index;
$this->reset();
}
/**
* Set characters to use as split points when breaking the command line into words
*
* This defaults to a sane value based on BASH's word break characters and shouldn't
* need to be changed unless your completions contain the default word break characters.
*
* @see wordBreaks
* @param string $charList - a single string containing all of the characters to break words on
*/
public function setWordBreaks($charList)
{
$this->wordBreaks = $charList;
$this->reset();
}
/**
* Split the command line into words using the configured word break characters
*
* @return string[]
*/
protected function splitCommand()
{
$this->words = array();
$this->wordIndex = null;
$cursor = 0;
$breaks = preg_quote($this->wordBreaks);
if (!preg_match_all("/([^$breaks]*)([$breaks]*)/", $this->commandLine, $matches)) {
return;
}
// Groups:
// 1: Word
// 2: Break characters
foreach ($matches[0] as $index => $wholeMatch) {
// Determine which word the cursor is in
$cursor += strlen($wholeMatch);
$word = $matches[1][$index];
$breaks = $matches[2][$index];
if ($this->wordIndex === null && $cursor >= $this->charIndex) {
$this->wordIndex = $index;
// Find the user's cursor position relative to the end of this word
// The end of the word is the internal cursor minus any break characters that were captured
$cursorWordOffset = $this->charIndex - ($cursor - strlen($breaks));
if ($cursorWordOffset < 0) {
// Cursor is inside the word - truncate the word at the cursor
// (This emulates normal BASH completion behaviour I've observed, though I'm not entirely sure if it's useful)
$word = substr($word, 0, strlen($word) + $cursorWordOffset);
} elseif ($cursorWordOffset > 0) {
// Cursor is in the break-space after a word
// Push an empty word at the cursor to allow completion of new terms at the cursor, ignoring words ahead
$this->wordIndex++;
$this->words[] = $word;
$this->words[] = '';
continue;
}
}
if ($word !== '') {
$this->words[] = $word;
}
}
if ($this->wordIndex > count($this->words) - 1) {
$this->wordIndex = count($this->words) - 1;
}
}
/**
* Reset the computed words so that $this->splitWords is forced to run again
*/
protected function reset()
{
$this->words = null;
$this->wordIndex = null;
}
}