From 07bfdff72da7d091e26cd8b507ebeca35861ed3b Mon Sep 17 00:00:00 2001 From: jens1o Date: Mon, 10 Apr 2017 12:24:22 +0200 Subject: [PATCH 01/16] :rocket: support constants --- .../completion/constant_with_namespace.php | 46 +++++++++++++++++++ fixtures/global_symbols.php | 7 +++ src/DefinitionResolver.php | 7 ++- src/Index/Index.php | 2 +- src/Protocol/SymbolInformation.php | 15 ++++-- tests/LanguageServerTest.php | 4 +- tests/Server/ServerTestCase.php | 28 ++++++----- tests/Server/TextDocument/HoverTest.php | 14 ++++++ tests/Server/Workspace/SymbolTest.php | 3 +- 9 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 fixtures/completion/constant_with_namespace.php diff --git a/fixtures/completion/constant_with_namespace.php b/fixtures/completion/constant_with_namespace.php new file mode 100644 index 0000000..b31d25c --- /dev/null +++ b/fixtures/completion/constant_with_namespace.php @@ -0,0 +1,46 @@ +namespacedName . '::' . $node->name; } - } + } else if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && strtolower((string)$node->name) === 'define') { + if (!isset($node->args[0]) || !($node->args[0]->value instanceof Node\Scalar\String_)) { + return null; + } + return (string)$node->args[0]->value->value; + } } } diff --git a/src/Index/Index.php b/src/Index/Index.php index 5c24813..b753476 100644 --- a/src/Index/Index.php +++ b/src/Index/Index.php @@ -117,7 +117,7 @@ class Index implements ReadableIndex, \Serializable * Registers a definition * * @param string $fqn The fully qualified name of the symbol - * @param string $definition The Definition object + * @param Definition $definition The Definition object * @return void */ public function setDefinition(string $fqn, Definition $definition) diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index 299dc55..a2ab964 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -50,9 +50,18 @@ class SymbolInformation { $parent = $node->getAttribute('parentNode'); $symbol = new self; - if ($node instanceof Node\Stmt\Class_) { - $symbol->kind = SymbolKind::CLASS_; - } else if ($node instanceof Node\Stmt\Trait_) { + if ( + $node instanceof Node\Expr\FuncCall + && $node->name instanceof Node\Name + && strtolower((string)$node->name) === 'define' + && isset($node->args[0]) + && $node->args[0]->value instanceof Node\Scalar\String_ + ) { + // constants with define() like + // define('TEST_PROPERTY', true); + $symbol->kind = SymbolKind::CONSTANT; + $symbol->name = (string)$node->args[0]->value->value; + } else if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_) { $symbol->kind = SymbolKind::CLASS_; } else if ($node instanceof Node\Stmt\Interface_) { $symbol->kind = SymbolKind::INTERFACE; diff --git a/tests/LanguageServerTest.php b/tests/LanguageServerTest.php index c32a76a..fb52ef6 100644 --- a/tests/LanguageServerTest.php +++ b/tests/LanguageServerTest.php @@ -57,7 +57,7 @@ class LanguageServerTest extends TestCase if ($msg->body->method === 'window/logMessage' && $promise->state === Promise::PENDING) { if ($msg->body->params->type === MessageType::ERROR) { $promise->reject(new Exception($msg->body->params->message)); - } else if (strpos($msg->body->params->message, 'All 26 PHP files parsed') !== false) { + } else if (strpos($msg->body->params->message, 'All 27 PHP files parsed') !== false) { $promise->fulfill(); } } @@ -103,7 +103,7 @@ class LanguageServerTest extends TestCase if ($promise->state === Promise::PENDING) { $promise->reject(new Exception($msg->body->params->message)); } - } else if (strpos($msg->body->params->message, 'All 26 PHP files parsed') !== false) { + } else if (strpos($msg->body->params->message, 'All 27 PHP files parsed') !== false) { $promise->fulfill(); } } diff --git a/tests/Server/ServerTestCase.php b/tests/Server/ServerTestCase.php index f5fec55..127f0e2 100644 --- a/tests/Server/ServerTestCase.php +++ b/tests/Server/ServerTestCase.php @@ -72,18 +72,19 @@ abstract class ServerTestCase extends TestCase $this->definitionLocations = [ // Global - 'TEST_CONST' => new Location($globalSymbolsUri, new Range(new Position( 9, 6), new Position( 9, 22))), - 'TestClass' => new Location($globalSymbolsUri, new Range(new Position(20, 0), new Position(61, 1))), - 'ChildClass' => new Location($globalSymbolsUri, new Range(new Position(99, 0), new Position(99, 37))), - 'TestTrait' => new Location($globalSymbolsUri, new Range(new Position(63, 0), new Position(66, 1))), - 'TestInterface' => new Location($globalSymbolsUri, new Range(new Position(68, 0), new Position(71, 1))), - 'TestClass::TEST_CLASS_CONST' => new Location($globalSymbolsUri, new Range(new Position(27, 10), new Position(27, 32))), - 'TestClass::testProperty' => new Location($globalSymbolsUri, new Range(new Position(41, 11), new Position(41, 24))), - 'TestClass::staticTestProperty' => new Location($globalSymbolsUri, new Range(new Position(34, 18), new Position(34, 37))), - 'TestClass::staticTestMethod()' => new Location($globalSymbolsUri, new Range(new Position(46, 4), new Position(49, 5))), - 'TestClass::testMethod()' => new Location($globalSymbolsUri, new Range(new Position(57, 4), new Position(60, 5))), - 'test_function()' => new Location($globalSymbolsUri, new Range(new Position(78, 0), new Position(81, 1))), - 'whatever()' => new Location($globalReferencesUri, new Range(new Position(21, 0), new Position(23, 1))), + 'TEST_PROPERTY' => new Location($globalSymbolsUri, new Range(new Position(104, 0), new Position(104, 30))), + 'TEST_CONST' => new Location($globalSymbolsUri, new Range(new Position( 9, 6), new Position( 9, 22))), + 'TestClass' => new Location($globalSymbolsUri, new Range(new Position(20, 0), new Position(61, 1))), + 'ChildClass' => new Location($globalSymbolsUri, new Range(new Position(99, 0), new Position(99, 37))), + 'TestTrait' => new Location($globalSymbolsUri, new Range(new Position(63, 0), new Position(66, 1))), + 'TestInterface' => new Location($globalSymbolsUri, new Range(new Position(68, 0), new Position(71, 1))), + 'TestClass::TEST_CLASS_CONST' => new Location($globalSymbolsUri, new Range(new Position(27, 10), new Position(27, 32))), + 'TestClass::testProperty' => new Location($globalSymbolsUri, new Range(new Position(41, 11), new Position(41, 24))), + 'TestClass::staticTestProperty' => new Location($globalSymbolsUri, new Range(new Position(34, 18), new Position(34, 37))), + 'TestClass::staticTestMethod()' => new Location($globalSymbolsUri, new Range(new Position(46, 4), new Position(49, 5))), + 'TestClass::testMethod()' => new Location($globalSymbolsUri, new Range(new Position(57, 4), new Position(60, 5))), + 'test_function()' => new Location($globalSymbolsUri, new Range(new Position(78, 0), new Position(81, 1))), + 'whatever()' => new Location($globalReferencesUri, new Range(new Position(21, 0), new Position(23, 1))), // Namespaced 'TestNamespace' => new Location($symbolsUri, new Range(new Position( 2, 10), new Position( 2, 23))), @@ -163,6 +164,9 @@ abstract class ServerTestCase extends TestCase ], // Global + 'TEST_PROPERTY' => [ + 0 => new Location($globalSymbolsUri, new Range(new Position(106, 6), new Position(106, 19))) + ], 'TEST_CONST' => [ 0 => new Location($referencesUri, new Range(new Position(29, 5), new Position(29, 15))), 1 => new Location($globalReferencesUri, new Range(new Position(29, 5), new Position(29, 15))) diff --git a/tests/Server/TextDocument/HoverTest.php b/tests/Server/TextDocument/HoverTest.php index cdc1718..628aab5 100644 --- a/tests/Server/TextDocument/HoverTest.php +++ b/tests/Server/TextDocument/HoverTest.php @@ -156,6 +156,20 @@ class HoverTest extends ServerTestCase ], $reference->range), $result); } + public function testHoverForGlobalConstant() { + // print TEST_PROPERTY ? 'true' : 'false'; + // Get hover for TEST_PROPERTY + $reference = $this->getReferenceLocations('TEST_PROPERTY')[0]; + $result = $this->textDocument->hover( + new TextDocumentIdentifier($reference->uri), + $reference->range->end + )->wait(); + $this->assertEquals(new Hover([ + new MarkedString('php', "range), $result); + } + public function testHoverForVariable() { // echo $var; diff --git a/tests/Server/Workspace/SymbolTest.php b/tests/Server/Workspace/SymbolTest.php index 5d0ed34..733acf4 100644 --- a/tests/Server/Workspace/SymbolTest.php +++ b/tests/Server/Workspace/SymbolTest.php @@ -58,9 +58,10 @@ class SymbolTest extends ServerTestCase new SymbolInformation('TestInterface', SymbolKind::INTERFACE, $this->getDefinitionLocation('TestInterface'), ''), new SymbolInformation('test_function', SymbolKind::FUNCTION, $this->getDefinitionLocation('test_function()'), ''), new SymbolInformation('ChildClass', SymbolKind::CLASS_, $this->getDefinitionLocation('ChildClass'), ''), + new SymbolInformation('define', SymbolKind::CONSTANT, $this->getDefinitionLocation('TEST_PROPERTY'), ''), new SymbolInformation('whatever', SymbolKind::FUNCTION, $this->getDefinitionLocation('whatever()'), ''), - new SymbolInformation('SecondTestNamespace', SymbolKind::NAMESPACE, $this->getDefinitionLocation('SecondTestNamespace'), '') + new SymbolInformation('SecondTestNamespace', SymbolKind::NAMESPACE, $this->getDefinitionLocation('SecondTestNamespace'), '') ], $result); // @codingStandardsIgnoreEnd } From 9ff8957f479b3b8995fbce5318c39f0e04f06bb0 Mon Sep 17 00:00:00 2001 From: jens1o Date: Mon, 10 Apr 2017 12:27:59 +0200 Subject: [PATCH 02/16] fix double definiton --- .../completion/constant_with_namespace.php | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/fixtures/completion/constant_with_namespace.php b/fixtures/completion/constant_with_namespace.php index b31d25c..cade11f 100644 --- a/fixtures/completion/constant_with_namespace.php +++ b/fixtures/completion/constant_with_namespace.php @@ -21,26 +21,3 @@ namespace { HELLO\world(); } - Date: Mon, 10 Apr 2017 12:33:33 +0200 Subject: [PATCH 03/16] fix code style --- src/DefinitionResolver.php | 2 +- tests/Server/TextDocument/HoverTest.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 9376ced..bbf9076 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -887,6 +887,6 @@ class DefinitionResolver return null; } return (string)$node->args[0]->value->value; - } + } } } diff --git a/tests/Server/TextDocument/HoverTest.php b/tests/Server/TextDocument/HoverTest.php index 628aab5..9815437 100644 --- a/tests/Server/TextDocument/HoverTest.php +++ b/tests/Server/TextDocument/HoverTest.php @@ -156,7 +156,8 @@ class HoverTest extends ServerTestCase ], $reference->range), $result); } - public function testHoverForGlobalConstant() { + public function testHoverForGlobalConstant() + { // print TEST_PROPERTY ? 'true' : 'false'; // Get hover for TEST_PROPERTY $reference = $this->getReferenceLocations('TEST_PROPERTY')[0]; From 76e7170f154c04b1fcea4d35bb863e70712a0bd9 Mon Sep 17 00:00:00 2001 From: jens1o Date: Mon, 17 Apr 2017 12:15:46 +0200 Subject: [PATCH 04/16] rename test constant and fix name gets renamed bug --- fixtures/global_symbols.php | 4 +- src/Protocol/SymbolInformation.php | 11 ++++- tests/Server/ServerTestCase.php | 6 +-- tests/Server/TextDocument/HoverTest.php | 8 ++-- tests/Server/Workspace/SymbolTest.php | 60 ++++++++++++------------- 5 files changed, 48 insertions(+), 41 deletions(-) diff --git a/fixtures/global_symbols.php b/fixtures/global_symbols.php index 11f78bd..ac93b68 100644 --- a/fixtures/global_symbols.php +++ b/fixtures/global_symbols.php @@ -102,6 +102,6 @@ class ChildClass extends TestClass {} /** * Lorem ipsum dolor sit amet, consectetur. */ -define('TEST_PROPERTY', false); +define('TEST_DEFINE_CONSTANT', false); -print TEST_PROPERTY ? 'true' : 'false'; +print TEST_DEFINE_CONSTANT ? 'true' : 'false'; diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index a2ab964..7ee34d0 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -58,9 +58,8 @@ class SymbolInformation && $node->args[0]->value instanceof Node\Scalar\String_ ) { // constants with define() like - // define('TEST_PROPERTY', true); + // define('TEST_DEFINE_CONSTANT', false); $symbol->kind = SymbolKind::CONSTANT; - $symbol->name = (string)$node->args[0]->value->value; } else if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_) { $symbol->kind = SymbolKind::CLASS_; } else if ($node instanceof Node\Stmt\Interface_) { @@ -91,6 +90,14 @@ class SymbolInformation } if ($node instanceof Node\Name) { $symbol->name = (string)$node; + } else if( + $node instanceof Node\Expr\FuncCall + && $node->name instanceof Node\Name + && strtolower((string)$node->name) === 'define' + && isset($node->args[0]) + && $node->args[0]->value instanceof Node\Scalar\String_ + ) { + $symbol->name = (string)$node->args[0]->value->value; } else if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignOp) { $symbol->name = $node->var->name; } else if ($node instanceof Node\Expr\ClosureUse) { diff --git a/tests/Server/ServerTestCase.php b/tests/Server/ServerTestCase.php index 127f0e2..1154216 100644 --- a/tests/Server/ServerTestCase.php +++ b/tests/Server/ServerTestCase.php @@ -72,7 +72,7 @@ abstract class ServerTestCase extends TestCase $this->definitionLocations = [ // Global - 'TEST_PROPERTY' => new Location($globalSymbolsUri, new Range(new Position(104, 0), new Position(104, 30))), + 'TEST_DEFINE_CONSTANT' => new Location($globalSymbolsUri, new Range(new Position(104, 0), new Position(104, 37))), 'TEST_CONST' => new Location($globalSymbolsUri, new Range(new Position( 9, 6), new Position( 9, 22))), 'TestClass' => new Location($globalSymbolsUri, new Range(new Position(20, 0), new Position(61, 1))), 'ChildClass' => new Location($globalSymbolsUri, new Range(new Position(99, 0), new Position(99, 37))), @@ -164,8 +164,8 @@ abstract class ServerTestCase extends TestCase ], // Global - 'TEST_PROPERTY' => [ - 0 => new Location($globalSymbolsUri, new Range(new Position(106, 6), new Position(106, 19))) + 'TEST_DEFINE_CONSTANT' => [ + 0 => new Location($globalSymbolsUri, new Range(new Position(106, 6), new Position(106, 26))) ], 'TEST_CONST' => [ 0 => new Location($referencesUri, new Range(new Position(29, 5), new Position(29, 15))), diff --git a/tests/Server/TextDocument/HoverTest.php b/tests/Server/TextDocument/HoverTest.php index 9815437..4da707a 100644 --- a/tests/Server/TextDocument/HoverTest.php +++ b/tests/Server/TextDocument/HoverTest.php @@ -158,15 +158,15 @@ class HoverTest extends ServerTestCase public function testHoverForGlobalConstant() { - // print TEST_PROPERTY ? 'true' : 'false'; - // Get hover for TEST_PROPERTY - $reference = $this->getReferenceLocations('TEST_PROPERTY')[0]; + // print TEST_DEFINE_CONSTANT ? 'true' : 'false'; + // Get hover for TEST_DEFINE_CONSTANT + $reference = $this->getReferenceLocations('TEST_DEFINE_CONSTANT')[0]; $result = $this->textDocument->hover( new TextDocumentIdentifier($reference->uri), $reference->range->end )->wait(); $this->assertEquals(new Hover([ - new MarkedString('php', "range), $result); } diff --git a/tests/Server/Workspace/SymbolTest.php b/tests/Server/Workspace/SymbolTest.php index 733acf4..65ce804 100644 --- a/tests/Server/Workspace/SymbolTest.php +++ b/tests/Server/Workspace/SymbolTest.php @@ -29,39 +29,39 @@ class SymbolTest extends ServerTestCase $referencesUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/references.php')); // @codingStandardsIgnoreStart $this->assertEquals([ - new SymbolInformation('TestNamespace', SymbolKind::NAMESPACE, new Location($referencesUri, new Range(new Position(2, 10), new Position(2, 23))), ''), + new SymbolInformation('TestNamespace', SymbolKind::NAMESPACE, new Location($referencesUri, new Range(new Position(2, 10), new Position(2, 23))), ''), // Namespaced - new SymbolInformation('TEST_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TestNamespace\\TEST_CONST'), 'TestNamespace'), - new SymbolInformation('TestClass', SymbolKind::CLASS_, $this->getDefinitionLocation('TestNamespace\\TestClass'), 'TestNamespace'), - new SymbolInformation('TEST_CLASS_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TestNamespace\\TestClass::TEST_CLASS_CONST'), 'TestNamespace\\TestClass'), - new SymbolInformation('staticTestProperty', SymbolKind::PROPERTY, $this->getDefinitionLocation('TestNamespace\\TestClass::staticTestProperty'), 'TestNamespace\\TestClass'), - new SymbolInformation('testProperty', SymbolKind::PROPERTY, $this->getDefinitionLocation('TestNamespace\\TestClass::testProperty'), 'TestNamespace\\TestClass'), - new SymbolInformation('staticTestMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestNamespace\\TestClass::staticTestMethod()'), 'TestNamespace\\TestClass'), - new SymbolInformation('testMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestNamespace\\TestClass::testMethod()'), 'TestNamespace\\TestClass'), - new SymbolInformation('TestTrait', SymbolKind::CLASS_, $this->getDefinitionLocation('TestNamespace\\TestTrait'), 'TestNamespace'), - new SymbolInformation('TestInterface', SymbolKind::INTERFACE, $this->getDefinitionLocation('TestNamespace\\TestInterface'), 'TestNamespace'), - new SymbolInformation('test_function', SymbolKind::FUNCTION, $this->getDefinitionLocation('TestNamespace\\test_function()'), 'TestNamespace'), - new SymbolInformation('ChildClass', SymbolKind::CLASS_, $this->getDefinitionLocation('TestNamespace\\ChildClass'), 'TestNamespace'), - new SymbolInformation('Example', SymbolKind::CLASS_, $this->getDefinitionLocation('TestNamespace\\Example'), 'TestNamespace'), - new SymbolInformation('__construct', SymbolKind::CONSTRUCTOR, $this->getDefinitionLocation('TestNamespace\\Example::__construct'), 'TestNamespace\\Example'), - new SymbolInformation('__destruct', SymbolKind::CONSTRUCTOR, $this->getDefinitionLocation('TestNamespace\\Example::__destruct'), 'TestNamespace\\Example'), - new SymbolInformation('whatever', SymbolKind::FUNCTION, $this->getDefinitionLocation('TestNamespace\\whatever()'), 'TestNamespace'), + new SymbolInformation('TEST_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TestNamespace\\TEST_CONST'), 'TestNamespace'), + new SymbolInformation('TestClass', SymbolKind::CLASS_, $this->getDefinitionLocation('TestNamespace\\TestClass'), 'TestNamespace'), + new SymbolInformation('TEST_CLASS_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TestNamespace\\TestClass::TEST_CLASS_CONST'), 'TestNamespace\\TestClass'), + new SymbolInformation('staticTestProperty', SymbolKind::PROPERTY, $this->getDefinitionLocation('TestNamespace\\TestClass::staticTestProperty'), 'TestNamespace\\TestClass'), + new SymbolInformation('testProperty', SymbolKind::PROPERTY, $this->getDefinitionLocation('TestNamespace\\TestClass::testProperty'), 'TestNamespace\\TestClass'), + new SymbolInformation('staticTestMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestNamespace\\TestClass::staticTestMethod()'), 'TestNamespace\\TestClass'), + new SymbolInformation('testMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestNamespace\\TestClass::testMethod()'), 'TestNamespace\\TestClass'), + new SymbolInformation('TestTrait', SymbolKind::CLASS_, $this->getDefinitionLocation('TestNamespace\\TestTrait'), 'TestNamespace'), + new SymbolInformation('TestInterface', SymbolKind::INTERFACE, $this->getDefinitionLocation('TestNamespace\\TestInterface'), 'TestNamespace'), + new SymbolInformation('test_function', SymbolKind::FUNCTION, $this->getDefinitionLocation('TestNamespace\\test_function()'), 'TestNamespace'), + new SymbolInformation('ChildClass', SymbolKind::CLASS_, $this->getDefinitionLocation('TestNamespace\\ChildClass'), 'TestNamespace'), + new SymbolInformation('Example', SymbolKind::CLASS_, $this->getDefinitionLocation('TestNamespace\\Example'), 'TestNamespace'), + new SymbolInformation('__construct', SymbolKind::CONSTRUCTOR, $this->getDefinitionLocation('TestNamespace\\Example::__construct'), 'TestNamespace\\Example'), + new SymbolInformation('__destruct', SymbolKind::CONSTRUCTOR, $this->getDefinitionLocation('TestNamespace\\Example::__destruct'), 'TestNamespace\\Example'), + new SymbolInformation('whatever', SymbolKind::FUNCTION, $this->getDefinitionLocation('TestNamespace\\whatever()'), 'TestNamespace'), // Global - new SymbolInformation('TEST_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TEST_CONST'), ''), - new SymbolInformation('TestClass', SymbolKind::CLASS_, $this->getDefinitionLocation('TestClass'), ''), - new SymbolInformation('TEST_CLASS_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TestClass::TEST_CLASS_CONST'), 'TestClass'), - new SymbolInformation('staticTestProperty', SymbolKind::PROPERTY, $this->getDefinitionLocation('TestClass::staticTestProperty'), 'TestClass'), - new SymbolInformation('testProperty', SymbolKind::PROPERTY, $this->getDefinitionLocation('TestClass::testProperty'), 'TestClass'), - new SymbolInformation('staticTestMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestClass::staticTestMethod()'), 'TestClass'), - new SymbolInformation('testMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestClass::testMethod()'), 'TestClass'), - new SymbolInformation('TestTrait', SymbolKind::CLASS_, $this->getDefinitionLocation('TestTrait'), ''), - new SymbolInformation('TestInterface', SymbolKind::INTERFACE, $this->getDefinitionLocation('TestInterface'), ''), - new SymbolInformation('test_function', SymbolKind::FUNCTION, $this->getDefinitionLocation('test_function()'), ''), - new SymbolInformation('ChildClass', SymbolKind::CLASS_, $this->getDefinitionLocation('ChildClass'), ''), - new SymbolInformation('define', SymbolKind::CONSTANT, $this->getDefinitionLocation('TEST_PROPERTY'), ''), - new SymbolInformation('whatever', SymbolKind::FUNCTION, $this->getDefinitionLocation('whatever()'), ''), + new SymbolInformation('TEST_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TEST_CONST'), ''), + new SymbolInformation('TestClass', SymbolKind::CLASS_, $this->getDefinitionLocation('TestClass'), ''), + new SymbolInformation('TEST_CLASS_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TestClass::TEST_CLASS_CONST'), 'TestClass'), + new SymbolInformation('staticTestProperty', SymbolKind::PROPERTY, $this->getDefinitionLocation('TestClass::staticTestProperty'), 'TestClass'), + new SymbolInformation('testProperty', SymbolKind::PROPERTY, $this->getDefinitionLocation('TestClass::testProperty'), 'TestClass'), + new SymbolInformation('staticTestMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestClass::staticTestMethod()'), 'TestClass'), + new SymbolInformation('testMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestClass::testMethod()'), 'TestClass'), + new SymbolInformation('TestTrait', SymbolKind::CLASS_, $this->getDefinitionLocation('TestTrait'), ''), + new SymbolInformation('TestInterface', SymbolKind::INTERFACE, $this->getDefinitionLocation('TestInterface'), ''), + new SymbolInformation('test_function', SymbolKind::FUNCTION, $this->getDefinitionLocation('test_function()'), ''), + new SymbolInformation('ChildClass', SymbolKind::CLASS_, $this->getDefinitionLocation('ChildClass'), ''), + new SymbolInformation('TEST_DEFINE_CONSTANT', SymbolKind::CONSTANT, $this->getDefinitionLocation('TEST_DEFINE_CONSTANT'), ''), + new SymbolInformation('whatever', SymbolKind::FUNCTION, $this->getDefinitionLocation('whatever()'), ''), - new SymbolInformation('SecondTestNamespace', SymbolKind::NAMESPACE, $this->getDefinitionLocation('SecondTestNamespace'), '') + new SymbolInformation('SecondTestNamespace', SymbolKind::NAMESPACE, $this->getDefinitionLocation('SecondTestNamespace'), '') ], $result); // @codingStandardsIgnoreEnd } From 7380acc49fbacb4d5a6b3a7bf76d6dd55eed2043 Mon Sep 17 00:00:00 2001 From: jens1o Date: Mon, 17 Apr 2017 12:18:27 +0200 Subject: [PATCH 05/16] fix code style --- src/Protocol/SymbolInformation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index 7ee34d0..d173426 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -90,7 +90,7 @@ class SymbolInformation } if ($node instanceof Node\Name) { $symbol->name = (string)$node; - } else if( + } else if ( $node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && strtolower((string)$node->name) === 'define' From 6fea33db04a9aceccd2baaa7d3548906206376d9 Mon Sep 17 00:00:00 2001 From: jens1o Date: Mon, 17 Apr 2017 16:04:12 +0200 Subject: [PATCH 06/16] unify code --- src/Protocol/SymbolInformation.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index d173426..ea03f9f 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -50,6 +50,8 @@ class SymbolInformation { $parent = $node->getAttribute('parentNode'); $symbol = new self; + $setDefaultName = true; + if ( $node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name @@ -60,6 +62,8 @@ class SymbolInformation // constants with define() like // define('TEST_DEFINE_CONSTANT', false); $symbol->kind = SymbolKind::CONSTANT; + $symbol->name = (string)$node->args[0]->value->value; + $setDefaultName = false; } else if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_) { $symbol->kind = SymbolKind::CLASS_; } else if ($node instanceof Node\Stmt\Interface_) { @@ -90,23 +94,18 @@ class SymbolInformation } if ($node instanceof Node\Name) { $symbol->name = (string)$node; - } else if ( - $node instanceof Node\Expr\FuncCall - && $node->name instanceof Node\Name - && strtolower((string)$node->name) === 'define' - && isset($node->args[0]) - && $node->args[0]->value instanceof Node\Scalar\String_ - ) { - $symbol->name = (string)$node->args[0]->value->value; } else if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignOp) { $symbol->name = $node->var->name; } else if ($node instanceof Node\Expr\ClosureUse) { $symbol->name = $node->var; } else if (isset($node->name)) { - $symbol->name = (string)$node->name; + if($setDefaultName) { + $symbol->name = (string)$node->name; + } } else { return null; } + $symbol->location = Location::fromNode($node); if ($fqn !== null) { $parts = preg_split('/(::|->|\\\\)/', $fqn); From 9d1af8412254f24c43c61c71392d4fb405bd7237 Mon Sep 17 00:00:00 2001 From: jens1o Date: Mon, 17 Apr 2017 16:06:30 +0200 Subject: [PATCH 07/16] code style --- src/Protocol/SymbolInformation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index ea03f9f..2d09d26 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -99,7 +99,7 @@ class SymbolInformation } else if ($node instanceof Node\Expr\ClosureUse) { $symbol->name = $node->var; } else if (isset($node->name)) { - if($setDefaultName) { + if ($setDefaultName) { $symbol->name = (string)$node->name; } } else { From 93fae1bb13cdbd1e4a0baf9d0f0373e3e67007c1 Mon Sep 17 00:00:00 2001 From: jens1o Date: Mon, 17 Apr 2017 16:16:31 +0200 Subject: [PATCH 08/16] update code style --- src/Protocol/SymbolInformation.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index 2d09d26..0a813ef 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -50,7 +50,6 @@ class SymbolInformation { $parent = $node->getAttribute('parentNode'); $symbol = new self; - $setDefaultName = true; if ( $node instanceof Node\Expr\FuncCall @@ -63,7 +62,6 @@ class SymbolInformation // define('TEST_DEFINE_CONSTANT', false); $symbol->kind = SymbolKind::CONSTANT; $symbol->name = (string)$node->args[0]->value->value; - $setDefaultName = false; } else if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_) { $symbol->kind = SymbolKind::CLASS_; } else if ($node instanceof Node\Stmt\Interface_) { @@ -98,10 +96,8 @@ class SymbolInformation $symbol->name = $node->var->name; } else if ($node instanceof Node\Expr\ClosureUse) { $symbol->name = $node->var; - } else if (isset($node->name)) { - if ($setDefaultName) { - $symbol->name = (string)$node->name; - } + } else if (isset($node->name) && !isset($symbol->name)) { + $symbol->name = (string)$node->name; } else { return null; } From 2625a1062b29cc26fa5ff090815cdfe6799f39bc Mon Sep 17 00:00:00 2001 From: jens1o Date: Mon, 17 Apr 2017 16:32:19 +0200 Subject: [PATCH 09/16] fix test & revert last commit --- src/Protocol/SymbolInformation.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index 0a813ef..2d09d26 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -50,6 +50,7 @@ class SymbolInformation { $parent = $node->getAttribute('parentNode'); $symbol = new self; + $setDefaultName = true; if ( $node instanceof Node\Expr\FuncCall @@ -62,6 +63,7 @@ class SymbolInformation // define('TEST_DEFINE_CONSTANT', false); $symbol->kind = SymbolKind::CONSTANT; $symbol->name = (string)$node->args[0]->value->value; + $setDefaultName = false; } else if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_) { $symbol->kind = SymbolKind::CLASS_; } else if ($node instanceof Node\Stmt\Interface_) { @@ -96,8 +98,10 @@ class SymbolInformation $symbol->name = $node->var->name; } else if ($node instanceof Node\Expr\ClosureUse) { $symbol->name = $node->var; - } else if (isset($node->name) && !isset($symbol->name)) { - $symbol->name = (string)$node->name; + } else if (isset($node->name)) { + if ($setDefaultName) { + $symbol->name = (string)$node->name; + } } else { return null; } From 8ac306f653564697583f1dcc75747c375fd6f41c Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Mon, 17 Apr 2017 16:37:21 +0200 Subject: [PATCH 10/16] Update SymbolInformation.php --- src/Protocol/SymbolInformation.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index 2d09d26..a1ce983 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -50,7 +50,6 @@ class SymbolInformation { $parent = $node->getAttribute('parentNode'); $symbol = new self; - $setDefaultName = true; if ( $node instanceof Node\Expr\FuncCall @@ -63,7 +62,6 @@ class SymbolInformation // define('TEST_DEFINE_CONSTANT', false); $symbol->kind = SymbolKind::CONSTANT; $symbol->name = (string)$node->args[0]->value->value; - $setDefaultName = false; } else if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_) { $symbol->kind = SymbolKind::CLASS_; } else if ($node instanceof Node\Stmt\Interface_) { @@ -92,18 +90,19 @@ class SymbolInformation } else { return null; } - if ($node instanceof Node\Name) { - $symbol->name = (string)$node; - } else if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignOp) { - $symbol->name = $node->var->name; - } else if ($node instanceof Node\Expr\ClosureUse) { - $symbol->name = $node->var; - } else if (isset($node->name)) { - if ($setDefaultName) { + + if (!isset($symbol->name)) { + if ($node instanceof Node\Name) { + $symbol->name = (string)$node; + } else if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignOp) { + $symbol->name = $node->var->name; + } else if ($node instanceof Node\Expr\ClosureUse) { + $symbol->name = $node->var; + } else if (isset($node->name)) { $symbol->name = (string)$node->name; + } else { + return null; } - } else { - return null; } $symbol->location = Location::fromNode($node); From ede90472d0768eb958791abf24cb8440b374da1f Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Mon, 17 Apr 2017 16:40:29 +0200 Subject: [PATCH 11/16] Remove whitespace --- src/Protocol/SymbolInformation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index a1ce983..4fb1d3f 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -91,7 +91,7 @@ class SymbolInformation return null; } - if (!isset($symbol->name)) { + if (!isset($symbol->name)) { if ($node instanceof Node\Name) { $symbol->name = (string)$node; } else if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignOp) { From eaacc47772e506c54bf8463dd427164f47665b4e Mon Sep 17 00:00:00 2001 From: jens1o Date: Fri, 9 Jun 2017 21:34:35 +0200 Subject: [PATCH 12/16] detect when class is abstract --- src/DefinitionResolver.php | 8 ++++++-- src/LanguageServer.php | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 9d97de7..b4ee7e5 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -114,7 +114,7 @@ class DefinitionResolver // For everything else, get the doc block summary corresponding to the current node. $docBlock = $this->getDocBlock($node); if ($docBlock !== null) { - // check wether we have a description, when true, add a new paragraph + // check whether we have a description, when true, add a new paragraph // with the description $description = $docBlock->getDescription()->render(); @@ -174,7 +174,11 @@ class DefinitionResolver $def->fqn = $fqn; // Determines whether the suggestion will show after "new" - $def->canBeInstantiated = $node instanceof Node\Statement\ClassDeclaration; + $def->canBeInstantiated = ( + $node instanceof Node\Statement\ClassDeclaration && + // use text's length for speedup: |abstract| = 7; |final| = 5 + ($node->abstractOrFinalModifier === null || $node->abstractOrFinalModifier->length !== 7) + ); // Interfaces, classes, traits, namespaces, functions, and global const elements $def->isGlobal = ( diff --git a/src/LanguageServer.php b/src/LanguageServer.php index 173abfe..118dd93 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -106,7 +106,7 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher protected $definitionResolver; /** - * @param PotocolReader $reader + * @param ProtocolReader $reader * @param ProtocolWriter $writer */ public function __construct(ProtocolReader $reader, ProtocolWriter $writer) @@ -132,7 +132,7 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher // If a ResponseError is thrown, send it back in the Response $error = $e; } catch (Throwable $e) { - // If an unexpected error occured, send back an INTERNAL_ERROR error response + // If an unexpected error occurred, send back an INTERNAL_ERROR error response $error = new AdvancedJsonRpc\Error( (string)$e, AdvancedJsonRpc\ErrorCode::INTERNAL_ERROR, From 0c9ee8b33d9cff16edc19889ea871de890afb5ef Mon Sep 17 00:00:00 2001 From: jens1o Date: Fri, 9 Jun 2017 21:39:12 +0200 Subject: [PATCH 13/16] I can't count properly --- src/DefinitionResolver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index b4ee7e5..be00a1d 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -176,8 +176,8 @@ class DefinitionResolver // Determines whether the suggestion will show after "new" $def->canBeInstantiated = ( $node instanceof Node\Statement\ClassDeclaration && - // use text's length for speedup: |abstract| = 7; |final| = 5 - ($node->abstractOrFinalModifier === null || $node->abstractOrFinalModifier->length !== 7) + // use text's length for speedup: |abstract| = 8; |final| = 5 + ($node->abstractOrFinalModifier === null || $node->abstractOrFinalModifier->length !== 8) ); // Interfaces, classes, traits, namespaces, functions, and global const elements From 454e078cd511df49623dc978a8e6e94be898139b Mon Sep 17 00:00:00 2001 From: jens1o Date: Fri, 9 Jun 2017 21:59:18 +0200 Subject: [PATCH 14/16] fix merge conflict and abstract detection --- src/DefinitionResolver.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index be00a1d..cf2f8a6 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -176,8 +176,8 @@ class DefinitionResolver // Determines whether the suggestion will show after "new" $def->canBeInstantiated = ( $node instanceof Node\Statement\ClassDeclaration && - // use text's length for speedup: |abstract| = 8; |final| = 5 - ($node->abstractOrFinalModifier === null || $node->abstractOrFinalModifier->length !== 8) + // check whether it is not an abstract class + ($node->abstractOrFinalModifier === null || $node->abstractOrFinalModifier->kind !== PhpParser\TokenKind::AbstractKeyword) ); // Interfaces, classes, traits, namespaces, functions, and global const elements @@ -1194,11 +1194,6 @@ class DefinitionResolver if ($tag->getVariableName() === \ltrim($variableName, "$")) { return $tag; } - } else if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && strtolower((string)$node->name) === 'define') { - if (!isset($node->args[0]) || !($node->args[0]->value instanceof Node\Scalar\String_)) { - return null; - } - return (string)$node->args[0]->value->value; } return null; } From 893596208e39dcb346c8a935b971f740d2f4fe77 Mon Sep 17 00:00:00 2001 From: jens1o Date: Sat, 10 Jun 2017 14:25:05 +0200 Subject: [PATCH 15/16] start with foreach regocnitian --- fixtures/foreach.php | 4 ++++ src/DefinitionResolver.php | 10 ++++++++++ tests/Server/TextDocument/Definition/ForeachTest.php | 3 +++ 3 files changed, 17 insertions(+) create mode 100644 fixtures/foreach.php create mode 100644 tests/Server/TextDocument/Definition/ForeachTest.php diff --git a/fixtures/foreach.php b/fixtures/foreach.php new file mode 100644 index 0000000..565490d --- /dev/null +++ b/fixtures/foreach.php @@ -0,0 +1,4 @@ + $foreachValue) {} + if ($n instanceof Node\Statement\ForeachStatement) { + if ($n->foreachValue !== null && $n->foreachValue->getText() === $name) { + return $n->foreachValue; + } elseif ($n->foreachKey !== null && $n->foreachKey->getText() === $name) { + return $n->foreachKey; + } + } } } while (isset($n) && $n = $n->parent); // Return null if nothing was found @@ -563,6 +572,7 @@ class DefinitionResolver // VARIABLE // $this -> Type\this // $myVariable -> type of corresponding assignment expression + // $foreachKey -> Register type if ($expr instanceof Node\Expression\Variable || $expr instanceof Node\UseVariableName) { if ($expr->getName() === 'this') { return new Types\This; diff --git a/tests/Server/TextDocument/Definition/ForeachTest.php b/tests/Server/TextDocument/Definition/ForeachTest.php new file mode 100644 index 0000000..6dc2532 --- /dev/null +++ b/tests/Server/TextDocument/Definition/ForeachTest.php @@ -0,0 +1,3 @@ + Date: Sat, 17 Jun 2017 11:25:31 +0200 Subject: [PATCH 16/16] modify comment and merge master --- src/DefinitionResolver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 5577246..685bad7 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -543,8 +543,8 @@ class DefinitionResolver ) { return $n; } - - // foreach ([0, 1, 2, 3] as $foreachKey => $foreachValue) {} + + // foreach loops if ($n instanceof Node\Statement\ForeachStatement) { if ($n->foreachValue !== null && $n->foreachValue->getText() === $name) { return $n->foreachValue;