Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\TabCompletion;
|
Chris@13
|
13
|
Chris@13
|
14 use Psy\TabCompletion\Matcher\AbstractMatcher;
|
Chris@13
|
15
|
Chris@13
|
16 /**
|
Chris@13
|
17 * A readline tab completion service.
|
Chris@13
|
18 *
|
Chris@13
|
19 * @author Marc Garcia <markcial@gmail.com>
|
Chris@13
|
20 */
|
Chris@13
|
21 class AutoCompleter
|
Chris@13
|
22 {
|
Chris@13
|
23 /** @var Matcher\AbstractMatcher[] */
|
Chris@13
|
24 protected $matchers;
|
Chris@13
|
25
|
Chris@13
|
26 /**
|
Chris@13
|
27 * Register a tab completion Matcher.
|
Chris@13
|
28 *
|
Chris@13
|
29 * @param AbstractMatcher $matcher
|
Chris@13
|
30 */
|
Chris@13
|
31 public function addMatcher(AbstractMatcher $matcher)
|
Chris@13
|
32 {
|
Chris@13
|
33 $this->matchers[] = $matcher;
|
Chris@13
|
34 }
|
Chris@13
|
35
|
Chris@13
|
36 /**
|
Chris@13
|
37 * Activate readline tab completion.
|
Chris@13
|
38 */
|
Chris@13
|
39 public function activate()
|
Chris@13
|
40 {
|
Chris@17
|
41 \readline_completion_function([&$this, 'callback']);
|
Chris@13
|
42 }
|
Chris@13
|
43
|
Chris@13
|
44 /**
|
Chris@13
|
45 * Handle readline completion.
|
Chris@13
|
46 *
|
Chris@13
|
47 * @param string $input Readline current word
|
Chris@13
|
48 * @param int $index Current word index
|
Chris@13
|
49 * @param array $info readline_info() data
|
Chris@13
|
50 *
|
Chris@13
|
51 * @return array
|
Chris@13
|
52 */
|
Chris@13
|
53 public function processCallback($input, $index, $info = [])
|
Chris@13
|
54 {
|
Chris@13
|
55 // Some (Windows?) systems provide incomplete `readline_info`, so let's
|
Chris@13
|
56 // try to work around it.
|
Chris@13
|
57 $line = $info['line_buffer'];
|
Chris@13
|
58 if (isset($info['end'])) {
|
Chris@17
|
59 $line = \substr($line, 0, $info['end']);
|
Chris@13
|
60 }
|
Chris@13
|
61 if ($line === '' && $input !== '') {
|
Chris@13
|
62 $line = $input;
|
Chris@13
|
63 }
|
Chris@13
|
64
|
Chris@17
|
65 $tokens = \token_get_all('<?php ' . $line);
|
Chris@13
|
66
|
Chris@13
|
67 // remove whitespaces
|
Chris@17
|
68 $tokens = \array_filter($tokens, function ($token) {
|
Chris@13
|
69 return !AbstractMatcher::tokenIs($token, AbstractMatcher::T_WHITESPACE);
|
Chris@13
|
70 });
|
Chris@13
|
71
|
Chris@13
|
72 $matches = [];
|
Chris@13
|
73 foreach ($this->matchers as $matcher) {
|
Chris@13
|
74 if ($matcher->hasMatched($tokens)) {
|
Chris@17
|
75 $matches = \array_merge($matcher->getMatches($tokens), $matches);
|
Chris@13
|
76 }
|
Chris@13
|
77 }
|
Chris@13
|
78
|
Chris@17
|
79 $matches = \array_unique($matches);
|
Chris@13
|
80
|
Chris@13
|
81 return !empty($matches) ? $matches : [''];
|
Chris@13
|
82 }
|
Chris@13
|
83
|
Chris@13
|
84 /**
|
Chris@13
|
85 * The readline_completion_function callback handler.
|
Chris@13
|
86 *
|
Chris@13
|
87 * @see processCallback
|
Chris@13
|
88 *
|
Chris@13
|
89 * @param string $input
|
Chris@13
|
90 * @param int $index
|
Chris@13
|
91 *
|
Chris@13
|
92 * @return array
|
Chris@13
|
93 */
|
Chris@13
|
94 public function callback($input, $index)
|
Chris@13
|
95 {
|
Chris@17
|
96 return $this->processCallback($input, $index, \readline_info());
|
Chris@13
|
97 }
|
Chris@13
|
98
|
Chris@13
|
99 /**
|
Chris@13
|
100 * Remove readline callback handler on destruct.
|
Chris@13
|
101 */
|
Chris@13
|
102 public function __destruct()
|
Chris@13
|
103 {
|
Chris@13
|
104 // PHP didn't implement the whole readline API when they first switched
|
Chris@13
|
105 // to libedit. And they still haven't.
|
Chris@17
|
106 if (\function_exists('readline_callback_handler_remove')) {
|
Chris@17
|
107 \readline_callback_handler_remove();
|
Chris@13
|
108 }
|
Chris@13
|
109 }
|
Chris@13
|
110 }
|