diff --git a/.gitignore b/.gitignore index 4f7aa83b..2729a4c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store .vscode +.idea vendor/ composer.lock diff --git a/src/SymbolFinder.php b/src/SymbolFinder.php index 3a489489..8fd42ab3 100644 --- a/src/SymbolFinder.php +++ b/src/SymbolFinder.php @@ -21,9 +21,9 @@ class SymbolFinder extends NodeVisitorAbstract ]; /** - * @var LanguageServer\Protocol\SymbolInformation[] + * @var \LanguageServer\Protocol\SymbolInformation[] */ - public $symbols; + public $symbols = []; /** * @var string @@ -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; diff --git a/tests/Server/TextDocumentTest.php b/tests/Server/TextDocumentTest.php index 5cbe470a..697a2bdb 100644 --- a/tests/Server/TextDocumentTest.php +++ b/tests/Server/TextDocumentTest.php @@ -24,7 +24,7 @@ public function testDocumentSymbol() // Request symbols $result = $textDocument->documentSymbol(new TextDocumentIdentifier('whatever')); $this->assertEquals([ - [ + [ 'name' => 'TestNamespace', 'kind' => SymbolKind::NAMESPACE, 'location' => [ @@ -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_, @@ -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())); @@ -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