Skip to content

Commit

Permalink
textcore: moving things around, adding pos history update for buffer …
Browse files Browse the repository at this point in the history
…switch
  • Loading branch information
rcoreilly committed Feb 18, 2025
1 parent 6237529 commit 1a75a39
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 55 deletions.
58 changes: 8 additions & 50 deletions text/textcore/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,6 @@ func (ed *Base) Init() {
}
})

// ed.handleKeyChord()
// ed.handleMouse()
// ed.handleLinkCursor()
// ed.handleFocus()
ed.OnClose(func(e events.Event) {
ed.editDone()
})
Expand Down Expand Up @@ -344,57 +340,19 @@ func (ed *Base) SetLines(buf *lines.Lines) *Base {
ed.SetLines(nil)
ed.SendClose()
})
// bhl := len(buf.posHistory) // todo:
// if bhl > 0 {
// cp := buf.posHistory[bhl-1]
// ed.posHistoryIndex = bhl - 1
// buf.Unlock()
// ed.SetCursorShow(cp)
// } else {
// ed.SetCursorShow(textpos.Pos{})
// }
} else {
ed.viewId = -1
}
ed.NeedsRender()
return ed
}

//////// Undo / Redo

// undo undoes previous action
func (ed *Base) undo() {
tbes := ed.Lines.Undo()
if tbes != nil {
tbe := tbes[len(tbes)-1]
if tbe.Delete { // now an insert
ed.SetCursorShow(tbe.Region.End)
phl := buf.PosHistoryLen()
if phl > 0 {
cp, _ := buf.PosHistoryAt(phl - 1)
ed.posHistoryIndex = phl - 1
ed.SetCursorShow(cp)
} else {
ed.SetCursorShow(tbe.Region.Start)
ed.SetCursorShow(textpos.Pos{})
}
} else {
ed.SendInput() // updates status..
ed.scrollCursorToCenterIfHidden()
}
ed.savePosHistory(ed.CursorPos)
ed.NeedsRender()
}

// redo redoes previously undone action
func (ed *Base) redo() {
tbes := ed.Lines.Redo()
if tbes != nil {
tbe := tbes[len(tbes)-1]
if tbe.Delete {
ed.SetCursorShow(tbe.Region.Start)
} else {
ed.SetCursorShow(tbe.Region.End)
}
} else {
ed.scrollCursorToCenterIfHidden()
ed.viewId = -1
}
ed.savePosHistory(ed.CursorPos)
ed.NeedsRender()
return ed
}

// styleBase applies the editor styles.
Expand Down
9 changes: 4 additions & 5 deletions text/textcore/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ func (ed *Editor) keyInput(e events.Event) {

// cancelAll cancels search, completer, and..
cancelAll := func() {
// todo:
// ed.CancelComplete()
// ed.cancelCorrect()
// ed.iSearchCancel()
// ed.qReplaceCancel()
ed.CancelComplete()
ed.cancelCorrect()
ed.iSearchCancel()
ed.qReplaceCancel()
ed.lastAutoInsert = 0
}

Expand Down
39 changes: 39 additions & 0 deletions text/textcore/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func (ed *Base) selectAll() {
ed.NeedsRender()
}

// todo: cleanup

// isWordEnd returns true if the cursor is just past the last letter of a word
// word is a string of characters none of which are classified as a word break
func (ed *Base) isWordEnd(tp textpos.Pos) bool {
Expand Down Expand Up @@ -225,6 +227,43 @@ func (ed *Base) SelectReset() {
ed.previousSelectRegion = textpos.Region{}
}

//////// Undo / Redo

// undo undoes previous action
func (ed *Base) undo() {
tbes := ed.Lines.Undo()
if tbes != nil {
tbe := tbes[len(tbes)-1]
if tbe.Delete { // now an insert
ed.SetCursorShow(tbe.Region.End)
} else {
ed.SetCursorShow(tbe.Region.Start)
}
} else {
ed.SendInput() // updates status..
ed.scrollCursorToCenterIfHidden()
}
ed.savePosHistory(ed.CursorPos)
ed.NeedsRender()
}

// redo redoes previously undone action
func (ed *Base) redo() {
tbes := ed.Lines.Redo()
if tbes != nil {
tbe := tbes[len(tbes)-1]
if tbe.Delete {
ed.SetCursorShow(tbe.Region.Start)
} else {
ed.SetCursorShow(tbe.Region.End)
}
} else {
ed.scrollCursorToCenterIfHidden()
}
ed.savePosHistory(ed.CursorPos)
ed.NeedsRender()
}

//////// Cut / Copy / Paste

// editorClipboardHistory is the [Base] clipboard history; everything that has been copied
Expand Down

0 comments on commit 1a75a39

Please sign in to comment.