widget: push calls to Editor.makeValid to lower-level indexPosition

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-02-09 12:59:21 +01:00
parent 430aee39be
commit e32925d6a3
+1 -13
View File
@@ -553,7 +553,6 @@ func (e *Editor) Layout(gtx layout.Context, sh text.Shaper, font text.Font, size
// updateSnippet adds a key.SnippetOp if the snippet content or position // updateSnippet adds a key.SnippetOp if the snippet content or position
// have changed. off and len are in runes. // have changed. off and len are in runes.
func (e *Editor) updateSnippet(gtx layout.Context, start, end int) { func (e *Editor) updateSnippet(gtx layout.Context, start, end int) {
e.makeValid()
if start > end { if start > end {
start, end = end, start start, end = end, start
} }
@@ -711,7 +710,6 @@ func (e *Editor) PaintCaret(gtx layout.Context) {
if !e.caret.on { if !e.caret.on {
return return
} }
e.makeValid()
carWidth := fixed.I(gtx.Px(unit.Dp(1))) carWidth := fixed.I(gtx.Px(unit.Dp(1)))
caretStart := e.closestPosition(combinedPos{runes: e.caret.start}) caretStart := e.closestPosition(combinedPos{runes: e.caret.start})
carX := caretStart.x carX := caretStart.x
@@ -746,7 +744,6 @@ func (e *Editor) PaintCaret(gtx layout.Context) {
// Len is the length of the editor contents, in runes. // Len is the length of the editor contents, in runes.
func (e *Editor) Len() int { func (e *Editor) Len() int {
e.makeValid()
end := e.closestPosition(combinedPos{runes: math.MaxInt}) end := e.closestPosition(combinedPos{runes: math.MaxInt})
return end.runes return end.runes
} }
@@ -840,7 +837,6 @@ func (e *Editor) layoutText(s text.Shaper) ([]text.Line, layout.Dimensions) {
// CaretPos returns the line & column numbers of the caret. // CaretPos returns the line & column numbers of the caret.
func (e *Editor) CaretPos() (line, col int) { func (e *Editor) CaretPos() (line, col int) {
e.makeValid()
caret := e.closestPosition(combinedPos{runes: e.caret.start}) caret := e.closestPosition(combinedPos{runes: e.caret.start})
return caret.lineCol.Y, caret.lineCol.X return caret.lineCol.Y, caret.lineCol.X
} }
@@ -848,13 +844,13 @@ func (e *Editor) CaretPos() (line, col int) {
// CaretCoords returns the coordinates of the caret, relative to the // CaretCoords returns the coordinates of the caret, relative to the
// editor itself. // editor itself.
func (e *Editor) CaretCoords() f32.Point { func (e *Editor) CaretCoords() f32.Point {
e.makeValid()
caret := e.closestPosition(combinedPos{runes: e.caret.start}) caret := e.closestPosition(combinedPos{runes: e.caret.start})
return f32.Pt(float32(caret.x)/64, float32(caret.y)) return f32.Pt(float32(caret.x)/64, float32(caret.y))
} }
// indexPosition returns the latest position from the index no later than pos. // indexPosition returns the latest position from the index no later than pos.
func (e *Editor) indexPosition(pos combinedPos) combinedPos { func (e *Editor) indexPosition(pos combinedPos) combinedPos {
e.makeValid()
// Initialize index with first caret position. // Initialize index with first caret position.
if len(e.index) == 0 { if len(e.index) == 0 {
l := e.lines[0] l := e.lines[0]
@@ -1001,7 +997,6 @@ func (e *Editor) replace(start, end int, s string) {
if e.SingleLine { if e.SingleLine {
s = strings.ReplaceAll(s, "\n", " ") s = strings.ReplaceAll(s, "\n", " ")
} }
e.makeValid()
if start > end { if start > end {
start, end = end, start start, end = end, start
} }
@@ -1028,7 +1023,6 @@ func (e *Editor) replace(start, end int, s string) {
} }
func (e *Editor) movePages(pages int, selAct selectionAction) { func (e *Editor) movePages(pages int, selAct selectionAction) {
e.makeValid()
caret := e.closestPosition(combinedPos{runes: e.caret.start}) caret := e.closestPosition(combinedPos{runes: e.caret.start})
x := caret.x + e.caret.xoff x := caret.x + e.caret.xoff
y := caret.y + pages*e.viewSize.Y y := caret.y + pages*e.viewSize.Y
@@ -1042,7 +1036,6 @@ func (e *Editor) movePages(pages int, selAct selectionAction) {
// relative to their current positions. Positive distances moves forward, // relative to their current positions. Positive distances moves forward,
// negative distances moves backward. Distances are in runes. // negative distances moves backward. Distances are in runes.
func (e *Editor) MoveCaret(startDelta, endDelta int) { func (e *Editor) MoveCaret(startDelta, endDelta int) {
e.makeValid()
e.caret.xoff = 0 e.caret.xoff = 0
e.caret.start = e.closestPosition(combinedPos{runes: e.caret.start + startDelta}).runes e.caret.start = e.closestPosition(combinedPos{runes: e.caret.start + startDelta}).runes
e.caret.end = e.closestPosition(combinedPos{runes: e.caret.end + endDelta}).runes e.caret.end = e.closestPosition(combinedPos{runes: e.caret.end + endDelta}).runes
@@ -1070,7 +1063,6 @@ func (e *Editor) moveEnd(selAct selectionAction) {
// Positive is forward, negative is backward. // Positive is forward, negative is backward.
// Absolute values greater than one will skip that many words. // Absolute values greater than one will skip that many words.
func (e *Editor) moveWord(distance int, selAct selectionAction) { func (e *Editor) moveWord(distance int, selAct selectionAction) {
e.makeValid()
// split the distance information into constituent parts to be // split the distance information into constituent parts to be
// used independently. // used independently.
words, direction := distance, 1 words, direction := distance, 1
@@ -1116,8 +1108,6 @@ func (e *Editor) deleteWord(distance int) {
return return
} }
e.makeValid()
if e.caret.start != e.caret.end { if e.caret.start != e.caret.end {
e.Delete(1) e.Delete(1)
distance -= sign(distance) distance -= sign(distance)
@@ -1175,7 +1165,6 @@ func (e *Editor) deleteWord(distance int) {
} }
func (e *Editor) scrollToCaret() { func (e *Editor) scrollToCaret() {
e.makeValid()
caret := e.closestPosition(combinedPos{runes: e.caret.start}) caret := e.closestPosition(combinedPos{runes: e.caret.start})
l := e.lines[caret.lineCol.Y] l := e.lines[caret.lineCol.Y]
if e.SingleLine { if e.SingleLine {
@@ -1220,7 +1209,6 @@ func (e *Editor) Selection() (start, end int) {
// SetCaret moves the caret to start, and sets the selection end to end. start // SetCaret moves the caret to start, and sets the selection end to end. start
// and end are in runes, and represent offsets into the editor text. // and end are in runes, and represent offsets into the editor text.
func (e *Editor) SetCaret(start, end int) { func (e *Editor) SetCaret(start, end int) {
e.makeValid()
e.caret.start = e.closestPosition(combinedPos{runes: start}).runes e.caret.start = e.closestPosition(combinedPos{runes: start}).runes
e.caret.end = e.closestPosition(combinedPos{runes: end}).runes e.caret.end = e.closestPosition(combinedPos{runes: end}).runes
e.caret.scroll = true e.caret.scroll = true