CompletionContext.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace Stecman\Component\Symfony\Console\BashCompletion;
  3. /**
  4. * Command line context for completion
  5. *
  6. * Represents the current state of the command line that is being completed
  7. */
  8. class CompletionContext
  9. {
  10. /**
  11. * The current contents of the command line as a single string
  12. *
  13. * Bash equivalent: COMP_LINE
  14. *
  15. * @var string
  16. */
  17. protected $commandLine;
  18. /**
  19. * The index of the user's cursor relative to the start of the command line.
  20. *
  21. * If the current cursor position is at the end of the current command,
  22. * the value of this variable is equal to the length of $this->commandLine
  23. *
  24. * Bash equivalent: COMP_POINT
  25. *
  26. * @var int
  27. */
  28. protected $charIndex = 0;
  29. /**
  30. * An array containing the individual words in the current command line.
  31. *
  32. * This is not set until $this->splitCommand() is called, when it is populated by
  33. * $commandLine exploded by $wordBreaks
  34. *
  35. * Bash equivalent: COMP_WORDS
  36. *
  37. * @var array|null
  38. */
  39. protected $words = null;
  40. /**
  41. * The index in $this->words containing the word at the current cursor position.
  42. *
  43. * This is not set until $this->splitCommand() is called.
  44. *
  45. * Bash equivalent: COMP_CWORD
  46. *
  47. * @var int|null
  48. */
  49. protected $wordIndex = null;
  50. /**
  51. * Characters that $this->commandLine should be split on to get a list of individual words
  52. *
  53. * Bash equivalent: COMP_WORDBREAKS
  54. *
  55. * @var string
  56. */
  57. protected $wordBreaks = "'\"()= \t\n";
  58. /**
  59. * Set the whole contents of the command line as a string
  60. *
  61. * @param string $commandLine
  62. */
  63. public function setCommandLine($commandLine)
  64. {
  65. $this->commandLine = $commandLine;
  66. $this->reset();
  67. }
  68. /**
  69. * Return the current command line verbatim as a string
  70. *
  71. * @return string
  72. */
  73. public function getCommandLine()
  74. {
  75. return $this->commandLine;
  76. }
  77. /**
  78. * Return the word from the command line that the cursor is currently in
  79. *
  80. * Most of the time this will be a partial word. If the cursor has a space before it,
  81. * this will return an empty string, indicating a new word.
  82. *
  83. * @return string
  84. */
  85. public function getCurrentWord()
  86. {
  87. if (isset($this->words[$this->wordIndex])) {
  88. return $this->words[$this->wordIndex];
  89. }
  90. return '';
  91. }
  92. /**
  93. * Return a word by index from the command line
  94. *
  95. * @see $words, $wordBreaks
  96. * @param int $index
  97. * @return string
  98. */
  99. public function getWordAtIndex($index)
  100. {
  101. if (isset($this->words[$index])) {
  102. return $this->words[$index];
  103. }
  104. return '';
  105. }
  106. /**
  107. * Get the contents of the command line, exploded into words based on the configured word break characters
  108. *
  109. * @see $wordBreaks, setWordBreaks
  110. * @return array
  111. */
  112. public function getWords()
  113. {
  114. if ($this->words === null) {
  115. $this->splitCommand();
  116. }
  117. return $this->words;
  118. }
  119. /**
  120. * Get the index of the word the cursor is currently in
  121. *
  122. * @see getWords, getCurrentWord
  123. * @return int
  124. */
  125. public function getWordIndex()
  126. {
  127. if ($this->wordIndex === null) {
  128. $this->splitCommand();
  129. }
  130. return $this->wordIndex;
  131. }
  132. /**
  133. * Get the character index of the user's cursor on the command line
  134. *
  135. * This is in the context of the full command line string, so includes word break characters.
  136. * Note that some shells can only provide an approximation for character index. Under ZSH for
  137. * example, this will always be the character at the start of the current word.
  138. *
  139. * @return int
  140. */
  141. public function getCharIndex()
  142. {
  143. return $this->charIndex;
  144. }
  145. /**
  146. * Set the cursor position as a character index relative to the start of the command line
  147. *
  148. * @param int $index
  149. */
  150. public function setCharIndex($index)
  151. {
  152. $this->charIndex = $index;
  153. $this->reset();
  154. }
  155. /**
  156. * Set characters to use as split points when breaking the command line into words
  157. *
  158. * This defaults to a sane value based on BASH's word break characters and shouldn't
  159. * need to be changed unless your completions contain the default word break characters.
  160. *
  161. * @see wordBreaks
  162. * @param string $charList - a single string containing all of the characters to break words on
  163. */
  164. public function setWordBreaks($charList)
  165. {
  166. $this->wordBreaks = $charList;
  167. $this->reset();
  168. }
  169. /**
  170. * Split the command line into words using the configured word break characters
  171. *
  172. * @return string[]
  173. */
  174. protected function splitCommand()
  175. {
  176. $this->words = array();
  177. $this->wordIndex = null;
  178. $cursor = 0;
  179. $breaks = preg_quote($this->wordBreaks);
  180. if (!preg_match_all("/([^$breaks]*)([$breaks]*)/", $this->commandLine, $matches)) {
  181. return;
  182. }
  183. // Groups:
  184. // 1: Word
  185. // 2: Break characters
  186. foreach ($matches[0] as $index => $wholeMatch) {
  187. // Determine which word the cursor is in
  188. $cursor += strlen($wholeMatch);
  189. $word = $matches[1][$index];
  190. $breaks = $matches[2][$index];
  191. if ($this->wordIndex === null && $cursor >= $this->charIndex) {
  192. $this->wordIndex = $index;
  193. // Find the user's cursor position relative to the end of this word
  194. // The end of the word is the internal cursor minus any break characters that were captured
  195. $cursorWordOffset = $this->charIndex - ($cursor - strlen($breaks));
  196. if ($cursorWordOffset < 0) {
  197. // Cursor is inside the word - truncate the word at the cursor
  198. // (This emulates normal BASH completion behaviour I've observed, though I'm not entirely sure if it's useful)
  199. $word = substr($word, 0, strlen($word) + $cursorWordOffset);
  200. } elseif ($cursorWordOffset > 0) {
  201. // Cursor is in the break-space after a word
  202. // Push an empty word at the cursor to allow completion of new terms at the cursor, ignoring words ahead
  203. $this->wordIndex++;
  204. $this->words[] = $word;
  205. $this->words[] = '';
  206. continue;
  207. }
  208. }
  209. if ($word !== '') {
  210. $this->words[] = $word;
  211. }
  212. }
  213. if ($this->wordIndex > count($this->words) - 1) {
  214. $this->wordIndex = count($this->words) - 1;
  215. }
  216. }
  217. /**
  218. * Reset the computed words so that $this->splitWords is forced to run again
  219. */
  220. protected function reset()
  221. {
  222. $this->words = null;
  223. $this->wordIndex = null;
  224. }
  225. }