From 347cfae7c01a06232410138bac17dd71e7492dfe Mon Sep 17 00:00:00 2001 From: Levan Gabeskiria Date: Thu, 8 Sep 2016 22:19:36 +0400 Subject: [PATCH] exclude variable symbols. --- .gitignore | 1 + src/SymbolFinder.php | 20 ++++++++++++++++++-- tests/Server/TextDocumentTest.php | 24 +++--------------------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 4f7aa83..2729a4c 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 3a48948..99f1efc 100644 --- a/src/SymbolFinder.php +++ b/src/SymbolFinder.php @@ -21,7 +21,7 @@ class SymbolFinder extends NodeVisitorAbstract ]; /** - * @var LanguageServer\Protocol\SymbolInformation[] + * @var \LanguageServer\Protocol\SymbolInformation[] */ public $symbols; @@ -44,8 +44,24 @@ class SymbolFinder extends NodeVisitorAbstract { $class = get_class($node); if (!isset(self::NODE_SYMBOL_KIND_MAP[$class])) { - return; + 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 5cbe470..697a2bd 100644 --- a/tests/Server/TextDocumentTest.php +++ b/tests/Server/TextDocumentTest.php @@ -24,7 +24,7 @@ class TextDocumentTest extends TestCase // Request symbols $result = $textDocument->documentSymbol(new TextDocumentIdentifier('whatever')); $this->assertEquals([ - [ + [ 'name' => 'TestNamespace', 'kind' => SymbolKind::NAMESPACE, 'location' => [ @@ -96,24 +96,6 @@ class TextDocumentTest extends TestCase ], '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 @@ class TextDocumentTest extends TestCase ]] ], json_decode(json_encode($args), true)); } - + public function testFormatting() { $textDocument = new Server\TextDocument(new LanguageClient(new MockProtocolStream())); @@ -208,7 +190,7 @@ class TextDocumentTest extends TestCase $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