From 422babff5a09bc36583cb9ed1a963ee8046cc9a3 Mon Sep 17 00:00:00 2001 From: Kyle Loveless Date: Mon, 11 Nov 2024 22:11:09 -0600 Subject: [PATCH] limit right scrolling and snap to end of line --- main.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index 026658a..fff5307 100644 --- a/main.ts +++ b/main.ts @@ -266,6 +266,7 @@ function editorRefreshScreen() { } function editorMoveCursor(key: string | number) { + let row = (e.cursorY >= e.numRows) ? "" : e.row[e.cursorY]; switch (key) { case EditorKey.ARROW_LEFT: if (e.cursorX != 0) { @@ -273,7 +274,9 @@ function editorMoveCursor(key: string | number) { } break; case EditorKey.ARROW_RIGHT: - e.cursorX++; + if (row != "" && e.cursorX < row.length) { + e.cursorX++; + } break; case EditorKey.ARROW_UP: if (e.cursorY != 0) { @@ -286,6 +289,12 @@ function editorMoveCursor(key: string | number) { } break; } + + row = (e.cursorY >= e.numRows) ? "" : e.row[e.cursorY]; + const rowLen = row != "" ? row.length : 0; + if (e.cursorX > rowLen) { + e.cursorX = rowLen; + } } function editorProcessKeypress() {