2023-11-21 15:14:08 -05:00
|
|
|
import Row from './row.ts';
|
2024-07-09 17:02:16 -04:00
|
|
|
import { arrayInsert, maxAdd, minSub } from './fns.ts';
|
2024-07-03 16:13:29 -04:00
|
|
|
import Option, { None, Some } from './option.ts';
|
2023-11-14 15:53:45 -05:00
|
|
|
import { getRuntime } from './runtime.ts';
|
2024-07-09 17:02:16 -04:00
|
|
|
import { Position, SearchDirection } from './types.ts';
|
2023-11-13 14:46:04 -05:00
|
|
|
|
|
|
|
export class Document {
|
|
|
|
#rows: Row[];
|
2023-11-30 16:14:52 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Has the document been modified?
|
|
|
|
*/
|
|
|
|
public dirty: boolean;
|
2023-11-13 14:46:04 -05:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
this.#rows = [];
|
2023-11-21 15:14:08 -05:00
|
|
|
this.dirty = false;
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
|
|
|
|
2023-11-16 11:10:33 -05:00
|
|
|
get numRows(): number {
|
2023-11-13 14:46:04 -05:00
|
|
|
return this.#rows.length;
|
|
|
|
}
|
|
|
|
|
2023-11-27 10:25:30 -05:00
|
|
|
public static default(): Document {
|
2024-07-09 17:02:16 -04:00
|
|
|
return new Document();
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
public isEmpty(): boolean {
|
|
|
|
return this.#rows.length === 0;
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:24:22 -05:00
|
|
|
/**
|
|
|
|
* Open a file for editing
|
|
|
|
*/
|
2023-11-14 15:53:45 -05:00
|
|
|
public async open(filename: string): Promise<Document> {
|
|
|
|
const { file } = await getRuntime();
|
|
|
|
|
|
|
|
// Clear any existing rows
|
|
|
|
if (!this.isEmpty()) {
|
|
|
|
this.#rows = [];
|
|
|
|
}
|
2023-11-13 14:46:04 -05:00
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
const rawFile = await file.openFile(filename);
|
|
|
|
rawFile.split(/\r?\n/)
|
2023-11-22 17:09:41 -05:00
|
|
|
.forEach((row) => this.insertRow(this.numRows, row));
|
2023-11-13 14:46:04 -05:00
|
|
|
|
2023-11-21 15:14:08 -05:00
|
|
|
this.dirty = false;
|
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
return this;
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
|
|
|
|
2024-02-29 14:24:22 -05:00
|
|
|
/**
|
|
|
|
* Save the current document
|
|
|
|
*/
|
2024-07-02 16:27:18 -04:00
|
|
|
public async save(filename: string): Promise<void> {
|
2023-11-21 15:14:08 -05:00
|
|
|
const { file } = await getRuntime();
|
|
|
|
|
2023-11-27 10:25:30 -05:00
|
|
|
await file.saveFile(filename, this.rowsToString());
|
2023-11-21 15:14:08 -05:00
|
|
|
|
|
|
|
this.dirty = false;
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:14:52 -05:00
|
|
|
public find(
|
|
|
|
q: string,
|
2024-07-09 17:02:16 -04:00
|
|
|
at: Position,
|
|
|
|
direction: SearchDirection = SearchDirection.Forward,
|
2024-07-03 16:13:29 -04:00
|
|
|
): Option<Position> {
|
2024-07-09 17:02:16 -04:00
|
|
|
if (at.y >= this.numRows) {
|
|
|
|
return None;
|
|
|
|
}
|
2024-07-03 16:13:29 -04:00
|
|
|
|
2024-07-09 17:02:16 -04:00
|
|
|
const position = Position.from(at);
|
2024-06-21 14:14:10 -04:00
|
|
|
|
2024-07-09 17:02:16 -04:00
|
|
|
const start = (direction === SearchDirection.Forward) ? at.y : 0;
|
|
|
|
const end = (direction === SearchDirection.Forward)
|
|
|
|
? this.numRows
|
|
|
|
: maxAdd(at.y, 1, this.numRows);
|
2024-06-21 14:14:10 -04:00
|
|
|
|
2024-07-09 17:02:16 -04:00
|
|
|
for (let y = start; y < end; y++) {
|
|
|
|
if (this.row(position.y).isNone()) {
|
|
|
|
return None;
|
|
|
|
}
|
2024-06-21 14:14:10 -04:00
|
|
|
|
2024-07-09 17:02:16 -04:00
|
|
|
const maybeMatch = this.#rows[y].find(q, position.x, direction);
|
|
|
|
if (maybeMatch.isSome()) {
|
|
|
|
position.x = maybeMatch.unwrap();
|
|
|
|
return Some(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction === SearchDirection.Forward) {
|
|
|
|
position.y = maxAdd(position.y, 1, this.numRows - 1);
|
|
|
|
position.x = 0;
|
|
|
|
} else {
|
|
|
|
position.y = minSub(position.y, 1, 0);
|
|
|
|
|
|
|
|
console.assert(position.y < this.numRows);
|
|
|
|
|
|
|
|
position.x = this.#rows[position.y].size - 1;
|
2024-06-21 14:14:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 17:02:16 -04:00
|
|
|
return None;
|
2023-11-30 16:14:52 -05:00
|
|
|
}
|
|
|
|
|
2023-11-21 15:14:08 -05:00
|
|
|
public insert(at: Position, c: string): void {
|
|
|
|
if (at.y === this.numRows) {
|
2023-11-22 17:09:41 -05:00
|
|
|
this.insertRow(this.numRows, c);
|
2023-11-21 15:14:08 -05:00
|
|
|
} else {
|
|
|
|
this.#rows[at.y].insertChar(at.x, c);
|
2024-07-03 16:13:29 -04:00
|
|
|
this.#rows[at.y].update(None);
|
2023-11-21 15:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
2024-07-02 16:27:18 -04:00
|
|
|
/**
|
|
|
|
* Insert a new line, splitting and/or creating a new row as needed
|
|
|
|
*/
|
2023-11-22 17:09:41 -05:00
|
|
|
public insertNewline(at: Position): void {
|
|
|
|
if (at.y > this.numRows) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-02 16:27:18 -04:00
|
|
|
// Just add a simple blank line
|
2023-11-22 17:09:41 -05:00
|
|
|
if (at.y === this.numRows) {
|
|
|
|
this.#rows.push(Row.default());
|
2024-07-02 16:27:18 -04:00
|
|
|
this.dirty = true;
|
2023-11-22 17:09:41 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-02 16:27:18 -04:00
|
|
|
// Split the current row, and insert a new
|
|
|
|
// row with the leftovers
|
2023-11-22 17:09:41 -05:00
|
|
|
const newRow = this.#rows[at.y].split(at.x);
|
2024-07-03 16:13:29 -04:00
|
|
|
newRow.update(None);
|
2023-11-22 17:09:41 -05:00
|
|
|
this.#rows = arrayInsert(this.#rows, at.y + 1, newRow);
|
|
|
|
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:24:22 -05:00
|
|
|
/**
|
|
|
|
* Remove a character from the document, merging
|
|
|
|
* adjacent lines if necessary
|
|
|
|
*/
|
2023-11-22 11:07:33 -05:00
|
|
|
public delete(at: Position): void {
|
2023-11-22 15:11:32 -05:00
|
|
|
const len = this.numRows;
|
|
|
|
if (at.y >= len) {
|
2023-11-22 11:07:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-05 09:27:28 -04:00
|
|
|
const maybeRow = this.row(at.y);
|
|
|
|
if (maybeRow.isNone()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const row = maybeRow.unwrap();
|
|
|
|
|
2023-11-30 11:15:37 -05:00
|
|
|
const mergeNextRow = at.x === row.size && at.y + 1 < len;
|
2023-11-22 15:11:32 -05:00
|
|
|
const mergeIntoPrevRow = at.x === 0 && at.y > 0;
|
|
|
|
|
|
|
|
// If we are at the end of a line, and press delete,
|
|
|
|
// add the contents of the next row, and delete
|
|
|
|
// the merged row object
|
|
|
|
if (mergeNextRow) {
|
|
|
|
// At the end of a line, pressing delete will merge
|
|
|
|
// the next line into the current on
|
|
|
|
const rowToAppend = this.#rows.at(at.y + 1)!.toString();
|
|
|
|
row.append(rowToAppend);
|
|
|
|
this.deleteRow(at.y + 1);
|
|
|
|
} else if (mergeIntoPrevRow) {
|
|
|
|
// At the beginning of a line, merge the current line
|
|
|
|
// into the previous Row
|
|
|
|
const rowToAppend = row.toString();
|
|
|
|
this.#rows[at.y - 1].append(rowToAppend);
|
|
|
|
this.deleteRow(at.y);
|
|
|
|
} else {
|
|
|
|
row.delete(at.x);
|
|
|
|
}
|
|
|
|
|
2024-07-03 16:13:29 -04:00
|
|
|
row.update(None);
|
2023-11-22 11:27:46 -05:00
|
|
|
|
|
|
|
this.dirty = true;
|
2023-11-22 11:07:33 -05:00
|
|
|
}
|
|
|
|
|
2024-07-05 09:27:28 -04:00
|
|
|
public row(i: number): Option<Row> {
|
2024-07-09 17:02:16 -04:00
|
|
|
if (i >= this.numRows) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-07-05 09:27:28 -04:00
|
|
|
return Option.from(this.#rows[i]);
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
2023-11-14 15:53:45 -05:00
|
|
|
|
2023-11-22 17:09:41 -05:00
|
|
|
public insertRow(at: number = this.numRows, s: string = ''): void {
|
|
|
|
this.#rows = arrayInsert(this.#rows, at, Row.from(s));
|
2024-07-03 16:13:29 -04:00
|
|
|
this.#rows[at].update(None);
|
2023-11-21 15:14:08 -05:00
|
|
|
|
|
|
|
this.dirty = true;
|
2023-11-20 15:14:36 -05:00
|
|
|
}
|
|
|
|
|
2024-07-03 16:13:29 -04:00
|
|
|
public highlight(searchMatch: Option<string>): void {
|
2024-06-26 15:55:47 -04:00
|
|
|
this.#rows.forEach((row) => {
|
|
|
|
row.update(searchMatch);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-22 15:11:32 -05:00
|
|
|
/**
|
|
|
|
* Delete the specified row
|
|
|
|
* @param at - the index of the row to delete
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private deleteRow(at: number): void {
|
|
|
|
this.#rows.splice(at, 1);
|
|
|
|
}
|
|
|
|
|
2023-11-22 17:09:41 -05:00
|
|
|
/**
|
|
|
|
* Convert the array of row objects into one string
|
|
|
|
* @private
|
|
|
|
*/
|
2023-11-21 15:14:08 -05:00
|
|
|
private rowsToString(): string {
|
|
|
|
return this.#rows.map((r) => r.toString()).join('\n');
|
|
|
|
}
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Document;
|