Skip to content

Variable symbols #15

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.vscode
.idea
vendor/
composer.lock
20 changes: 18 additions & 2 deletions src/SymbolFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class SymbolFinder extends NodeVisitorAbstract
];

/**
* @var LanguageServer\Protocol\SymbolInformation[]
* @var \LanguageServer\Protocol\SymbolInformation[]
*/
public $symbols;
public $symbols = [];

/**
* @var string
Expand All @@ -46,6 +46,22 @@ public function enterNode(Node $node)
if (!isset(self::NODE_SYMBOL_KIND_MAP[$class])) {
return;
}

$symbol = end($this->symbols);
$kind = self::NODE_SYMBOL_KIND_MAP[$class];

// exclude variable symbols that are defined in methods and functions.
if ($symbol && $kind === SymbolKind::VARIABLE &&
($symbol->kind === SymbolKind::METHOD || $symbol->kind === SymbolKind::FUNCTION)
) {
if (
$node->getAttribute('startLine') - 1 > $symbol->location->range->start->line &&
$node->getAttribute('endLine') - 1 < $symbol->location->range->end->line
) {
return;
}
}

$symbol = new SymbolInformation();
$symbol->kind = self::NODE_SYMBOL_KIND_MAP[$class];
$symbol->name = (string)$node->name;
Expand Down
24 changes: 3 additions & 21 deletions tests/Server/TextDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testDocumentSymbol()
// Request symbols
$result = $textDocument->documentSymbol(new TextDocumentIdentifier('whatever'));
$this->assertEquals([
[
[
'name' => 'TestNamespace',
'kind' => SymbolKind::NAMESPACE,
'location' => [
Expand Down Expand Up @@ -96,24 +96,6 @@ public function testDocumentSymbol()
],
'containerName' => null
],
[
'name' => 'testVariable',
'kind' => SymbolKind::VARIABLE,
'location' => [
'uri' => 'whatever',
'range' => [
'start' => [
'line' => 10,
'character' => 8
],
'end' => [
'line' => 10,
'character' => 20
]
]
],
'containerName' => null
],
[
'name' => 'TestTrait',
'kind' => SymbolKind::CLASS_,
Expand Down Expand Up @@ -197,7 +179,7 @@ public function publishDiagnostics(string $uri, array $diagnostics)
]]
], json_decode(json_encode($args), true));
}

public function testFormatting()
{
$textDocument = new Server\TextDocument(new LanguageClient(new MockProtocolStream()));
Expand All @@ -208,7 +190,7 @@ public function testFormatting()
$textDocumentItem->version = 1;
$textDocumentItem->text = file_get_contents(__DIR__ . '/../../fixtures/format.php');
$textDocument->didOpen($textDocumentItem);

// how code should look after formatting
$expected = file_get_contents(__DIR__ . '/../../fixtures/format_expected.php');
// Request formatting
Expand Down