1
0
Fork 0

Extend symbol search tests

pull/60/merge
Felix Becker 2016-10-09 15:48:08 +02:00
parent a74bf90d77
commit 1689e4d0dc
3 changed files with 249 additions and 24 deletions

View File

@ -133,6 +133,9 @@ class PhpDocument
if ($symbols === null) { if ($symbols === null) {
return null; return null;
} }
if ($query === '') {
return $symbols;
}
return array_filter($symbols, function($symbol) use ($query) { return array_filter($symbols, function($symbol) use ($query) {
return stripos($symbol->name, $query) !== false; return stripos($symbol->name, $query) !== false;
}); });
@ -316,7 +319,7 @@ class PhpDocument
if ($node instanceof Node\Stmt\ClassLike && !isset($nameNode) && isset($node->name)) { if ($node instanceof Node\Stmt\ClassLike && !isset($nameNode) && isset($node->name)) {
// Class, interface or trait declaration // Class, interface or trait declaration
return (string)$node->namespacedName; return (string)$node->namespacedName;
} else if ($node instanceof Node\Stmt\Function_) { } else if ($node instanceof Node\Stmt\Function_ && !isset($nameNode)) {
// Function: use functionName() as the name // Function: use functionName() as the name
return (string)$node->namespacedName . '()'; return (string)$node->namespacedName . '()';
} else if ($node instanceof Node\Stmt\ClassMethod) { } else if ($node instanceof Node\Stmt\ClassMethod) {

View File

@ -12,12 +12,12 @@ use LanguageServer\NodeVisitor\{ReferencesAdder, DefinitionCollector};
class DefinitionCollectorTest extends TestCase class DefinitionCollectorTest extends TestCase
{ {
public function test() public function testCollectsSymbols()
{ {
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$document = new PhpDocument('whatever', $project, $client, $parser); $document = new PhpDocument('symbols', $project, $client, $parser);
$traverser = new NodeTraverser; $traverser = new NodeTraverser;
$traverser->addVisitor(new NameResolver); $traverser->addVisitor(new NameResolver);
$traverser->addVisitor(new ReferencesAdder($document)); $traverser->addVisitor(new ReferencesAdder($document));
@ -49,4 +49,22 @@ class DefinitionCollectorTest extends TestCase
$this->assertInstanceOf(Node\Stmt\Interface_::class, $defs['TestNamespace\\TestInterface']); $this->assertInstanceOf(Node\Stmt\Interface_::class, $defs['TestNamespace\\TestInterface']);
$this->assertInstanceOf(Node\Stmt\Function_::class, $defs['TestNamespace\\test_function()']); $this->assertInstanceOf(Node\Stmt\Function_::class, $defs['TestNamespace\\test_function()']);
} }
public function testDoesNotCollectReferences()
{
$client = new LanguageClient(new MockProtocolStream());
$project = new Project($client);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$document = new PhpDocument('references', $project, $client, $parser);
$traverser = new NodeTraverser;
$traverser->addVisitor(new NameResolver);
$traverser->addVisitor(new ReferencesAdder($document));
$definitionCollector = new DefinitionCollector;
$traverser->addVisitor($definitionCollector);
$stmts = $parser->parse(file_get_contents(__DIR__ . '/../../fixtures/references.php'));
$traverser->traverse($stmts);
$defs = $definitionCollector->definitions;
$this->assertEquals(['TestNamespace\\whatever()'], array_keys($defs));
$this->assertInstanceOf(Node\Stmt\Function_::class, $defs['TestNamespace\\whatever()']);
}
} }

View File

@ -21,52 +21,256 @@ class SymbolTest extends TestCase
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$this->workspace = new Server\Workspace($project, $client); $this->workspace = new Server\Workspace($project, $client);
$project->getDocument('symbols')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/symbols.php'));
// create two documents $project->getDocument('references')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/references.php'));
$project->getDocument('file:///document1.php')->updateContent("<?php\nfunction foo() {}\nfunction bar() {}\n");
$project->getDocument('file:///document2.php')->updateContent("<?php\nfunction baz() {}\nfunction frob() {}\n");
} }
public function testSymbol() public function testEmptyQueryReturnsAllSymbols()
{ {
// Request symbols // Request symbols
$result = $this->workspace->symbol('f'); $result = $this->workspace->symbol('');
$this->assertEquals([ $this->assertEquals([
[ [
'name' => 'foo', 'name' => 'TEST_CONST',
'kind' => SymbolKind::FUNCTION, 'kind' => SymbolKind::CONSTANT,
'location' => [ 'location' => [
'uri' => 'file:///document1.php', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 1, 'line' => 4,
'character' => 0 'character' => 6
], ],
'end' => [ 'end' => [
'line' => 1, 'line' => 4,
'character' => 17 'character' => 22
] ]
] ]
], ],
'containerName' => null 'containerName' => 'TestNamespace'
], ],
[ [
'name' => 'frob', 'name' => 'TestClass',
'kind' => SymbolKind::FUNCTION, 'kind' => SymbolKind::CLASS_,
'location' => [ 'location' => [
'uri' => 'file:///document2.php', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 2, 'line' => 6,
'character' => 0 'character' => 0
], ],
'end' => [ 'end' => [
'line' => 2, 'line' => 21,
'character' => 18 'character' => 1
] ]
] ]
], ],
'containerName' => null 'containerName' => 'TestNamespace'
],
[
'name' => 'TEST_CLASS_CONST',
'kind' => SymbolKind::CONSTANT,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 8,
'character' => 10
],
'end' => [
'line' => 8,
'character' => 32
]
]
],
'containerName' => 'TestNamespace\\TestClass'
],
[
'name' => 'staticTestProperty',
'kind' => SymbolKind::PROPERTY,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 9,
'character' => 18
],
'end' => [
'line' => 9,
'character' => 37
]
]
],
'containerName' => 'TestNamespace\\TestClass'
],
[
'name' => 'testProperty',
'kind' => SymbolKind::PROPERTY,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 10,
'character' => 11
],
'end' => [
'line' => 10,
'character' => 24
]
]
],
'containerName' => 'TestNamespace\\TestClass'
],
[
'name' => 'staticTestMethod',
'kind' => SymbolKind::METHOD,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 12,
'character' => 4
],
'end' => [
'line' => 15,
'character' => 5
]
]
],
'containerName' => 'TestNamespace\\TestClass'
],
[
'name' => 'testMethod',
'kind' => SymbolKind::METHOD,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 17,
'character' => 4
],
'end' => [
'line' => 20,
'character' => 5
]
]
],
'containerName' => 'TestNamespace\\TestClass'
],
[
'name' => 'TestTrait',
'kind' => SymbolKind::CLASS_,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 23,
'character' => 0
],
'end' => [
'line' => 26,
'character' => 1
]
]
],
'containerName' => 'TestNamespace'
],
[
'name' => 'TestInterface',
'kind' => SymbolKind::INTERFACE,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 28,
'character' => 0
],
'end' => [
'line' => 31,
'character' => 1
]
]
],
'containerName' => 'TestNamespace'
],
[
'name' => 'test_function',
'kind' => SymbolKind::FUNCTION,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 33,
'character' => 0
],
'end' => [
'line' => 36,
'character' => 1
]
]
],
'containerName' => 'TestNamespace'
],
[
'name' => 'whatever',
'kind' => SymbolKind::FUNCTION,
'location' => [
'uri' => 'references',
'range' => [
'start' => [
'line' => 15,
'character' => 0
],
'end' => [
'line' => 17,
'character' => 1
]
]
],
'containerName' => 'TestNamespace'
]
], json_decode(json_encode($result), true));
}
public function testQueryFiltersResults()
{
// Request symbols
$result = $this->workspace->symbol('testmethod');
$this->assertEquals([
[
'name' => 'staticTestMethod',
'kind' => SymbolKind::METHOD,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 12,
'character' => 4
],
'end' => [
'line' => 15,
'character' => 5
]
]
],
'containerName' => 'TestNamespace\\TestClass'
],
[
'name' => 'testMethod',
'kind' => SymbolKind::METHOD,
'location' => [
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 17,
'character' => 4
],
'end' => [
'line' => 20,
'character' => 5
]
]
],
'containerName' => 'TestNamespace\\TestClass'
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
} }