diff --git a/src/Formatter.php b/src/Formatter.php index 1156876..f5c973c 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -32,9 +32,19 @@ class Formatter if ($content === $new) { return []; } - return [new TextEdit(new Range(new Position(0, 0), new Position(PHP_INT_MAX, PHP_INT_MAX)), $new)]; + return [new TextEdit(new Range(new Position(0, 0), $this->calculateEndPosition($content)), $new)]; } + /** + * @param string $content + * @return \LanguageServer\Protocol\Position + */ + private function calculateEndPosition(string $content): Position + { + $lines = explode("\n", $content); + return new Position(sizeof($lines) - 1, strlen(end($lines))); + } + /** * * @param string $uri diff --git a/tests/Server/TextDocument/FormattingTest.php b/tests/Server/TextDocument/FormattingTest.php index 06b45fc..4861d60 100644 --- a/tests/Server/TextDocument/FormattingTest.php +++ b/tests/Server/TextDocument/FormattingTest.php @@ -47,8 +47,8 @@ class FormattingTest extends TestCase 'character' => 0 ], 'end' => [ - 'line' => PHP_INT_MAX, - 'character' => PHP_INT_MAX + 'line' => 20, + 'character' => 0 ] ], 'newText' => $expected