about summary refs log tree commit diff stats
path: root/edit
diff options
context:
space:
mode:
Diffstat (limited to 'edit')
-rw-r--r--edit/002-typing.mu59
1 files changed, 57 insertions, 2 deletions
diff --git a/edit/002-typing.mu b/edit/002-typing.mu
index c6c8edd5..041a6421 100644
--- a/edit/002-typing.mu
+++ b/edit/002-typing.mu
@@ -874,12 +874,16 @@ def insert-new-line-and-indent editor:&:editor, screen:&:screen -> editor:&:edit
   left:num <- get *editor, left:offset
   right:num <- get *editor, right:offset
   screen-height:num <- screen-height screen
+  at-start-of-wrapped-line?:bool <- at-start-of-wrapped-line? editor
   # insert newline
   insert 10/newline, before-cursor
   before-cursor <- next before-cursor
   *editor <- put *editor, before-cursor:offset, before-cursor
-  cursor-row <- add cursor-row, 1
-  *editor <- put *editor, cursor-row:offset, cursor-row
+  {
+    break-if at-start-of-wrapped-line?
+    cursor-row <- add cursor-row, 1
+    *editor <- put *editor, cursor-row:offset, cursor-row
+  }
   cursor-column <- copy left
   *editor <- put *editor, cursor-column:offset, cursor-column
   # maybe scroll
@@ -906,6 +910,24 @@ def insert-new-line-and-indent editor:&:editor, screen:&:screen -> editor:&:edit
   }
 ]
 
+def at-start-of-wrapped-line? editor:&:editor -> result:bool [
+  local-scope
+  load-ingredients
+  left:num <- get *editor, left:offset
+  cursor-column:num <- get *editor, cursor-column:offset
+  cursor-at-left?:bool <- equal cursor-column, left
+  return-unless cursor-at-left?, 0/false
+  before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
+  before-before-cursor:&:duplex-list:char <- prev before-cursor
+  return-unless before-before-cursor, 0/false  # cursor is at start of editor
+  char-before-cursor:char <- get *before-cursor, value:offset
+  cursor-after-newline?:bool <- equal char-before-cursor, 10/newline
+  return-if cursor-after-newline?, 0/false
+  # if cursor is at left margin and not at start, but previous character is not a newline,
+  # then we're at start of a wrapped line
+  return 1/true
+]
+
 # takes a pointer 'curr' into the doubly-linked list and its sentinel, counts
 # the number of spaces at the start of the line containing 'curr'.
 def line-indent curr:&:duplex-list:char, start:&:duplex-list:char -> result:num [
@@ -986,6 +1008,39 @@ scenario editor-clears-previous-line-completely-after-inserting-newline [
   ]
 ]
 
+scenario editor-splits-wrapped-line-after-inserting-newline [
+  local-scope
+  assume-screen 10/width, 5/height
+  e:&:editor <- new-editor [abcdef], 0/left, 5/right
+  editor-render screen, e
+  screen-should-contain [
+    .          .
+    .abcd↩     .
+    .ef        .
+    .┈┈┈┈┈     .
+    .          .
+  ]
+  assume-console [
+    left-click 2, 0
+    press enter
+  ]
+  run [
+    editor-event-loop screen, console, e
+    10:num/raw <- get *e, cursor-row:offset
+    11:num/raw <- get *e, cursor-column:offset
+  ]
+  screen-should-contain [
+    .          .
+    .abcd      .
+    .ef        .
+    .┈┈┈┈┈     .
+  ]
+  memory-should-contain [
+    10 <- 2  # cursor-row
+    11 <- 0  # cursor-column
+  ]
+]
+
 scenario editor-inserts-indent-after-newline [
   local-scope
   assume-screen 10/width, 10/height