62159
Sy
symb / files
symb / internal / tui / editor / update.go
5.4 KB
1package editor 2 3import tea "charm.land/bubbletea/v2" 4 5// --------------------------------------------------------------------------- 6// Update 7// --------------------------------------------------------------------------- 8 9func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { 10 var cmds []tea.Cmd 11 12 switch msg := msg.(type) { 13 case tea.KeyPressMsg: 14 if m.focus { 15 if moved := m.handleKeyPress(msg); moved { 16 m.clampCursor() 17 m.clampScroll() 18 } 19 } 20 case tea.MouseClickMsg, tea.MouseMotionMsg, tea.MouseReleaseMsg, tea.MouseWheelMsg: 21 if m.focus { 22 m.handleEditorMouse(msg) 23 } 24 } 25 26 var cmd tea.Cmd 27 m.cursor, cmd = m.cursor.Update(msg) 28 cmds = append(cmds, cmd) 29 30 return m, tea.Batch(cmds...) 31} 32 33// handleKeyPress dispatches a key event. Returns true if the cursor moved or content changed. 34func (m *Model) handleKeyPress(msg tea.KeyPressMsg) bool { 35 key := msg.Keystroke() 36 37 if handled := m.handleShiftNav(key); handled { 38 return true 39 } 40 if handled := m.handlePlainNav(key); handled { 41 return true 42 } 43 if handled := m.handleEditKey(key); handled { 44 return true 45 } 46 47 // Text insertion 48 if !m.ReadOnly && msg.Text != "" { 49 m.DeleteSelection() 50 for _, r := range msg.Text { 51 m.insertRune(r) 52 } 53 return true 54 } 55 return false 56} 57 58// handleShiftNav handles shift+arrow/pgup/pgdown/home/end for selection extension. 59func (m *Model) handleShiftNav(key string) bool { 60 switch key { 61 case "shift+up": 62 m.startOrExtendSelection() 63 m.row-- 64 m.clampCursor() 65 m.updateSelectionActive() 66 case "shift+down": 67 m.startOrExtendSelection() 68 m.row++ 69 m.clampCursor() 70 m.updateSelectionActive() 71 case "shift+left": 72 m.startOrExtendSelection() 73 if m.col > 0 { 74 m.col-- 75 } else if m.row > 0 { 76 m.row-- 77 m.col = len(m.currentLine()) 78 } 79 m.updateSelectionActive() 80 case "shift+right": 81 m.startOrExtendSelection() 82 if m.col < len(m.currentLine()) { 83 m.col++ 84 } else if m.row < len(m.lines)-1 { 85 m.row++ 86 m.col = 0 87 } 88 m.updateSelectionActive() 89 case "shift+home": 90 m.startOrExtendSelection() 91 m.col = 0 92 m.updateSelectionActive() 93 case "shift+end": 94 m.startOrExtendSelection() 95 m.col = len(m.currentLine()) 96 m.updateSelectionActive() 97 case "shift+pgup": 98 m.startOrExtendSelection() 99 m.row -= m.height 100 m.clampCursor() 101 m.updateSelectionActive() 102 case "shift+pgdown": 103 m.startOrExtendSelection() 104 m.row += m.height 105 m.clampCursor() 106 m.updateSelectionActive() 107 default: 108 return false 109 } 110 return true 111} 112 113// handlePlainNav handles plain arrow/home/end/pgup/pgdown navigation. 114func (m *Model) handlePlainNav(key string) bool { 115 switch key { 116 case "up": 117 m.ClearSelection() 118 m.row-- 119 m.clampCursor() 120 case "down": 121 m.ClearSelection() 122 m.row++ 123 m.clampCursor() 124 case "left": 125 m.ClearSelection() 126 if m.col > 0 { 127 m.col-- 128 } else if m.row > 0 { 129 m.row-- 130 m.col = len(m.currentLine()) 131 } 132 case "right": 133 m.ClearSelection() 134 if m.col < len(m.currentLine()) { 135 m.col++ 136 } else if m.row < len(m.lines)-1 { 137 m.row++ 138 m.col = 0 139 } 140 case "home", "ctrl+a": 141 m.ClearSelection() 142 m.col = 0 143 case "end", "ctrl+e": 144 m.ClearSelection() 145 m.col = len(m.currentLine()) 146 case "pgup": 147 m.ClearSelection() 148 m.row -= m.height 149 m.clampCursor() 150 case "pgdown": 151 m.ClearSelection() 152 m.row += m.height 153 m.clampCursor() 154 case "ctrl+home": 155 m.ClearSelection() 156 m.row = 0 157 m.col = 0 158 case "ctrl+end": 159 m.ClearSelection() 160 m.row = len(m.lines) - 1 161 m.col = len(m.currentLine()) 162 default: 163 return false 164 } 165 return true 166} 167 168// handleEditKey handles backspace, delete, enter, tab. 169func (m *Model) handleEditKey(key string) bool { 170 switch key { 171 case "backspace", "ctrl+h": 172 if m.HasSelection() { 173 m.DeleteSelection() 174 } else { 175 m.deleteBack() 176 } 177 case "delete": 178 if m.HasSelection() { 179 m.DeleteSelection() 180 } else { 181 m.deleteForward() 182 } 183 case "shift+enter": 184 if m.SubmitOnEnter { 185 m.DeleteSelection() 186 m.insertNewline() 187 } else { 188 return false 189 } 190 case "enter": 191 if m.SubmitOnEnter { 192 return false // Let parent handle submit 193 } 194 m.DeleteSelection() 195 m.insertNewline() 196 case "tab": 197 m.DeleteSelection() 198 m.tabIndent() 199 default: 200 return false 201 } 202 return true 203} 204 205// handleEditorMouse dispatches mouse events for the editor. 206func (m *Model) handleEditorMouse(msg tea.Msg) { 207 switch msg := msg.(type) { 208 case tea.MouseClickMsg: 209 if msg.Button == tea.MouseLeft { 210 p := m.screenToPos(msg.X, msg.Y) 211 m.dragging = true 212 m.sel = &selection{anchor: p, active: p} 213 m.row = p.row 214 m.col = p.col 215 m.clampCursor() 216 } 217 case tea.MouseMotionMsg: 218 if m.dragging { 219 p := m.screenToPos(msg.X, msg.Y) 220 m.sel.active = p 221 m.row = p.row 222 m.col = p.col 223 m.clampCursor() 224 } 225 case tea.MouseReleaseMsg: 226 m.dragging = false 227 if m.sel != nil && m.sel.empty() { 228 m.ClearSelection() 229 } 230 case tea.MouseWheelMsg: 231 if msg.Button == tea.MouseWheelUp { 232 m.scroll -= 3 233 m.clampScrollBounds() 234 } else if msg.Button == tea.MouseWheelDown { 235 m.scroll += 3 236 m.clampScrollBounds() 237 } 238 } 239} 240 241// screenToPos converts screen-relative x,y to a buffer row,col. 242// x,y are relative to the editor component origin. 243func (m *Model) screenToPos(x, y int) pos { 244 visRow := m.scroll + y 245 if visRow < 0 { 246 visRow = 0 247 } 248 bufRow, runeOffset := m.visualToBuffer(visRow) 249 250 col := x - m.gutterWidth 251 if col < 0 { 252 col = 0 253 } 254 // runeOffset is in expanded-tab space; convert back to buffer col. 255 // We need to find which buffer rune corresponds to runeOffset + col 256 // in the expanded string. 257 bufCol := m.expandedColToBufferCol(bufRow, runeOffset+col) 258 return pos{row: bufRow, col: bufCol} 259}