Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support constants #347

Merged
merged 11 commits into from
Apr 17, 2017
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions fixtures/completion/constant_with_namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace HELLO {

/**
* Does something really cool!
*/
function world() {

}

\HE
}

namespace {

/**
* Lorem ipsum dolor sit amet.
*/
define('HELLO', true);

HELLO\world();
}
7 changes: 7 additions & 0 deletions fixtures/global_symbols.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ public function testMethod($testParameter)
};

class ChildClass extends TestClass {}

/**
* Lorem ipsum dolor sit amet, consectetur.
*/
define('TEST_DEFINE_CONSTANT', false);

print TEST_DEFINE_CONSTANT ? 'true' : 'false';
5 changes: 5 additions & 0 deletions src/DefinitionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,11 @@ public static function getDefinedFqn(Node $node)
}
return (string)$class->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;
}
}
}
2 changes: 1 addition & 1 deletion src/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getDefinition(string $fqn, bool $globalFallback = false)
* 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)
Expand Down
40 changes: 27 additions & 13 deletions src/Protocol/SymbolInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,19 @@ public static function fromNode(Node $node, string $fqn = null)
{
$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_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_) {
$symbol->kind = SymbolKind::INTERFACE;
Expand Down Expand Up @@ -80,17 +90,21 @@ public static function fromNode(Node $node, string $fqn = null)
} 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)) {
$symbol->name = (string)$node->name;
} else {
return null;

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;
}
}

$symbol->location = Location::fromNode($node);
if ($fqn !== null) {
$parts = preg_split('/(::|->|\\\\)/', $fqn);
Expand Down
4 changes: 2 additions & 2 deletions tests/LanguageServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testIndexingWithDirectFileAccess()
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();
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public function testIndexingWithFilesAndContentRequests()
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();
}
}
Expand Down
28 changes: 16 additions & 12 deletions tests/Server/ServerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,19 @@ public function setUp()
$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_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))),
'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))),
Expand Down Expand Up @@ -163,6 +164,9 @@ public function setUp()
],

// Global
'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))),
1 => new Location($globalReferencesUri, new Range(new Position(29, 5), new Position(29, 15)))
Expand Down
15 changes: 15 additions & 0 deletions tests/Server/TextDocument/HoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ public function testHoverForConstant()
], $reference->range), $result);
}

public function testHoverForGlobalConstant()
{
// 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', "<?php\n\\define('TEST_DEFINE_CONSTANT', \\false);"),
'Lorem ipsum dolor sit amet, consectetur.'
], $reference->range), $result);
}

public function testHoverForVariable()
{
// echo $var;
Expand Down
Loading