From fa77305cac43e6f9a70f214d7237d8db3d2ee79a Mon Sep 17 00:00:00 2001 From: Daniel Leech Date: Wed, 15 May 2019 22:13:55 +0100 Subject: [PATCH] Added target parameter to open file response Provide a hint as to how the editor should open the file, e.g. in a new tab, a horizontal split, current "buffer" etc. --- lib/Response/OpenFileResponse.php | 44 +++++++++++++++++++- tests/Unit/Response/OpenFileResponseTest.php | 44 ++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Response/OpenFileResponseTest.php diff --git a/lib/Response/OpenFileResponse.php b/lib/Response/OpenFileResponse.php index 4d9469a..010106c 100644 --- a/lib/Response/OpenFileResponse.php +++ b/lib/Response/OpenFileResponse.php @@ -3,9 +3,22 @@ namespace Phpactor\Extension\Rpc\Response; use Phpactor\Extension\Rpc\Response; +use RuntimeException; class OpenFileResponse implements Response { + const TARGET_FOCUSED_WINDOW = 'focused_window'; + const TARGET_VERTICAL_SPLIT = 'vsplit'; + const TARGET_HORIZONTAL_SPLIT = 'hsplit'; + const TARGET_NEW_TAB = 'new_tab'; + + const VALID_TARGETS = [ + self::TARGET_FOCUSED_WINDOW, + self::TARGET_VERTICAL_SPLIT, + self::TARGET_HORIZONTAL_SPLIT, + self::TARGET_NEW_TAB + ]; + /** * @var string */ @@ -21,11 +34,17 @@ class OpenFileResponse implements Response */ private $forceReload; - private function __construct(string $path, int $offset = 0, bool $forceReload = false) + /** + * @var string + */ + private $target; + + private function __construct(string $path, int $offset = 0, bool $forceReload = false, string $target = self::TARGET_FOCUSED_WINDOW) { $this->path = $path; $this->offset = $offset; $this->forceReload = $forceReload; + $this->target = $target; } public static function fromPath(string $path): OpenFileResponse @@ -48,7 +67,8 @@ public function parameters(): array return [ 'path' => $this->path, 'offset' => $this->offset, - 'force_reload' => $this->forceReload + 'force_reload' => $this->forceReload, + 'target' => $this->target, ]; } @@ -57,6 +77,11 @@ public function path(): string return $this->path; } + public function target(): string + { + return $this->target; + } + public function withForcedReload(bool $bool): OpenFileResponse { $new = clone $this; @@ -64,4 +89,19 @@ public function withForcedReload(bool $bool): OpenFileResponse return $new; } + + public function withTarget(string $target): OpenFileResponse + { + if (!in_array($target, self::VALID_TARGETS)) { + throw new RuntimeException(sprintf( + 'Unknown target "%s", known targets "%s"', + $target, + implode('", "', self::VALID_TARGETS) + )); + } + $new = clone $this; + $new->target = $target; + + return $new; + } } diff --git a/tests/Unit/Response/OpenFileResponseTest.php b/tests/Unit/Response/OpenFileResponseTest.php new file mode 100644 index 0000000..1388d9b --- /dev/null +++ b/tests/Unit/Response/OpenFileResponseTest.php @@ -0,0 +1,44 @@ +assertEquals(OpenFileResponse::TARGET_FOCUSED_WINDOW, $response->target()); + } + + public function testExceptionOnInvalidTarget() + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Unknown target "nope"'); + OpenFileResponse::fromPath(__FILE__) + ->withTarget('nope'); + } + + public function testReturnsConfiguredTarget() + { + $response = OpenFileResponse::fromPath(__FILE__) + ->withTarget(OpenFileResponse::TARGET_HORIZONTAL_SPLIT); + $this->assertEquals(OpenFileResponse::TARGET_HORIZONTAL_SPLIT, $response->target()); + } + + public function testReturnsParameters() + { + $response = OpenFileResponse::fromPathAndOffset(__FILE__, 6) + ->withTarget(OpenFileResponse::TARGET_HORIZONTAL_SPLIT) + ->withForcedReload(true); + $this->assertEquals([ + 'path' => __FILE__, + 'offset' => 6, + 'force_reload' => true, + 'target' => OpenFileResponse::TARGET_HORIZONTAL_SPLIT + ], $response->parameters()); + } +}