1
0
Fork 0

Make documentSymbol work 🎉

pull/6/head v2.0.5
Felix Becker 2016-09-02 02:56:45 +02:00
parent b8b038d0b0
commit 57604e61f1
4 changed files with 13 additions and 10 deletions

View File

@ -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)

BIN
images/documentSymbol.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

View File

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

View File

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