diff --git a/README.md b/README.md index b2ec922..35853bf 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,5 @@ [![License](https://img.shields.io/packagist/l/felixfbecker/language-server.svg)](https://github.com/felixfbecker/php-language-server/blob/master/LICENSE.txt) A pure PHP implementation of the [Language Server Protocol](https://github.com/Microsoft/language-server-protocol). + +![Find all symbols demo](images/documentSymbol.gif) diff --git a/images/documentSymbol.gif b/images/documentSymbol.gif new file mode 100644 index 0000000..43a468a Binary files /dev/null and b/images/documentSymbol.gif differ diff --git a/src/SymbolFinder.php b/src/SymbolFinder.php index 604e3cf..0d0b90e 100644 --- a/src/SymbolFinder.php +++ b/src/SymbolFinder.php @@ -4,6 +4,7 @@ declare(strict_types = 1); namespace LanguageServer; use PhpParser\{NodeVisitorAbstract, Node}; +use LanguageServer\Protocol\{SymbolInformation, SymbolKind, Range, Position, Location}; class SymbolFinder extends NodeVisitorAbstract { @@ -41,17 +42,17 @@ class SymbolFinder extends NodeVisitorAbstract public function enterNode(Node $node) { $class = get_class($node); - if (!isset(self::NODE_SYMBOL_MAP[$class])) { + if (!isset(self::NODE_SYMBOL_KIND_MAP[$class])) { return; } $symbol = new SymbolInformation(); - $symbol->kind = self::NODE_SYMBOL_MAP[$class]; + $symbol->kind = self::NODE_SYMBOL_KIND_MAP[$class]; $symbol->name = (string)$node->name; $symbol->location = new Location( $this->uri, new Range( - new Position($node->getAttribute('startLine'), $node->getAttribute('startColumn')), - new Position($node->getAttribute('endLine'), $node->getAttribute('endColumn')) + new Position($node->getAttribute('startLine') - 1, $node->getAttribute('startColumn') - 1), + new Position($node->getAttribute('endLine') - 1, $node->getAttribute('endColumn') - 1) ) ); $symbol->containerName = $this->containerName; diff --git a/src/TextDocumentManager.php b/src/TextDocumentManager.php index b7a09f8..f0825ff 100644 --- a/src/TextDocumentManager.php +++ b/src/TextDocumentManager.php @@ -4,7 +4,7 @@ namespace LanguageServer; use PhpParser\{Error, Comment, Node, ParserFactory, NodeTraverser, Lexer}; use PhpParser\NodeVisitor\NameResolver; -use LanguageServer\Protocol\TextDocumentItem; +use LanguageServer\Protocol\{TextDocumentItem, TextDocumentIdentifier, VersionedTextDocumentIdentifier}; /** * Provides method handlers for all textDocument/* methods @@ -65,18 +65,18 @@ class TextDocumentManager /** * The document change notification is sent from the client to the server to signal changes to a text document. * - * @param LanguageServer\Protocol\VersionedTextDocumentIdentifier $textDocument - * @param LanguageServer\Protocol\TextDocumentContentChangeEvent[] $contentChanges + * @param Protocol\VersionedTextDocumentIdentifier $textDocument + * @param Protocol\TextDocumentContentChangeEvent[] $contentChanges * @return void */ public function didChange(VersionedTextDocumentIdentifier $textDocument, array $contentChanges) { - $this->updateAst($textDocument->uri, $contentChanges->text); + $this->updateAst($textDocument->uri, $contentChanges[0]->text); } private function updateAst(string $uri, string $content) { - $stmts = $parser->parse($content); + $stmts = $this->parser->parse($content); // TODO report errors as diagnostics // foreach ($parser->getErrors() as $error) { // error_log($error->getMessage()); @@ -85,7 +85,7 @@ class TextDocumentManager if ($stmts) { $traverser = new NodeTraverser; $traverser->addVisitor(new NameResolver); - $traverser->addVisitor(new ColumnCalculator($textDocument->text)); + $traverser->addVisitor(new ColumnCalculator($content)); $traverser->traverse($stmts); } $this->asts[$uri] = $stmts;