Make document.row getter return an option instead of a nullable

This commit is contained in:
Timothy Warren 2024-07-05 09:27:28 -04:00
parent 090d6262c3
commit 1cfdeece60
4 changed files with 34 additions and 25 deletions

View File

@ -323,10 +323,10 @@ const DocumentTest = {
doc.insert(Position.at(9, 0), 'buzz'); doc.insert(Position.at(9, 0), 'buzz');
assertEquals(doc.numRows, 1); assertEquals(doc.numRows, 1);
assertTrue(doc.dirty); assertTrue(doc.dirty);
const row0 = doc.row(0); const row0 = doc.row(0).unwrap();
assertEquals(row0?.toString(), 'foobazbarbuzz'); assertEquals(row0.toString(), 'foobazbarbuzz');
assertEquals(row0?.rstring(), 'foobazbarbuzz'); assertEquals(row0.rstring(), 'foobazbarbuzz');
assertEquals(row0?.rsize, 13); assertEquals(row0.rsize, 13);
doc.insert(Position.at(0, 1), 'Lorem Ipsum'); doc.insert(Position.at(0, 1), 'Lorem Ipsum');
assertEquals(doc.numRows, 2); assertEquals(doc.numRows, 2);

View File

@ -145,7 +145,13 @@ export class Document {
return; return;
} }
const row = this.row(at.y)!; const maybeRow = this.row(at.y);
if (maybeRow.isNone()) {
return;
}
const row = maybeRow.unwrap();
const mergeNextRow = at.x === row.size && at.y + 1 < len; const mergeNextRow = at.x === row.size && at.y + 1 < len;
const mergeIntoPrevRow = at.x === 0 && at.y > 0; const mergeIntoPrevRow = at.x === 0 && at.y > 0;
@ -173,8 +179,8 @@ export class Document {
this.dirty = true; this.dirty = true;
} }
public row(i: number): Row | null { public row(i: number): Option<Row> {
return this.#rows[i] ?? null; return Option.from(this.#rows[i]);
} }
public insertRow(at: number = this.numRows, s: string = ''): void { public insertRow(at: number = this.numRows, s: string = ''): void {

View File

@ -85,7 +85,7 @@ class Editor {
return this.#document.numRows; return this.#document.numRows;
} }
private get currentRow(): Row | null { private get currentRow(): Option<Row> {
return this.#document.row(this.#cursor.y); return this.#document.row(this.#cursor.y);
} }
@ -153,8 +153,8 @@ class Editor {
break; break;
case KeyCommand.End: case KeyCommand.End:
if (this.currentRow !== null) { if (this.currentRow.isSome()) {
this.#cursor.x = this.currentRow.size; this.#cursor.x = this.currentRow.unwrap().size;
} }
break; break;
@ -360,25 +360,27 @@ class Editor {
} }
private moveCursor(char: string): void { private moveCursor(char: string): void {
const rowSize = (this.currentRow.isSome())
? this.currentRow.unwrap().size
: 0;
switch (char) { switch (char) {
case KeyCommand.ArrowLeft: case KeyCommand.ArrowLeft:
if (this.#cursor.x > 0) { if (this.#cursor.x > 0) {
this.#cursor.x--; this.#cursor.x--;
} else if (this.#cursor.y > 0) { } else if (this.#cursor.y > 0) {
this.#cursor.y--; this.#cursor.y--;
this.#cursor.x = (this.currentRow !== null) this.#cursor.x = rowSize;
? this.currentRow.size
: 0;
} }
break; break;
case KeyCommand.ArrowRight: case KeyCommand.ArrowRight:
if ( if (
this.currentRow !== null && this.#cursor.x < this.currentRow.size this.currentRow.isSome() && this.#cursor.x < rowSize
) { ) {
this.#cursor.x++; this.#cursor.x++;
} else if ( } else if (
this.currentRow !== null && this.currentRow.isSome() &&
this.#cursor.x === this.currentRow.size this.#cursor.x === rowSize
) { ) {
this.#cursor.y++; this.#cursor.y++;
this.#cursor.x = 0; this.#cursor.x = 0;
@ -396,16 +398,15 @@ class Editor {
break; break;
} }
const rowLen = this.currentRow?.size ?? 0; if (this.#cursor.x > rowSize) {
if (this.#cursor.x > rowLen) { this.#cursor.x = rowSize;
this.#cursor.x = rowLen;
} }
} }
private scroll(): void { private scroll(): void {
this.#renderX = 0; this.#renderX = 0;
if (this.currentRow !== null) { if (this.currentRow.isSome()) {
this.#renderX = this.currentRow.cxToRx(this.#cursor.x); this.#renderX = this.currentRow.unwrap().cxToRx(this.#cursor.x);
} }
if (this.#cursor.y < this.#offset.y) { if (this.#cursor.y < this.#offset.y) {
@ -475,12 +476,14 @@ class Editor {
} }
private drawFileRow(y: number): void { private drawFileRow(y: number): void {
const row = this.#document.row(y); const maybeRow = this.#document.row(y);
if (row === null) { if (maybeRow.isNone()) {
log(`Trying to draw non-existent row '${y}'`, LogLevel.Warning); log(`Trying to draw non-existent row '${y}'`, LogLevel.Warning);
return this.drawPlaceholderRow(y); return this.drawPlaceholderRow(y);
} }
const row = maybeRow.unwrap();
const len = Math.min( const len = Math.min(
posSub(row.rsize, this.#offset.x), posSub(row.rsize, this.#offset.x),
this.#screen.cols, this.#screen.cols,

View File

@ -64,11 +64,11 @@ export class Search {
const current = this.getNextRow(parent.numRows); const current = this.getNextRow(parent.numRows);
const row = parent.row(current); const row = parent.row(current);
if (row === null) { if (row.isNone()) {
continue; continue;
} }
const possible = row.find(q); const possible = row.unwrap().find(q);
if (possible.isSome()) { if (possible.isSome()) {
this.lastMatch = current; this.lastMatch = current;
return possible.map((p: number) => Position.at(p, current)); return possible.map((p: number) => Position.at(p, current));