From 1bb2b4a86c9b82305f67fec2708853be3e016b2e Mon Sep 17 00:00:00 2001 From: Michal Niewrzal Date: Mon, 5 Sep 2016 18:37:39 +0200 Subject: [PATCH 1/4] Support document formatting #8 --- src/LanguageServer.php | 2 ++ src/Server/TextDocument.php | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/LanguageServer.php b/src/LanguageServer.php index 87663536..9208e8df 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -74,6 +74,8 @@ public function initialize(string $rootPath, int $processId, ClientCapabilities $serverCapabilities->textDocumentSync = TextDocumentSyncKind::FULL; // Support "Find all symbols" $serverCapabilities->documentSymbolProvider = true; + // Support "Format Code" + $serverCapabilities->documentFormattingProvider = true; return new InitializeResult($serverCapabilities); } diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index f3fd6bb7..fc33fe42 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -2,7 +2,7 @@ namespace LanguageServer\Server; -use PhpParser\{Error, Comment, Node, ParserFactory, NodeTraverser, Lexer}; +use PhpParser\{Error, Comment, Node, ParserFactory, NodeTraverser, Lexer, PrettyPrinter\Standard as PrettyPrinterStandard}; use PhpParser\NodeVisitor\NameResolver; use LanguageServer\{LanguageClient, ColumnCalculator, SymbolFinder}; use LanguageServer\Protocol\{ @@ -12,7 +12,9 @@ Diagnostic, DiagnosticSeverity, Range, - Position + Position, + FormattingOptions, + TextEdit }; /** @@ -124,4 +126,25 @@ private function updateAst(string $uri, string $content) $this->asts[$uri] = $stmts; } } + + /** + * The document formatting request is sent from the server to the client to format a whole document. + * + * @param TextDocumentIdentifier $textDocument The document to format + * @param FormattingOptions $options The format options + * @return TextEdit[] + */ + public function formatting(TextDocumentIdentifier $textDocument, FormattingOptions $options) + { + $nodes = $this->asts[$textDocument->uri]; + if (empty($nodes)){ + return []; + } + $prettyPrinter = new PrettyPrinterStandard(); + $edit = new TextEdit(); + $edit->range = new Range(new Position(0, 0), new Position(PHP_INT_MAX, PHP_INT_MAX)); + $edit->newText = $prettyPrinter->prettyPrintFile($nodes); + return [$edit]; + } + } From a2af52075bb63d6e8f847be832575430471bb1b3 Mon Sep 17 00:00:00 2001 From: Michal Niewrzal Date: Mon, 5 Sep 2016 20:25:35 +0200 Subject: [PATCH 2/4] Support document formatting #8 --- fixtures/Format.php | 20 +++++++++++++++++++ fixtures/Format_expected.php | 15 +++++++++++++++ src/Server/TextDocument.php | 7 ++++--- tests/LanguageServerTest.php | 2 +- tests/Server/TextDocumentTest.php | 32 ++++++++++++++++++++++++++++++- 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 fixtures/Format.php create mode 100644 fixtures/Format_expected.php diff --git a/fixtures/Format.php b/fixtures/Format.php new file mode 100644 index 00000000..b45ebab8 --- /dev/null +++ b/fixtures/Format.php @@ -0,0 +1,20 @@ +asts[$textDocument->uri]; - if (empty($nodes)){ + if (empty($nodes)) { return []; } - $prettyPrinter = new PrettyPrinterStandard(); + $prettyPrinter = new PrettyPrinter(); $edit = new TextEdit(); $edit->range = new Range(new Position(0, 0), new Position(PHP_INT_MAX, PHP_INT_MAX)); $edit->newText = $prettyPrinter->prettyPrintFile($nodes); diff --git a/tests/LanguageServerTest.php b/tests/LanguageServerTest.php index 4c1ca825..fbba7b54 100644 --- a/tests/LanguageServerTest.php +++ b/tests/LanguageServerTest.php @@ -40,7 +40,7 @@ public function testInitialize() 'workspaceSymbolProvider' => null, 'codeActionProvider' => null, 'codeLensProvider' => null, - 'documentFormattingProvider' => null, + 'documentFormattingProvider' => true, 'documentRangeFormattingProvider' => null, 'documentOnTypeFormattingProvider' => null, 'renameProvider' => null diff --git a/tests/Server/TextDocumentTest.php b/tests/Server/TextDocumentTest.php index 4f795d47..687b4b68 100644 --- a/tests/Server/TextDocumentTest.php +++ b/tests/Server/TextDocumentTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase; use LanguageServer\Tests\MockProtocolStream; use LanguageServer\{Server, Client, LanguageClient}; -use LanguageServer\Protocol\{TextDocumentItem, TextDocumentIdentifier, SymbolKind, DiagnosticSeverity}; +use LanguageServer\Protocol\{TextDocumentItem, TextDocumentIdentifier, SymbolKind, DiagnosticSeverity, FormattingOptions}; use AdvancedJsonRpc\{Request as RequestBody, Response as ResponseBody}; class TextDocumentTest extends TestCase @@ -197,4 +197,34 @@ public function publishDiagnostics(string $uri, array $diagnostics) ]] ], json_decode(json_encode($args), true)); } + + public function testFormatting() + { + $textDocument = new Server\TextDocument(new LanguageClient(new MockProtocolStream())); + // Trigger parsing of source + $textDocumentItem = new TextDocumentItem(); + $textDocumentItem->uri = 'whatever'; + $textDocumentItem->languageId = 'php'; + $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 + $result = $textDocument->formatting(new TextDocumentIdentifier('whatever'), new FormattingOptions()); + $this->assertEquals([0 => [ + 'range' => [ + 'start' => [ + 'line' => 0, + 'character' => 0 + ], + 'end' => [ + 'line' => PHP_INT_MAX, + 'character' => PHP_INT_MAX + ] + ], + 'newText' => $expected + ]], json_decode(json_encode($result), true)); + } } From 9e9744a6c22c1f3e88d38d7dbf6a360ebcaeb9a8 Mon Sep 17 00:00:00 2001 From: Michal Niewrzal Date: Tue, 6 Sep 2016 11:43:40 +0200 Subject: [PATCH 3/4] Support document formatting #8 --- fixtures/{Format.php => format.php} | 0 fixtures/{Format_expected.php => format_expected.php} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename fixtures/{Format.php => format.php} (100%) rename fixtures/{Format_expected.php => format_expected.php} (100%) diff --git a/fixtures/Format.php b/fixtures/format.php similarity index 100% rename from fixtures/Format.php rename to fixtures/format.php diff --git a/fixtures/Format_expected.php b/fixtures/format_expected.php similarity index 100% rename from fixtures/Format_expected.php rename to fixtures/format_expected.php From 0c0dfed57386c3547b021ec02254fae4d7020029 Mon Sep 17 00:00:00 2001 From: Michal Niewrzal Date: Tue, 6 Sep 2016 12:04:47 +0200 Subject: [PATCH 4/4] Support document formatting #8 --- tests/Server/TextDocumentTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Server/TextDocumentTest.php b/tests/Server/TextDocumentTest.php index 687b4b68..5cbe470a 100644 --- a/tests/Server/TextDocumentTest.php +++ b/tests/Server/TextDocumentTest.php @@ -206,11 +206,11 @@ public function testFormatting() $textDocumentItem->uri = 'whatever'; $textDocumentItem->languageId = 'php'; $textDocumentItem->version = 1; - $textDocumentItem->text = file_get_contents(__DIR__ . '/../../fixtures/Format.php'); + $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'); + $expected = file_get_contents(__DIR__ . '/../../fixtures/format_expected.php'); // Request formatting $result = $textDocument->formatting(new TextDocumentIdentifier('whatever'), new FormattingOptions()); $this->assertEquals([0 => [