4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\TabCompletion;
14 use Psy\TabCompletion\Matcher\AbstractMatcher;
17 * A readline tab completion service.
19 * @author Marc Garcia <markcial@gmail.com>
23 /** @var Matcher\AbstractMatcher[] */
27 * Register a tab completion Matcher.
29 * @param AbstractMatcher $matcher
31 public function addMatcher(AbstractMatcher $matcher)
33 $this->matchers[] = $matcher;
37 * Activate readline tab completion.
39 public function activate()
41 readline_completion_function(array(&$this, 'callback'));
45 * Handle readline completion.
47 * @param string $input Readline current word
48 * @param int $index Current word index
49 * @param array $info readline_info() data
53 public function processCallback($input, $index, $info = array())
55 // Some (Windows?) systems provide incomplete `readline_info`, so let's
56 // try to work around it.
57 $line = $info['line_buffer'];
58 if (isset($info['end'])) {
59 $line = substr($line, 0, $info['end']);
61 if ($line === '' && $input !== '') {
65 $tokens = token_get_all('<?php ' . $line);
68 $tokens = array_filter($tokens, function ($token) {
69 return !AbstractMatcher::tokenIs($token, AbstractMatcher::T_WHITESPACE);
73 foreach ($this->matchers as $matcher) {
74 if ($matcher->hasMatched($tokens)) {
75 $matches = array_merge($matcher->getMatches($tokens), $matches);
79 $matches = array_unique($matches);
81 return !empty($matches) ? $matches : array('');
85 * The readline_completion_function callback handler.
87 * @see processCallback
94 public function callback($input, $index)
96 return $this->processCallback($input, $index, readline_info());
100 * Remove readline callback handler on destruct.
102 public function __destruct()
104 // PHP didn't implement the whole readline API when they first switched
105 // to libedit. And they still haven't.
107 // So this is a thing to make PsySH work on 5.3.x:
108 if (function_exists('readline_callback_handler_remove')) {
109 readline_callback_handler_remove();