diff --git a/src/Document.php b/src/Document.php index e484aef..efc5291 100644 --- a/src/Document.php +++ b/src/Document.php @@ -2,6 +2,8 @@ namespace Aviat\Kilo; +use Aviat\Kilo\Type\Point; + /** * The representation of the current document being edited * @@ -10,6 +12,9 @@ namespace Aviat\Kilo; class Document { public ?Syntax $syntax = NULL; + // Tokens for highlighting PHP + public array $tokens = []; + private function __construct( public array $rows = [], public ?string $filename = NULL, diff --git a/src/Editor.php b/src/Editor.php index 2955b72..0a63174 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -100,6 +100,7 @@ class Editor { $this->cursor = Point::new(); $this->offset = Point::new(); $this->terminalSize = Terminal::size(); + $this->document = Document::new(); if (is_string($filename)) { diff --git a/tests/EditorTest.php b/tests/EditorTest.php index f83d719..81214a7 100644 --- a/tests/EditorTest.php +++ b/tests/EditorTest.php @@ -3,16 +3,25 @@ namespace Aviat\Kilo\Tests\Traits; use Aviat\Kilo\Editor; +use Aviat\Kilo\Type\TerminalSize; use PHPUnit\Framework\TestCase; use Spatie\Snapshots\MatchesSnapshots; -use function Aviat\Kilo\get_window_size; class MockEditor extends Editor { - public function __set(string $key, $value): void + public static function mock(string $filename = ''): Editor { - if (property_exists($this, $key)) + $self = self::new(2, ['kilo', $filename]); + $self->terminalSize = new TerminalSize(23, 80); + $self->statusMessage->time = 1234567890; + + return $self; + } + + public function __set(string $name, mixed $value): void + { + if (property_exists($this, $name)) { - $this->$key = $value; + $this->$name = $value; } } } @@ -20,43 +29,35 @@ class MockEditor extends Editor { class EditorTest extends TestCase { use MatchesSnapshots; - protected Editor $editor; - - public function setUp(): void - { - parent::setUp(); - - // Mock the screen size, since that can vary widely - $this->editor = MockEditor::new(); - $this->editor->__set('screenRows', 23); - $this->editor->__set('screenCols', 80); - } - public function testSanity(): void { - $this->assertEquals(0, $this->editor->numRows); - $this->assertNull($this->editor->syntax); + $editor = MockEditor::mock(); + + $this->assertEquals(0, $editor->numRows); + $this->assertNull($editor->syntax); } public function test__debugInfo(): void { - $state = json_encode($this->editor->__debugInfo(), JSON_THROW_ON_ERROR); + $editor = MockEditor::mock(); + + $state = json_encode($editor->__debugInfo(), JSON_THROW_ON_ERROR); $this->assertMatchesJsonSnapshot($state); } public function testOpenPHP(): void { - $this->editor->open('test.php'); - $state = json_encode($this->editor, JSON_THROW_ON_ERROR); + $editor = MockEditor::mock('test.php'); + $state = json_encode($editor, JSON_THROW_ON_ERROR); $this->assertMatchesJsonSnapshot($state); } public function testOpen(): void { - $this->editor->open('src/ffi.h'); - $state = json_encode($this->editor, JSON_THROW_ON_ERROR); + $editor = MockEditor::mock('src/ffi.h'); + $state = json_encode($editor, JSON_THROW_ON_ERROR); $this->assertMatchesJsonSnapshot($state); } } \ No newline at end of file diff --git a/tests/__snapshots__/EditorTest__test__debugInfo__1.json b/tests/__snapshots__/EditorTest__test__debugInfo__1.json index f08d0ec..6c18fa4 100644 --- a/tests/__snapshots__/EditorTest__test__debugInfo__1.json +++ b/tests/__snapshots__/EditorTest__test__debugInfo__1.json @@ -3,6 +3,12 @@ "x": 0, "y": 0 }, + "document": { + "syntax": null, + "tokens": [], + "rows": [], + "filename": null + }, "offset": { "x": 0, "y": 0 @@ -11,9 +17,14 @@ "filename": "", "renderX": 0, "rows": [], - "screenCols": 80, - "screenRows": 23, - "statusMsg": "", + "terminalSize": { + "rows": 21, + "cols": 80 + }, + "statusMessage": { + "text": "HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find", + "time": 1234567890 + }, "syntax": null, "tokens": [] }