| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of Psy Shell.
|
|
|
5 |
*
|
|
|
6 |
* (c) 2012-2022 Justin Hileman
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Psy\TabCompletion\Matcher;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* A class name tab completion Matcher.
|
|
|
16 |
*
|
|
|
17 |
* This matcher provides completion for all declared classes.
|
|
|
18 |
*
|
|
|
19 |
* @author Marc Garcia <markcial@gmail.com>
|
|
|
20 |
*/
|
|
|
21 |
class ClassNamesMatcher extends AbstractMatcher
|
|
|
22 |
{
|
|
|
23 |
/**
|
|
|
24 |
* {@inheritdoc}
|
|
|
25 |
*/
|
|
|
26 |
public function getMatches(array $tokens, array $info = []): array
|
|
|
27 |
{
|
|
|
28 |
$class = $this->getNamespaceAndClass($tokens);
|
|
|
29 |
if ($class !== '' && $class[0] === '\\') {
|
|
|
30 |
$class = \substr($class, 1, \strlen($class));
|
|
|
31 |
}
|
|
|
32 |
$quotedClass = \preg_quote($class);
|
|
|
33 |
|
|
|
34 |
return \array_map(
|
|
|
35 |
function ($className) use ($class) {
|
|
|
36 |
// get the number of namespace separators
|
|
|
37 |
$nsPos = \substr_count($class, '\\');
|
|
|
38 |
$pieces = \explode('\\', $className);
|
|
|
39 |
// $methods = Mirror::get($class);
|
|
|
40 |
return \implode('\\', \array_slice($pieces, $nsPos, \count($pieces)));
|
|
|
41 |
},
|
|
|
42 |
\array_filter(
|
|
|
43 |
\array_merge(\get_declared_classes(), \get_declared_interfaces()),
|
|
|
44 |
function ($className) use ($quotedClass) {
|
|
|
45 |
return AbstractMatcher::startsWith($quotedClass, $className);
|
|
|
46 |
}
|
|
|
47 |
)
|
|
|
48 |
);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* {@inheritdoc}
|
|
|
53 |
*/
|
|
|
54 |
public function hasMatched(array $tokens): bool
|
|
|
55 |
{
|
|
|
56 |
$token = \array_pop($tokens);
|
|
|
57 |
$prevToken = \array_pop($tokens);
|
|
|
58 |
|
|
|
59 |
$ignoredTokens = [
|
|
|
60 |
self::T_INCLUDE, self::T_INCLUDE_ONCE, self::T_REQUIRE, self::T_REQUIRE_ONCE,
|
|
|
61 |
];
|
|
|
62 |
|
|
|
63 |
switch (true) {
|
|
|
64 |
case self::hasToken([$ignoredTokens], $token):
|
|
|
65 |
case self::hasToken([$ignoredTokens], $prevToken):
|
|
|
66 |
case \is_string($token) && $token === '$':
|
|
|
67 |
return false;
|
|
|
68 |
case self::hasToken([self::T_NEW, self::T_OPEN_TAG, self::T_NS_SEPARATOR, self::T_STRING], $prevToken):
|
|
|
69 |
case self::hasToken([self::T_NEW, self::T_OPEN_TAG, self::T_NS_SEPARATOR], $token):
|
|
|
70 |
case self::hasToken([self::T_OPEN_TAG, self::T_VARIABLE], $token):
|
|
|
71 |
case self::isOperator($token):
|
|
|
72 |
return true;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
return false;
|
|
|
76 |
}
|
|
|
77 |
}
|