Compare commits

..

No commits in common. "a50c22c0e07e668b82b4ab70b616dec58ab17d42" and "29569997379bcdd77b26341dfbdf873ea04b2306" have entirely different histories.

10 changed files with 145 additions and 149 deletions

View File

@ -14,7 +14,7 @@
</coverage> </coverage>
<testsuites> <testsuites>
<testsuite name="PHPKilo"> <testsuite name="PHPKilo">
<directory phpVersion="8.0.0" phpVersionOperator="&gt;=">tests</directory> <directory phpVersion="7.4.0" phpVersionOperator="&gt;=">tests</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<logging/> <logging/>

View File

@ -105,12 +105,12 @@ class Document {
if ($c === KeyType::ENTER || $c === RawKeyCode::CARRIAGE_RETURN) if ($c === KeyType::ENTER || $c === RawKeyCode::CARRIAGE_RETURN)
{ {
$this->insertNewline($at); $this->insertNewline($at);
$this->dirty = true;
return; return;
} }
$this->rows[$at->y]->insert($at->x, $c);
$this->dirty = true; $this->dirty = true;
$this->rows[$at->y]->insert($at->x, $c);
} }
public function delete(Point $at): void public function delete(Point $at): void
@ -120,6 +120,7 @@ class Document {
return; return;
} }
$this->dirty = true;
$row =& $this->rows[$at->y]; $row =& $this->rows[$at->y];
if ($at->x === $this->rows[$at->y]->size && $at->y + 1 < $this->numRows) if ($at->x === $this->rows[$at->y]->size && $at->y + 1 < $this->numRows)
@ -131,8 +132,6 @@ class Document {
{ {
$row->delete($at->x); $row->delete($at->x);
} }
$this->dirty = true;
} }
public function insertRow(int $at, string $s, bool $updateSyntax = TRUE): void public function insertRow(int $at, string $s, bool $updateSyntax = TRUE): void
@ -172,8 +171,6 @@ class Document {
{ {
$this->refreshPHPSyntax(); $this->refreshPHPSyntax();
} }
$this->dirty = true;
} }
protected function deleteRow(int $at): void protected function deleteRow(int $at): void
@ -226,13 +223,11 @@ class Document {
$newChars = substr($chars, 0, $at->x); $newChars = substr($chars, 0, $at->x);
// Truncate the previous row // Truncate the previous row
$row->setChars($newChars); $row->chars = $newChars;
// Add a new row with the contents of the previous row at the point of the split // Add a new row with the contents of the previous row at the point of the split
$this->insertRow($at->y + 1, substr($chars, $at->x)); $this->insertRow($at->y + 1, substr($chars, $at->x));
} }
$this->dirty = true;
} }
protected function selectSyntaxHighlight(): void protected function selectSyntaxHighlight(): void
@ -260,7 +255,7 @@ class Document {
public function refreshSyntax(): void public function refreshSyntax(): void
{ {
// Update the syntax highlighting for all the rows of the file // Update the syntax highlighting for all the rows of the file
array_walk($this->rows, static fn (Row $row) => $row->update()); array_walk($this->rows, static fn (Row $row) => $row->highlight());
} }
private function refreshPHPSyntax(): void private function refreshPHPSyntax(): void

View File

@ -8,10 +8,10 @@ use Aviat\Kilo\Enum\RawKeyCode;
/** /**
* @property-read int $size * @property-read int $size
* @property-read int $rsize * @property-read int $rsize
* @property-read string $chars * @property string $chars
*/ */
class Row { class Row {
// use Traits\MagicProperties; use Traits\MagicProperties;
/** /**
* The version of the row to be displayed (where tabs are converted to display spaces) * The version of the row to be displayed (where tabs are converted to display spaces)
@ -28,6 +28,8 @@ class Row {
*/ */
private bool $hlOpenComment = FALSE; private bool $hlOpenComment = FALSE;
private const T_RAW = -1;
/** /**
* Create a row in the current document * Create a row in the current document
* *
@ -87,6 +89,15 @@ class Row {
}; };
} }
public function __set(string $name, mixed $value): void
{
if ($name === 'chars')
{
$this->chars = $value;
$this->highlight();
}
}
/** /**
* Convert the row contents to a string for saving * Convert the row contents to a string for saving
* *
@ -141,6 +152,8 @@ class Row {
// Safely insert into arbitrary position in the existing string // Safely insert into arbitrary position in the existing string
$this->chars = substr($this->chars, 0, $at) . $c . substr($this->chars, $at); $this->chars = substr($this->chars, 0, $at) . $c . substr($this->chars, $at);
$this->update(); $this->update();
$this->parent->dirty = true;
} }
/** /**
@ -152,6 +165,8 @@ class Row {
{ {
$this->chars .= $s; $this->chars .= $s;
$this->update(); $this->update();
$this->parent->dirty = true;
} }
/** /**
@ -168,12 +183,8 @@ class Row {
$this->chars = substr_replace($this->chars, '', $at, 1); $this->chars = substr_replace($this->chars, '', $at, 1);
$this->update(); $this->update();
}
public function setChars(string $chars): void $this->parent->dirty = true;
{
$this->chars = $chars;
$this->update();
} }
/** /**
@ -218,7 +229,7 @@ class Row {
while ($i < $this->rsize) while ($i < $this->rsize)
{ {
// Multi-line comments // Multi-line comments
if ($syntax->mlComments() && $inString === '') if ($mcsLen > 0 && $mceLen > 0 && $inString === '')
{ {
if ($inComment) if ($inComment)
{ {
@ -246,14 +257,15 @@ class Row {
if ( if (
$this->highlightComment($i, $syntax) $this->highlightComment($i, $syntax)
|| $this->highlightMultilineComments($i, $syntax)
|| $this->highlightPrimaryKeywords($i, $syntax) || $this->highlightPrimaryKeywords($i, $syntax)
|| $this->highlightSecondaryKeywords($i, $syntax) || $this->highlightSecondaryKeywords($i, $syntax)
|| $this->highlightCharacter($i, $syntax)
|| $this->highlightString($i, $syntax)
|| $this->highlightNumber($i, $syntax)
|| $this->highlightOperators($i, $syntax) || $this->highlightOperators($i, $syntax)
|| $this->highlightCommonOperators($i) || $this->highlightCommonOperators($i)
|| $this->highlightCommonDelimeters($i) || $this->highlightCommonDelimeters($i)
|| $this->highlightCharacter($i, $syntax)
|| $this->highlightString($i, $syntax)
|| $this->highlightNumber($i, $syntax)
) { ) {
$i++; $i++;
continue; continue;
@ -389,11 +401,6 @@ class Row {
protected function highlightCharacter(int &$i, Syntax $opts): bool protected function highlightCharacter(int &$i, Syntax $opts): bool
{ {
if (($i + 1) >= $this->rsize)
{
return false;
}
$char = $this->render[$i]; $char = $this->render[$i];
$nextChar = $this->render[$i + 1]; $nextChar = $this->render[$i + 1];
@ -440,6 +447,35 @@ class Row {
return false; return false;
} }
protected function highlightMultilineComments(int &$i, Syntax $opts): bool
{
if ( ! $opts->mlComments())
{
return false;
}
$mcs = $opts->multiLineCommentStart;
$mce = $opts->multiLineCommentEnd;
$mcsLen = strlen($mcs);
$mceLen = strlen($mce);
if ($i + $mcsLen < $this->rsize && \str_contains($this->render, $mcs))
{
$endix = strpos($this->render, $mcs);
$closingIndex = ($endix !== false)
? $i + $endix + $mcsLen + $mceLen
: $this->rsize;
array_replace_range($this->hl, $i, $closingIndex, Highlight::ML_COMMENT);
$i += $closingIndex;
return true;
}
return false;
}
protected function highlightString(int &$i, Syntax $opts): bool protected function highlightString(int &$i, Syntax $opts): bool
{ {
$char = $this->render[$i]; $char = $this->render[$i];
@ -589,12 +625,17 @@ class Row {
$highlight = match(true) { $highlight = match(true) {
// Matches a predefined PHP token // Matches a predefined PHP token
$token['type'] !== T_RAW && $tokenHighlight !== Highlight::NORMAL $token['type'] !== self::T_RAW && $tokenHighlight !== Highlight::NORMAL
=> $tokenHighlight, => $tokenHighlight,
// Matches a specific syntax character // Matches a specific syntax character
$charHighlight !== Highlight::NORMAL => $charHighlight, $charHighlight !== Highlight::NORMAL => $charHighlight,
// Types/identifiers/keywords that don't have their own token, but are
// defined as keywords
in_array($token['char'], $this->parent->fileType->syntax->keywords2, TRUE)
=> Highlight::KEYWORD2,
default => Highlight::NORMAL, default => Highlight::NORMAL,
}; };

View File

@ -6,7 +6,6 @@ use PhpToken;
use function Aviat\Kilo\str_contains; use function Aviat\Kilo\str_contains;
use function Aviat\Kilo\tabs_to_spaces; use function Aviat\Kilo\tabs_to_spaces;
use const Aviat\Kilo\T_RAW;
class PHP8 extends PhpToken { class PHP8 extends PhpToken {
private array $rawLines = []; private array $rawLines = [];
@ -120,7 +119,7 @@ class PHP8 extends PhpToken {
// Simple characters, usually delimiters or single character operators // Simple characters, usually delimiters or single character operators
$this->tokens[$lineNumber][] = [ $this->tokens[$lineNumber][] = [
'type' => T_RAW, 'type' => -1,
'typeName' => 'RAW', 'typeName' => 'RAW',
'char' => tabs_to_spaces($token), 'char' => tabs_to_spaces($token),
'line' => $lineNumber, 'line' => $lineNumber,

View File

@ -10,4 +10,3 @@ const KILO_TAB_STOP = 4;
const KILO_QUIT_TIMES = 3; const KILO_QUIT_TIMES = 3;
const NO_MATCH = -1; const NO_MATCH = -1;
const T_RAW = -1;

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace Aviat\Kilo\Tests; namespace Aviat\Kilo\Tests\Traits;
use Aviat\Kilo\ANSI; use Aviat\Kilo\ANSI;
use Aviat\Kilo\Enum\Color; use Aviat\Kilo\Enum\Color;

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace Aviat\Kilo\Tests; namespace Aviat\Kilo\Tests\Traits;
use Aviat\Kilo\Editor; use Aviat\Kilo\Editor;
use Aviat\Kilo\Type\TerminalSize; use Aviat\Kilo\Type\TerminalSize;
@ -47,6 +47,8 @@ class EditorTest extends TestCase {
public function testOpen(): void public function testOpen(): void
{ {
$this->markTestSkipped();
$editor = MockEditor::mock('src/ffi.h'); $editor = MockEditor::mock('src/ffi.h');
$state = json_encode($editor->__debugInfo(), JSON_THROW_ON_ERROR); $state = json_encode($editor->__debugInfo(), JSON_THROW_ON_ERROR);

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace Aviat\Kilo\Tests; namespace Aviat\Kilo\Tests\Traits;
use Aviat\Kilo\ use Aviat\Kilo\
{ {
@ -17,9 +17,7 @@ class RowTest extends TestCase {
parent::setUp(); parent::setUp();
$this->document = Document::new(); $this->document = Document::new();
$this->document->insertRow(0, ''); $this->row = Row::new($this->document, '', 0);
$this->row = $this->document->rows[0];
} }
public function testSanity(): void public function testSanity(): void
@ -33,14 +31,17 @@ class RowTest extends TestCase {
public function testSetRunsUpdate(): void public function testSetRunsUpdate(): void
{ {
$this->row->setChars('abcde'); $this->markTestSkipped('Hangs');
$this->assertNotEmpty($this->row->chars);
$this->row->chars = 'abcde';
$this->assertEquals('abcde', $this->row->render); $this->assertEquals('abcde', $this->row->render);
} }
public function test__toString(): void public function test__toString(): void
{ {
$this->row->setChars('abcde'); $this->markTestSkipped('Hangs');
$this->row->chars = 'abcde';
$this->assertEquals("abcde\n", (string)$this->row); $this->assertEquals("abcde\n", (string)$this->row);
} }
@ -61,7 +62,9 @@ class RowTest extends TestCase {
public function testInsert(): void public function testInsert(): void
{ {
$this->row->setChars('abde'); $this->markTestSkipped('Hangs');
$this->row->chars = 'abde';
$this->row->insert(2, 'c'); $this->row->insert(2, 'c');
$this->assertEquals('abcde', $this->row->chars); $this->assertEquals('abcde', $this->row->chars);
@ -71,7 +74,9 @@ class RowTest extends TestCase {
public function testInsertBadOffset(): void public function testInsertBadOffset(): void
{ {
$this->row->setChars('ab'); $this->markTestSkipped('Hangs');
$this->row->chars = 'ab';
$this->row->insert(5, 'c'); $this->row->insert(5, 'c');
$this->assertEquals('abc', $this->row->chars); $this->assertEquals('abc', $this->row->chars);
@ -81,7 +86,9 @@ class RowTest extends TestCase {
public function testDelete(): void public function testDelete(): void
{ {
$this->row->setChars('abcdef'); $this->markTestSkipped('Hangs');
$this->row->chars = 'abcdef';
$this->row->delete(5); $this->row->delete(5);
$this->assertEquals('abcde', $this->row->chars); $this->assertEquals('abcde', $this->row->chars);
@ -91,9 +98,12 @@ class RowTest extends TestCase {
public function testDeleteBadOffset(): void public function testDeleteBadOffset(): void
{ {
$this->row->setChars('ab'); $this->markTestSkipped('Hangs');
$this->row->chars = 'ab';
$this->row->delete(5); $this->row->delete(5);
$this->assertEquals('ab', $this->row->chars); $this->assertEquals('ab', $this->row->chars);
$this->assertEquals(false, $this->document->dirty);
} }
} }

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace Aviat\Kilo\Tests; namespace Aviat\Kilo\Tests\Traits;
use Aviat\Kilo\{Termios, TermiosException}; use Aviat\Kilo\{Termios, TermiosException};
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;

View File

@ -10,13 +10,7 @@
"tokens": [], "tokens": [],
"filetype": "C", "filetype": "C",
"keywords1": [ "keywords1": [
"auto",
"break",
"case",
"const",
"continue", "continue",
"default",
"do",
"typedef", "typedef",
"switch", "switch",
"return", "return",
@ -24,23 +18,13 @@
"while", "while",
"break", "break",
"struct", "struct",
"extern",
"union", "union",
"class", "class",
"else", "else",
"enum", "enum",
"for", "for",
"case", "case",
"if", "if"
"inline",
"register",
"restrict",
"return",
"sizeof",
"switch",
"typedef",
"union",
"volatile"
], ],
"keywords2": [ "keywords2": [
"#include", "#include",
@ -54,50 +38,16 @@
"float", "float",
"#error", "#error",
"#undef", "#undef",
"#elif",
"long", "long",
"char", "char",
"int", "int",
"void", "void",
"#if", "#if"
"uint32_t",
"wchar_t",
"int32_t",
"int64_t",
"uint64_t",
"int16_t",
"uint16_t",
"uint8_t",
"int8_t"
],
"operators": [
"<=>",
"<<=",
">>=",
"++",
"--",
"==",
"!=",
">=",
"<=",
"&&",
"||",
"<<",
">>",
"+=",
"-=",
"*=",
"\/=",
"%=",
"&=",
"|=",
"^=",
"->",
"::"
], ],
"singleLineCommentStart": "\/\/", "singleLineCommentStart": "\/\/",
"multiLineCommentStart": "\/*", "multiLineCommentStart": "\/*",
"multiLineCommentEnd": "*\/" "multiLineCommentEnd": "*\/",
"flags": 3
} }
}, },
"tokens": [], "tokens": [],
@ -620,11 +570,11 @@
4, 4,
4, 4,
4, 4,
7,
0, 0,
0, 0,
0, 0,
7, 0,
0,
0, 0,
5, 5,
5, 5,
@ -673,11 +623,11 @@
0, 0,
0, 0,
0, 0,
7,
0, 0,
13, 0,
13, 5,
13, 5,
5,
0 0
], ],
"idx": 17 "idx": 17
@ -983,7 +933,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 25 "idx": 25
}, },
@ -1061,7 +1011,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 28 "idx": 28
}, },
@ -1139,7 +1089,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 31 "idx": 31
}, },
@ -1211,7 +1161,7 @@
{ {
"render": "{", "render": "{",
"hl": [ "hl": [
9 0
], ],
"idx": 35 "idx": 35
}, },
@ -1263,7 +1213,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 37 "idx": 37
}, },
@ -1321,7 +1271,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 40 "idx": 40
}, },
@ -1380,7 +1330,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 43 "idx": 43
}, },
@ -1437,7 +1387,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 46 "idx": 46
}, },
@ -1494,10 +1444,10 @@
0, 0,
0, 0,
0, 0,
9,
0, 0,
6,
6,
0, 0,
9,
0 0
], ],
"idx": 49 "idx": 49
@ -1571,7 +1521,6 @@
0, 0,
0, 0,
0, 0,
7,
0, 0,
0, 0,
0, 0,
@ -1581,14 +1530,15 @@
0, 0,
0, 0,
0, 0,
7 0,
0
], ],
"idx": 52 "idx": 52
}, },
{ {
"render": "};", "render": "};",
"hl": [ "hl": [
9, 0,
0 0
], ],
"idx": 53 "idx": 53
@ -1615,15 +1565,15 @@
0, 0,
0, 0,
0, 0,
9, 0,
4,
4,
4,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0,
7,
0,
3, 3,
3, 3,
3, 3,
@ -1639,7 +1589,6 @@
0, 0,
0, 0,
0, 0,
7,
0, 0,
0, 0,
0, 0,
@ -1649,7 +1598,8 @@
0, 0,
0, 0,
0, 0,
9, 0,
0,
0 0
], ],
"idx": 55 "idx": 55
@ -1671,15 +1621,15 @@
0, 0,
0, 0,
0, 0,
9, 0,
4,
4,
4,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0,
7,
0,
4, 4,
4, 4,
4, 4,
@ -1700,13 +1650,13 @@
0, 0,
0, 0,
0, 0,
7,
0, 0,
3, 0,
3, 0,
3, 0,
3, 0,
3, 0,
0,
0, 0,
3, 3,
3, 3,
@ -1723,7 +1673,6 @@
0, 0,
0, 0,
0, 0,
7,
0, 0,
0, 0,
0, 0,
@ -1733,7 +1682,8 @@
0, 0,
0, 0,
0, 0,
9, 0,
0,
0 0
], ],
"idx": 56 "idx": 56
@ -1956,7 +1906,7 @@
0, 0,
0, 0,
0, 0,
9 0
], ],
"idx": 61 "idx": 61
}, },
@ -1988,7 +1938,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 62 "idx": 62
}, },
@ -2020,7 +1970,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 63 "idx": 63
}, },
@ -2055,7 +2005,7 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 64 "idx": 64
}, },
@ -2090,14 +2040,14 @@
0, 0,
0, 0,
0, 0,
7 0
], ],
"idx": 65 "idx": 65
}, },
{ {
"render": "};", "render": "};",
"hl": [ "hl": [
9, 0,
0 0
], ],
"idx": 66 "idx": 66
@ -2115,21 +2065,21 @@
0, 0,
0, 0,
0, 0,
9,
0,
0,
0,
7,
0, 0,
4, 4,
4, 4,
4, 4,
7, 0,
0,
4,
4,
4,
0,
0,
0, 0,
0, 0,
0, 0,
0, 0,
9,
0 0
], ],
"idx": 67 "idx": 67