package operator import ( "git.gophernest.net/azpect/TextEditor/internal/action" "git.gophernest.net/azpect/TextEditor/internal/core" tea "github.com/charmbracelet/bubbletea" ) // DeleteOperator implements Operator (d) - deletes text in various modes. type DeleteOperator struct{} // DeleteOperator.Operate: Deletes text based on the current mode and motion type. func (o DeleteOperator) Operate(m action.Model, start, end core.Position, mtype core.MotionType) tea.Cmd { switch m.Mode() { case core.VisualMode: deleteCharSelection(m, start, end) case core.VisualLineMode: deleteLineSelection(m, start, end) case core.VisualBlockMode: deleteBlockSelection(m, start, end) case core.NormalMode: deleteNormalMode(m, start, end, mtype) } return nil } // Verify DeleteOperator implements DoublePresser var _ action.DoublePresser = DeleteOperator{} // DeleteOperator.DoublePress: Handles dd - deletes Count entire lines. func (o DeleteOperator) DoublePress(m action.Model, count int) tea.Cmd { win := m.ActiveWindow() buf := m.ActiveBuffer() // If we have a higher value than lines remaining, we can only run so many times opCount := min(count, buf.LineCount()-win.Cursor.Line) var lines []string for range opCount { y := win.Cursor.Line lines = append(lines, buf.Line(y)) buf.DeleteLine(y) if buf.LineCount() == 0 { buf.InsertLine(0, "") } if y >= buf.LineCount() { y = buf.LineCount() - 1 } win.SetCursorLine(y) } // Put her in the register! m.UpdateDefaultRegister(core.LinewiseRegister, lines) return nil } // deleteNormalMode: Deletes text in normal mode based on motion type. func deleteNormalMode(m action.Model, start, end core.Position, mtype core.MotionType) { // Normalize so start is always before or equal to end if start.Line > end.Line || (start.Line == end.Line && start.Col > end.Col) { start, end = end, start } // Linewise motions (j, k, G, gg) always operate on whole lines if mtype == core.Linewise { deleteLineSelection(m, start, end) return } // Charwise motions on same line if start.Line == end.Line { // No movement = nothing to delete if start.Col == end.Col && mtype == core.CharwiseExclusive { return } // Exclusive motion: end position not included, so back up one if mtype == core.CharwiseExclusive { end.Col-- } if end.Col >= start.Col { deleteCharSelection(m, start, end) } return } // Charwise motion spanning multiple lines (e.g., d/search) deleteCharSelection(m, start, end) } // deleteCharSelection: Deletes a character-wise selection. func deleteCharSelection(m action.Model, start, end core.Position) { win := m.ActiveWindow() buf := m.ActiveBuffer() if start.Line == end.Line { line := buf.Line(start.Line) endCol := min(end.Col+1, len(line)) buf.SetLine(start.Line, line[:start.Col]+line[endCol:]) } else { startLine := buf.Line(start.Line) endLine := buf.Line(end.Line) prefix := startLine[:start.Col] suffix := "" if end.Col+1 < len(endLine) { suffix = endLine[end.Col+1:] } // Delete from end back to start to preserve indices for i := end.Line; i >= start.Line; i-- { buf.DeleteLine(i) } buf.InsertLine(start.Line, prefix+suffix) } win.SetCursorLine(start.Line) win.SetCursorCol(start.Col) } // deleteLineSelection: Deletes entire lines in a selection range. func deleteLineSelection(m action.Model, start, end core.Position) { win := m.ActiveWindow() buf := m.ActiveBuffer() var lines []string for i := end.Line; i >= start.Line; i-- { lines = append(lines, buf.Line(i)) buf.DeleteLine(i) } if buf.LineCount() == 0 { buf.InsertLine(0, "") } y := start.Line if y >= buf.LineCount() { y = buf.LineCount() - 1 } win.SetCursorLine(y) // Update registers m.UpdateDefaultRegister(core.LinewiseRegister, lines) } // deleteBlockSelection: Deletes a rectangular block selection. func deleteBlockSelection(m action.Model, start, end core.Position) { win := m.ActiveWindow() buf := m.ActiveBuffer() startCol := min(start.Col, end.Col) endCol := max(start.Col, end.Col) for y := start.Line; y <= end.Line; y++ { line := buf.Line(y) if startCol >= len(line) { continue } ec := min(endCol+1, len(line)) buf.SetLine(y, line[:startCol]+line[ec:]) } win.SetCursorLine(start.Line) win.SetCursorCol(startCol) }