Răsfoiți Sursa

Add delete line

Johan Rong 1 lună în urmă
părinte
comite
d114ff9e3d
3 a modificat fișierele cu 49 adăugiri și 3 ștergeri
  1. 44 0
      internal/buffer/buffer.go
  2. 1 3
      internal/editor/action.go
  3. 4 0
      internal/editor/keyinput.go

+ 44 - 0
internal/buffer/buffer.go

@@ -122,6 +122,50 @@ func (buf *Buffer) Delete() {
 	buf.CM.DeduplicateAndSort()
 }
 
+func (buf *Buffer) DeleteLine() {
+    buf.ensureRope()
+    buf.CM.DeduplicateAndSort()
+
+    if buf.Rope.Len() == 0 || len(buf.CM.Cursors) == 0 {
+        return
+    }
+
+    primary := buf.CM.Cursors[buf.CM.PrimaryIdx]
+    line, _ := LineCol(buf, primary.Offset)
+
+    start := OffsetForLine(buf, line)
+    end := buf.Rope.Len()
+
+	if line+1 < LineCount(buf) {
+        end = OffsetForLine(buf, line+1)
+    } else if line > 0 && start > 0 && buf.Rope.At(start-1) == '\n' {
+        start--
+    }
+
+    if end <= start {
+        return
+    }
+
+    buf.Rope.Remove(start, end)
+    buf.LI.RemoveAt(start, end)
+
+    newLine := line
+    if newLine >= LineCount(buf) {
+        newLine = LineCount(buf) - 1
+    }
+    if newLine < 0 {
+        newLine = 0
+    }
+    newOffset := OffsetForLine(buf, newLine)
+
+    for i := range buf.CM.Cursors {
+        buf.CM.Cursors[i].Offset = newOffset
+        buf.CM.Cursors[i].Goal = 0
+    }
+
+    buf.CM.DeduplicateAndSort()
+}
+
 func (buf *Buffer) Clear() {
 	buf.ensureRope()
 

+ 1 - 3
internal/editor/action.go

@@ -256,9 +256,7 @@ func DefaultActionManager() ActionManager {
 					},
 				},
 				Callback: func(m *Model, args []string) {
-					m.BM.PaletteBuffer.Clear()
-					m.BM.PaletteBuffer.Insert("moose.error:TODO: line delete not implemented yet")
-					return
+					m.BM.Current().DeleteLine()
 				},
 			},
 			Action{

+ 4 - 0
internal/editor/keyinput.go

@@ -8,6 +8,10 @@ import (
 func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
 	hasValidContinuingChordPath := false
 
+	// if m.Mode == ModeNormal && util.StandardizeBinding(ev.Name()) is numeric { // type number in normal mode to repeat a keybind x amount of times
+	// m.AM.NumericCM.Recording = true, .... along these lines
+	//}
+
 	for _, action := range append(m.CurrentActionSet(), m.AM.Common...) {
 		for _, binding := range action.Bindings {
 			switch binding.Type {