diff --git a/src/LanguageServer.php b/src/LanguageServer.php index dc2d0a5c..7884f5af 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -307,6 +307,9 @@ public function initialize(ClientCapabilities $capabilities, string $rootPath = $serverCapabilities->xworkspaceReferencesProvider = true; $serverCapabilities->xdefinitionProvider = true; $serverCapabilities->xdependenciesProvider = true; + $serverCapabilities->experimental = [ + 'xevaluatableExpressionProvider' => true, + ]; return new InitializeResult($serverCapabilities); }); diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index 8f78c6f1..13bb82c5 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -426,4 +426,34 @@ public function xdefinition(TextDocumentIdentifier $textDocument, Position $posi return [new SymbolLocationInformation($descriptor, $def->symbolInformation->location)]; }); } + + /** + * Return an evaluatable exrpession under the cursor for the debugger. + * + * @param TextDocumentIdentifier $textDocument The text document + * @param Position $position The position inside the text document + * @return Promise + */ + public function xevaluatableExpression(TextDocumentIdentifier $textDocument, Position $position): Promise + { + return coroutine(function () use ($textDocument, $position) { + $document = yield $this->documentLoader->getOrLoad($textDocument->uri); + // Find the node under the cursor + $node = $document->getNodeAtPosition($position); + if ($node === null) { + return null; + } + if ( + $node instanceof Node\Expression\Variable || + $node instanceof Node\Parameter || + $node instanceof Node\Expression\SubscriptExpression || + $node instanceof Node\Expression\MemberAccessExpression || + (($node2 = $node->getFirstAncestor(Node\Expression\SubscriptExpression::class)) && ($node = $node2)) + ) { + $range = RangeFactory::fromNode($node); + return [ 'expression' => $node->getText(), 'range' => $range ]; + } + return null; + }); + } } diff --git a/tests/LanguageServerTest.php b/tests/LanguageServerTest.php index eef1a629..67b9f3fc 100644 --- a/tests/LanguageServerTest.php +++ b/tests/LanguageServerTest.php @@ -47,6 +47,9 @@ public function testInitialize() $serverCapabilities->xworkspaceReferencesProvider = true; $serverCapabilities->xdefinitionProvider = true; $serverCapabilities->xdependenciesProvider = true; + $serverCapabilities->experimental = [ + 'xevaluatableExpressionProvider' => true, + ]; $this->assertEquals(new InitializeResult($serverCapabilities), $result); }