From edf57733bacfc80f2c64fc5617b227dc16718b0f Mon Sep 17 00:00:00 2001 From: Damjan Cvetko Date: Wed, 26 Jul 2023 06:35:31 +0200 Subject: [PATCH 1/4] Implement textDocument/xevaluatableExpression --- src/Server/TextDocument.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index 8f78c6f1..56545a3f 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -426,4 +426,35 @@ 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; + }); + } + } From cd271cffc2c276bdc47326d78329d3bdfb98f0cc Mon Sep 17 00:00:00 2001 From: Damjan Cvetko Date: Wed, 26 Jul 2023 07:02:23 +0200 Subject: [PATCH 2/4] phpcs --- src/Server/TextDocument.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index 56545a3f..13bb82c5 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -452,9 +452,8 @@ public function xevaluatableExpression(TextDocumentIdentifier $textDocument, Pos ) { $range = RangeFactory::fromNode($node); return [ 'expression' => $node->getText(), 'range' => $range ]; - } + } return null; }); } - } From bf73e32a28668fccb183316566a335e21af01dda Mon Sep 17 00:00:00 2001 From: Damjan Cvetko Date: Wed, 26 Jul 2023 10:52:14 +0200 Subject: [PATCH 3/4] Expose xevaluatableExpressionProvider experimental server capability. --- src/LanguageServer.php | 3 +++ 1 file changed, 3 insertions(+) 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); }); From 1302e365c4e5fb773df30225a9e9dd7c58e46441 Mon Sep 17 00:00:00 2001 From: Damjan Cvetko Date: Wed, 26 Jul 2023 11:12:54 +0200 Subject: [PATCH 4/4] Add new server capability. --- tests/LanguageServerTest.php | 3 +++ 1 file changed, 3 insertions(+) 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); }