all: clean up code, upgrade to modern Go

Signed-off-by: ddkwork
This commit is contained in:
Admin
2025-05-05 23:27:04 +08:00
committed by Elias Naur
parent 86668e8b45
commit f73287be87
58 changed files with 413 additions and 241 deletions
+2 -2
View File
@@ -929,7 +929,7 @@ func (e *Editor) replace(start, end int, s string, addHistory bool) int {
if addHistory {
deleted := make([]rune, 0, replaceSize)
readPos := e.text.ByteOffset(start)
for i := 0; i < replaceSize; i++ {
for range replaceSize {
ru, s, _ := e.text.ReadRuneAt(int64(readPos))
readPos += int64(s)
deleted = append(deleted, ru)
@@ -1021,7 +1021,7 @@ func (e *Editor) deleteWord(distance int) (deletedRunes int) {
return r
}
runes := 1
for ii := 0; ii < words; ii++ {
for range words {
r := next(runes)
wantSpace := unicode.IsSpace(r)
for r := next(runes); unicode.IsSpace(r) == wantSpace && !atEnd(runes); r = next(runes) {
+2 -2
View File
@@ -992,7 +992,7 @@ func TestEditorSelectShortcuts(t *testing.T) {
tFont := font.Font{}
tFontSize := unit.Sp(10)
tShaper := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection()))
var tEditor = &Editor{
tEditor := &Editor{
SingleLine: false,
ReadOnly: true,
}
@@ -1255,7 +1255,7 @@ func TestNoFilterAllocs(t *testing.T) {
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
e.Update(gtx)
}
})
+10 -5
View File
@@ -31,7 +31,8 @@ func TestFit(t *testing.T) {
Dims: image.Point{50, 200},
Scale: f32.Point{X: 1, Y: 1},
Result: image.Point{X: 50, Y: 100},
}},
},
},
Contain: {
{
Dims: image.Point{50, 25},
@@ -41,7 +42,8 @@ func TestFit(t *testing.T) {
Dims: image.Point{50, 200},
Scale: f32.Point{X: 0.5, Y: 0.5},
Result: image.Point{X: 25, Y: 100},
}},
},
},
Cover: {
{
Dims: image.Point{50, 25},
@@ -51,7 +53,8 @@ func TestFit(t *testing.T) {
Dims: image.Point{50, 200},
Scale: f32.Point{X: 2, Y: 2},
Result: image.Point{X: 100, Y: 100},
}},
},
},
ScaleDown: {
{
Dims: image.Point{50, 25},
@@ -61,7 +64,8 @@ func TestFit(t *testing.T) {
Dims: image.Point{50, 200},
Scale: f32.Point{X: 0.5, Y: 0.5},
Result: image.Point{X: 25, Y: 100},
}},
},
},
Fill: {
{
Dims: image.Point{50, 25},
@@ -71,7 +75,8 @@ func TestFit(t *testing.T) {
Dims: image.Point{50, 200},
Scale: f32.Point{X: 2, Y: 0.5},
Result: image.Point{X: 100, Y: 100},
}},
},
},
}
for fit, tests := range fittests {
+22 -17
View File
@@ -73,10 +73,11 @@ func makeAccountingTestText(str string, fontSize, lineWidth int) (txt []text.Gly
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
shaper := text.NewShaper(text.NoSystemFonts(), text.WithCollection([]font.FontFace{{
Font: font.Font{Typeface: "LTR"},
Face: ltrFace,
},
shaper := text.NewShaper(text.NoSystemFonts(), text.WithCollection([]font.FontFace{
{
Font: font.Font{Typeface: "LTR"},
Face: ltrFace,
},
{
Font: font.Font{Typeface: "RTL"},
Face: rtlFace,
@@ -99,10 +100,11 @@ func getGlyphs(fontSize, minWidth, lineWidth int, align text.Alignment, str stri
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
shaper := text.NewShaper(text.NoSystemFonts(), text.WithCollection([]font.FontFace{{
Font: font.Font{Typeface: "LTR"},
Face: ltrFace,
},
shaper := text.NewShaper(text.NoSystemFonts(), text.WithCollection([]font.FontFace{
{
Font: font.Font{Typeface: "LTR"},
Face: ltrFace,
},
{
Font: font.Font{Typeface: "RTL"},
Face: rtlFace,
@@ -245,7 +247,7 @@ func TestIndexPositionWhitespace(t *testing.T) {
if len(gi.positions) != len(tc.expected) {
t.Errorf("expected %d positions, got %d", len(tc.expected), len(gi.positions))
}
for i := 0; i < min(len(gi.positions), len(tc.expected)); i++ {
for i := range min(len(gi.positions), len(tc.expected)) {
actual := gi.positions[i]
expected := tc.expected[i]
if actual != expected {
@@ -258,7 +260,6 @@ func TestIndexPositionWhitespace(t *testing.T) {
}
})
}
}
// TestIndexPositionBidi tests whether the index correct generates cursor positions for
@@ -312,7 +313,7 @@ func TestIndexPositionBidi(t *testing.T) {
lastLine := 0
lastCol := -1
lastY := 0
for i := 0; i < min(len(gi.positions), len(tc.expectedXs)); i++ {
for i := range min(len(gi.positions), len(tc.expectedXs)) {
actualX := gi.positions[i].x
expectedX := tc.expectedXs[i]
if actualX != expectedX {
@@ -538,7 +539,7 @@ func TestIndexPositionLines(t *testing.T) {
if len(gi.lines) != len(tc.expectedLines) {
t.Errorf("expected %d lines, got %d", len(tc.expectedLines), len(gi.lines))
}
for i := 0; i < min(len(gi.lines), len(tc.expectedLines)); i++ {
for i := range min(len(gi.lines), len(tc.expectedLines)) {
actual := gi.lines[i]
expected := tc.expectedLines[i]
if actual != expected {
@@ -623,7 +624,7 @@ func TestIndexPositionRunes(t *testing.T) {
if len(gi.positions) != len(tc.expected) {
t.Errorf("expected %d positions, got %d", len(tc.expected), len(gi.positions))
}
for i := 0; i < min(len(gi.positions), len(tc.expected)); i++ {
for i := range min(len(gi.positions), len(tc.expected)) {
actual := gi.positions[i]
expected := tc.expected[i]
if expected.runes != actual.runes {
@@ -646,6 +647,7 @@ func TestIndexPositionRunes(t *testing.T) {
})
}
}
func printPositions(t *testing.T, positions []combinedPos) {
t.Helper()
for i, p := range positions {
@@ -720,7 +722,7 @@ func TestGraphemeReaderNext(t *testing.T) {
if len(asRunes) != len(runes) {
t.Errorf("expected %d runes, got %d", len(asRunes), len(runes))
}
for i := 0; i < max(len(asRunes), len(runes)); i++ {
for i := range max(len(asRunes), len(runes)) {
if i < min(len(asRunes), len(runes)) {
if runes[i] != asRunes[i] {
t.Errorf("expected runes[%d]=%d, got %d", i, asRunes[i], runes[i])
@@ -734,6 +736,7 @@ func TestGraphemeReaderNext(t *testing.T) {
})
}
}
func TestGraphemeReaderGraphemes(t *testing.T) {
latinDoc := bytes.NewReader([]byte(latinDocument))
arabicDoc := bytes.NewReader([]byte(arabicDocument))
@@ -787,7 +790,7 @@ func TestGraphemeReaderGraphemes(t *testing.T) {
if len(asRunes)+1 < len(graphemes) {
t.Errorf("expected <= %d graphemes, got %d", len(asRunes)+1, len(graphemes))
}
for i := 0; i < len(graphemes)-1; i++ {
for i := range len(graphemes) - 1 {
if graphemes[i] >= graphemes[i+1] {
t.Errorf("graphemes[%d](%d) >= graphemes[%d](%d)", i, graphemes[i], i+1, graphemes[i+1])
}
@@ -795,6 +798,7 @@ func TestGraphemeReaderGraphemes(t *testing.T) {
})
}
}
func BenchmarkGraphemeReaderNext(b *testing.B) {
latinDoc := bytes.NewReader([]byte(latinDocument))
arabicDoc := bytes.NewReader([]byte(arabicDocument))
@@ -831,7 +835,7 @@ func BenchmarkGraphemeReaderNext(b *testing.B) {
var paragraph []rune = make([]rune, 4096)
b.Run(tc.name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
pr.SetSource(tc.input)
ok := true
@@ -844,6 +848,7 @@ func BenchmarkGraphemeReaderNext(b *testing.B) {
})
}
}
func BenchmarkGraphemeReaderGraphemes(b *testing.B) {
latinDoc := bytes.NewReader([]byte(latinDocument))
arabicDoc := bytes.NewReader([]byte(arabicDocument))
@@ -879,7 +884,7 @@ func BenchmarkGraphemeReaderGraphemes(b *testing.B) {
} {
b.Run(tc.name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
pr.SetSource(tc.input)
for g := tc.read(); len(g) > 0; g = tc.read() {
_ = g
+1 -1
View File
@@ -717,7 +717,7 @@ func (e *textView) MoveWord(distance int, selAct selectionAction) {
}
return r
}
for ii := 0; ii < words; ii++ {
for range words {
for r := next(); unicode.IsSpace(r) && !atEnd(); r = next() {
e.MoveCaret(direction, 0)
caret = e.closestToRune(e.caret.start)
+4 -4
View File
@@ -92,7 +92,7 @@ func BenchmarkLabelStatic(b *testing.B) {
runesStr := string(runes)
l := Label{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
l.Layout(gtx, cache, font, fontSize, runesStr, op.CallOp{})
if render {
win.Frame(gtx.Ops)
@@ -124,7 +124,7 @@ func BenchmarkLabelDynamic(b *testing.B) {
l := Label{}
r := rand.New(rand.NewSource(42))
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
// simulate a constantly changing string
a := r.Intn(len(runes))
b := r.Intn(len(runes))
@@ -161,7 +161,7 @@ func BenchmarkEditorStatic(b *testing.B) {
e := Editor{}
e.SetText(runesStr)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
if render {
win.Frame(gtx.Ops)
@@ -194,7 +194,7 @@ func BenchmarkEditorDynamic(b *testing.B) {
e.SetText(string(runes))
r := rand.New(rand.NewSource(42))
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
// simulate a constantly changing string
a := r.Intn(e.Len())
b := r.Intn(e.Len())