Skip to content

Commit d020289

Browse files
committed
wip
2 parents c288adf + eb9b6c5 commit d020289

File tree

3 files changed

+52
-59
lines changed

3 files changed

+52
-59
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/MakeNowJust/heredoc v1.0.0
77
github.com/atotto/clipboard v0.1.4
8-
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20241121171714-fbd5423ea935
8+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20241126192050-a8ed96118b08
99
github.com/charmbracelet/harmonica v0.2.0
1010
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20241121164047-8448a9be4804
1111
github.com/charmbracelet/x/ansi v0.5.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
66
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
77
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
88
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
9-
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20241121171714-fbd5423ea935 h1:S+hhEwWnJxDeZMtHqIHgGVilNWsez3xmFOpwSc9GbcE=
10-
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20241121171714-fbd5423ea935/go.mod h1:BbC4R+6e9TLjbskxrjISt/DDCn4OiB6v+ArqfYiPyyg=
9+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20241126192050-a8ed96118b08 h1:8hwULvCHjF6JjaeosebMGbB06oCv46d4s+Lbs5ytAT4=
10+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20241126192050-a8ed96118b08/go.mod h1:BbC4R+6e9TLjbskxrjISt/DDCn4OiB6v+ArqfYiPyyg=
1111
github.com/charmbracelet/colorprofile v0.1.8 h1:PywDeXsiAzlPtkiiKgMEVLvb6nlEuKrMj9+FJBtj4jU=
1212
github.com/charmbracelet/colorprofile v0.1.8/go.mod h1:+jpmObxZl1Dab3H3IMVIPSZTsKcFpjJUv97G0dLqM60=
1313
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=

textarea/textarea.go

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,12 @@ type Model struct {
293293
// Cursor row.
294294
row int
295295

296-
// The bubble offset in the parent model.
296+
// The bubble offset relative to the parent bubble.
297297
offsetX, offsetY int
298298

299+
// The last recorded real cursor position.
300+
realCol, realRow int
301+
299302
// Last character offset, used to maintain state when the cursor is moved
300303
// vertically such that we can maintain the same navigating position.
301304
lastCharOffset int
@@ -383,6 +386,11 @@ func DefaultDarkStyles() Styles {
383386
return DefaultStyles(true)
384387
}
385388

389+
// SetOffset sets the offset of the textarea relative to the parent bubble.
390+
func (m *Model) SetOffset(x, y int) {
391+
m.offsetX, m.offsetY = x, y
392+
}
393+
386394
// SetValue sets the value of the text input.
387395
func (m *Model) SetValue(s string) {
388396
m.Reset()
@@ -536,11 +544,6 @@ func (m Model) Line() int {
536544
return m.row
537545
}
538546

539-
// SetOffset sets the bubble offset in the parent model.
540-
func (m *Model) SetOffset(x, y int) {
541-
m.offsetX, m.offsetY = x, y
542-
}
543-
544547
// CursorDown moves the cursor down by one line.
545548
// Returns whether or not the cursor blink should be reset.
546549
func (m *Model) CursorDown() {
@@ -661,6 +664,8 @@ func (m *Model) Reset() {
661664
m.value = make([][]rune, minHeight, maxLines)
662665
m.col = 0
663666
m.row = 0
667+
m.realCol = 0
668+
m.realRow = 0
664669
m.viewport.GotoTop()
665670
m.SetCursor(0)
666671
}
@@ -1026,15 +1031,16 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
10261031
}
10271032

10281033
// Need to check for completion before, because key is configurable and might be double assigned
1029-
keyMsg, ok := msg.(tea.KeyMsg)
1030-
if ok && key.Matches(keyMsg, m.KeyMap.AcceptSuggestion) {
1031-
if m.canAcceptSuggestion() {
1032-
m.value = m.matchedSuggestions[m.currentSuggestionIndex]
1033-
m.format()
1034-
m.row = len(m.value) - 1
1035-
m.CursorEnd()
1036-
}
1037-
}
1034+
// keyMsg, ok := msg.(tea.KeyMsg)
1035+
// if ok && key.Matches(keyMsg, m.KeyMap.AcceptSuggestion) {
1036+
// if m.canAcceptSuggestion() {
1037+
// m.value = m.matchedSuggestions[m.currentSuggestionIndex]
1038+
// m.format()
1039+
// m.row = len(m.value) - 1
1040+
// m.CursorEnd()
1041+
// m.SetSuggestions(nil)
1042+
// }
1043+
// }
10381044

10391045
// Used to determine if the cursor should blink.
10401046
oldRow, oldCol := m.cursorLineNumber(), m.col
@@ -1173,11 +1179,23 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
11731179
cmds = append(cmds, tea.SetCursorPosition(m.offsetX+newCol, m.offsetY+newRow))
11741180

11751181
m.Cursor, cmd = m.Cursor.Update(msg)
1176-
if (newRow != oldRow || newCol != oldCol) && m.Cursor.Mode() == cursor.CursorBlink {
1182+
if cmd != nil {
1183+
cmds = append(cmds, cmd)
1184+
}
1185+
1186+
if m.Cursor.Mode() == cursor.CursorBlink && (newRow != oldRow || newCol != oldCol) {
11771187
m.Cursor.Blink = false
1178-
cmd = m.Cursor.BlinkCmd()
1188+
cmds = append(cmds, m.Cursor.BlinkCmd())
1189+
}
1190+
1191+
// Ensure the real cursor is at the correct position.
1192+
row := m.cursorLineNumber()
1193+
lineInfo := m.LineInfo()
1194+
realCol, realRow := m.offsetX+lineInfo.CharOffset, m.offsetY+row-m.viewport.YOffset
1195+
if realCol != m.realCol || realRow != m.realRow {
1196+
m.realCol, m.realRow = realCol, realRow
1197+
cmds = append(cmds, tea.SetCursorPosition(realCol, realRow))
11791198
}
1180-
cmds = append(cmds, cmd)
11811199

11821200
m.repositionView()
11831201

@@ -1197,7 +1215,7 @@ func (m Model) suggestionView(offset int) string {
11971215

11981216
var lines []string
11991217
for _, line := range strings.Split(suggestion[len(m.Value())+offset:], "\n") {
1200-
lines = append(lines, m.style.Placeholder.Inline(true).Render(line))
1218+
lines = append(lines, m.activeStyle.Placeholder.Inline(true).Render(line))
12011219
}
12021220
if len(lines) > m.Height() {
12031221
m.SetHeight(len(lines) + 1)
@@ -1217,7 +1235,7 @@ func (m Model) View() string {
12171235
style lipgloss.Style
12181236
newLines int
12191237
widestLineNumber int
1220-
lineInfo = m.LineInfo()
1238+
// lineInfo = m.LineInfo()
12211239
)
12221240

12231241
displayLine := 0
@@ -1276,45 +1294,20 @@ func (m Model) View() string {
12761294
wrappedLine = []rune(strings.TrimSuffix(string(wrappedLine), " "))
12771295
padding -= m.width - strwidth
12781296
}
1279-
if m.row == l && lineInfo.RowOffset == wl {
1280-
ln := string(wrappedLine[:lineInfo.ColumnOffset])
1281-
if m.SyntaxHighlighter == nil {
1282-
ln = style.Render(ln)
1283-
} else {
1284-
ln = m.SyntaxHighlighter(ln)
1285-
}
1286-
s.WriteString(ln)
12871297

1288-
if m.col >= len(line) && lineInfo.CharOffset >= m.width {
1289-
m.Cursor.SetChar(" ")
1290-
s.WriteString(m.Cursor.View())
1291-
// XXX: suggestions?
1292-
} else {
1293-
m.Cursor.SetChar(string(wrappedLine[lineInfo.ColumnOffset]))
1294-
if m.canAcceptSuggestion() && len(m.matchedSuggestions) > 0 {
1295-
suggestion := m.matchedSuggestions[m.currentSuggestionIndex]
1296-
if len(suggestion) >= m.row {
1297-
suggestion = suggestion[m.row:]
1298-
}
1299-
m.Cursor.TextStyle = m.style.Placeholder
1300-
if len(suggestion) > m.row && len(suggestion[m.row]) > m.col {
1301-
m.Cursor.SetChar(string(suggestion[m.row][m.col]))
1302-
}
1303-
}
1304-
s.WriteString(style.Render(m.Cursor.View()))
1305-
s.WriteString(style.Render(string(wrappedLine[lineInfo.ColumnOffset+1:])))
1306-
s.WriteString(m.suggestionView(1))
1307-
// XXX: suggestions
1308-
}
1298+
ln = string(wrappedLine)
1299+
if m.SyntaxHighlighter == nil {
1300+
ln = style.Render(ln)
13091301
} else {
1310-
ln := string(wrappedLine)
1311-
if m.SyntaxHighlighter == nil {
1312-
ln = style.Render(ln)
1313-
} else {
1314-
ln = m.SyntaxHighlighter(ln)
1315-
}
1316-
s.WriteString(ln)
1302+
ln = m.SyntaxHighlighter(ln)
13171303
}
1304+
s.WriteString(ln)
1305+
1306+
// if m.col < len(line) || lineInfo.CharOffset < m.width {
1307+
// if m.canAcceptSuggestion() && len(m.matchedSuggestions) > 0 {
1308+
// s.WriteString(m.suggestionView(1))
1309+
// }
1310+
// }
13181311

13191312
pad := strings.Repeat(" ", max(0, padding))
13201313
if m.SyntaxHighlighter == nil {

0 commit comments

Comments
 (0)