feat: port the client to MyGo native UI (0.3.0)

Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
This commit is contained in:
OpenChamber committed 2026-11-18 13:22:59 +08:00
1 parent 2fb4a8ea79
commit 6c0ee61750
2538 files changed
+675779 -4792

No files matched your search

+447
View File
@@ -0,0 +1,447 @@
// Package rope stores text as a persistent tree of chunks of UTF-8: an
// AVL tree whose leaves own up to 4 KB of the text each, and whose nodes
// sum the bytes, runes, UTF-16 units and newlines below them, as Zed's
// rope sums its chunks. An edit copies the chunks it touches and the path
// to them and shares the rest of the tree with the text before it, so a
// version of the text is a pointer, kept as long as wanted and read from
// any goroutine while others are made. ui.TextBuffer and ui/editor's
// buffers keep their text in it.
package rope
import (
"encoding/binary"
"fmt"
"io"
"strings"
"unicode/utf16"
"unicode/utf8"
)
// ChunkBytes is the most bytes a leaf holds.
const ChunkBytes = 4096
// Node is a node of the tree: a leaf holding text, or a branch of two.
// A nil node is the empty text.
type Node struct {
left, right *Node
text string // only leaves own bytes
sum Summary
height int
}
// Summary counts a text: its bytes, runes, UTF-16 units and newlines.
type Summary struct{ Bytes, Runes, Units, Lines int }
func (a Summary) add(b Summary) Summary {
return Summary{a.Bytes + b.Bytes, a.Runes + b.Runes, a.Units + b.Units, a.Lines + b.Lines}
}
// Sum returns the summary of the text of n.
func Sum(n *Node) Summary {
if n == nil {
return Summary{}
}
return n.sum
}
func height(n *Node) int {
if n == nil {
return 0
}
return n.height
}
// Summarize counts s.
func Summarize(s string) Summary {
n := CountRunes(s)
return Summary{len(s), n, unitsForRunes(s, n), strings.Count(s, "\n")}
}
// CountRunes counts the runes of s, ASCII eight bytes at a time, as most
// code and logs are.
func CountRunes(s string) int {
all := s
for len(s) >= 8 {
if binary.LittleEndian.Uint64([]byte(s))&0x8080808080808080 != 0 {
return utf8.RuneCountInString(all)
}
s = s[8:]
}
for i := range len(s) {
if s[i] >= utf8.RuneSelf {
return utf8.RuneCountInString(all)
}
}
return len(all)
}
func unitsForRunes(s string, n int) int {
if n == len(s) {
return n
}
for _, r := range s {
if r > 0xffff {
n++
}
}
return n
}
func leaf(s string) *Node {
if s == "" {
return nil
}
return &Node{text: strings.Clone(s), sum: Summarize(s), height: 1}
}
func branch(a, b *Node) *Node {
if a == nil {
return b
}
if b == nil {
return a
}
return &Node{left: a, right: b, sum: a.sum.add(b.sum), height: 1 + max(a.height, b.height)}
}
func balance(n *Node) *Node {
if height(n.left)-height(n.right) > 1 {
a := n.left
if height(a.left) < height(a.right) {
b := a.right
a = branch(branch(a.left, b.left), b.right)
}
return branch(a.left, branch(a.right, n.right))
}
if height(n.right)-height(n.left) > 1 {
b := n.right
if height(b.right) < height(b.left) {
a := b.left
b = branch(a.left, branch(a.right, b.right))
}
return branch(branch(n.left, b.left), b.right)
}
return n
}
// Join returns the text of a followed by that of b.
func Join(a, b *Node) *Node {
if a == nil {
return b
}
if b == nil {
return a
}
if a.left == nil && b.left == nil && a.sum.Bytes+b.sum.Bytes <= ChunkBytes {
return leaf(a.text + b.text)
}
if a.height > b.height+1 {
return balance(branch(a.left, Join(a.right, b)))
}
if b.height > a.height+1 {
return balance(branch(Join(a, b.left), b.right))
}
return branch(a, b)
}
// Build returns a tree of s, owning its bytes, so that a small surviving
// fragment cannot pin a large input string.
func Build(s string) *Node {
var leaves []*Node
for start := 0; start < len(s); {
end := start
for end < len(s) && end-start < ChunkBytes {
_, n := utf8.DecodeRuneInString(s[end:])
end += n
}
leaves = append(leaves, leaf(s[start:end]))
start = end
}
var build func(int, int) *Node
build = func(a, z int) *Node {
if a == z {
return nil
}
if z-a == 1 {
return leaves[a]
}
m := (a + z) / 2
return branch(build(a, m), build(m, z))
}
return build(0, len(leaves))
}
// Split returns the text before byte at and the text from it.
func Split(n *Node, at int) (*Node, *Node) {
if n == nil {
return nil, nil
}
if at <= 0 {
return nil, n
}
if at >= n.sum.Bytes {
return n, nil
}
if n.left == nil {
return leaf(n.text[:at]), leaf(n.text[at:])
}
if at < n.left.sum.Bytes {
a, b := Split(n.left, at)
return a, Join(b, n.right)
}
a, b := Split(n.right, at-n.left.sum.Bytes)
return Join(n.left, a), b
}
// ReplaceBytes returns the text with bytes a to b replaced by s.
func ReplaceBytes(n *Node, a, b int, s string) *Node {
total := Sum(n).Bytes
a, b = max(0, min(a, total)), max(0, min(b, total))
left, rest := Split(n, a)
_, right := Split(rest, b-a)
return Join(Join(left, Build(s)), right)
}
// Replace returns the text with runes start to end replaced by s.
func Replace(n *Node, start, end int, s string) *Node {
total := Sum(n).Runes
a, z := max(0, min(start, end, total)), max(0, min(max(start, end), total))
return ReplaceBytes(n, ByteOfRune(n, a), ByteOfRune(n, z), s)
}
// ByteOfRune returns the byte rune index starts at.
func ByteOfRune(n *Node, index int) int {
index = max(0, min(index, Sum(n).Runes))
offset := 0
for n != nil && n.left != nil {
if index < n.left.sum.Runes {
n = n.left
} else {
index -= n.left.sum.Runes
offset += n.left.sum.Bytes
n = n.right
}
}
if n == nil {
return offset
}
if n.sum.Bytes == n.sum.Runes {
return offset + index
}
for at := range n.text {
if index == 0 {
return offset + at
}
index--
}
return offset + len(n.text)
}
// ByteAfterRunes returns the byte count runes after byte from, which
// starts a rune, or the end of the text: it reads on from from rather
// than from the start of the chunk holding it, as ByteOfRune does, so a
// walk through the text in order reads each byte once.
func ByteAfterRunes(n *Node, from, count int) int {
end := Sum(n).Bytes
at := max(0, min(from, end))
if count <= 0 {
return at
}
Chunks(n, at, end, func(s string) bool {
for i := range s {
if count == 0 {
at += i
return false
}
count--
}
at += len(s)
return true
})
return at
}
// RuneOfUnit returns the rune holding UTF-16 unit index, or after it.
func RuneOfUnit(n *Node, index int) int {
offset := 0
for n != nil && n.left != nil {
if index < n.left.sum.Units {
n = n.left
} else {
index -= n.left.sum.Units
offset += n.left.sum.Runes
n = n.right
}
}
if n == nil {
return offset
}
if n.sum.Runes == n.sum.Units {
return offset + min(index, n.sum.Runes)
}
for _, r := range n.text {
count := utf16.RuneLen(r)
if index < count {
break
}
index -= count
offset++
}
return offset
}
// ByteOfUnit returns the byte of UTF-16 unit index, rounded down to a
// rune.
func ByteOfUnit(n *Node, index int) int {
off := 0
for n != nil && n.left != nil {
if index < n.left.sum.Units {
n = n.left
} else {
index -= n.left.sum.Units
off += n.left.sum.Bytes
n = n.right
}
}
if n == nil {
return off
}
for i, c := range n.text {
k := utf16.RuneLen(c)
if k < 0 {
k = 1
}
if index < k {
return off + i
}
index -= k
}
return off + len(n.text)
}
// Prefix returns the summary of the text before byte at.
func Prefix(n *Node, at int) Summary {
if n == nil || at <= 0 {
return Summary{}
}
if at >= n.sum.Bytes {
return n.sum
}
if n.left == nil {
return Summarize(n.text[:at])
}
if at <= n.left.sum.Bytes {
return Prefix(n.left, at)
}
return n.left.sum.add(Prefix(n.right, at-n.left.sum.Bytes))
}
// LineStart returns the byte line starts at, the end of the text past
// the last line.
func LineStart(n *Node, line int) int {
if line <= 0 {
return 0
}
if line > Sum(n).Lines {
return Sum(n).Bytes
}
offset := 0
for n.left != nil {
if line <= n.left.sum.Lines {
n = n.left
} else {
line -= n.left.sum.Lines
offset += n.left.sum.Bytes
n = n.right
}
}
from := 0
for range line {
from += strings.IndexByte(n.text[from:], '\n') + 1
}
return offset + from
}
// Read writes the bytes start to end to out.
func Read(n *Node, start, end int, out *strings.Builder) {
if n == nil || start >= end {
return
}
if n.left == nil {
out.WriteString(n.text[start:end])
return
}
split := n.left.sum.Bytes
if start < split {
Read(n.left, start, min(end, split), out)
}
if end > split {
Read(n.right, max(0, start-split), end-split, out)
}
}
// Chunks calls fn with the text from a to b, a piece at a time, until fn
// returns false.
func Chunks(n *Node, a, b int, fn func(s string) bool) {
a, b = max(0, a), min(b, Sum(n).Bytes)
var walk func(n *Node, off int) bool
walk = func(n *Node, off int) bool {
if n == nil || off >= b || off+n.sum.Bytes <= a {
return true
}
if n.left == nil {
lo, hi := max(a-off, 0), min(b-off, len(n.text))
return fn(n.text[lo:hi])
}
return walk(n.left, off) && walk(n.right, off+n.left.sum.Bytes)
}
walk(n, 0)
}
// WriteTo writes the text to w, a chunk at a time.
func WriteTo(n *Node, w io.Writer) (int64, error) {
var total int64
var err error
Chunks(n, 0, Sum(n).Bytes, func(s string) bool {
var k int
k, err = io.WriteString(w, s)
total += int64(k)
if err == nil && k != len(s) {
err = io.ErrShortWrite
}
return err == nil
})
return total, err
}
// Check reports what is wrong with the tree, for tests: an oversized or
// miscounted leaf, or an unbalanced or miscounted branch.
func Check(n *Node) error {
_, err := check(n)
return err
}
func check(n *Node) (Summary, error) {
if n == nil {
return Summary{}, nil
}
if n.left == nil {
if len(n.text) > ChunkBytes+3 || n.sum != Summarize(n.text) || n.height != 1 {
return Summary{}, fmt.Errorf("rope: invalid leaf %q", n.text)
}
return n.sum, nil
}
a, err := check(n.left)
if err != nil {
return a, err
}
b, err := check(n.right)
if err != nil {
return b, err
}
d := n.left.height - n.right.height
if d < -1 || d > 1 || n.height != 1+max(n.left.height, n.right.height) || n.sum != a.add(b) {
return Summary{}, fmt.Errorf("rope: invalid branch of height %d", n.height)
}
return n.sum, nil
}
+202
View File
@@ -0,0 +1,202 @@
package rope
import (
"math/rand/v2"
"strings"
"testing"
"unicode/utf16"
"unicode/utf8"
)
// sample is text of several chunks, with multi-byte runes, runes outside
// the BMP and empty lines.
func sample() string {
var b strings.Builder
for i := range 3000 {
switch i % 5 {
case 0:
b.WriteString("func main() {\n")
case 1:
b.WriteString("\tfmt.Println(\"héllo, 世界\")\n")
case 2:
b.WriteString("\n")
case 3:
b.WriteString("// 😀 emoji\n")
default:
b.WriteString("}\n")
}
}
b.WriteString("last")
return b.String()
}
func TestTextMatchesString(t *testing.T) {
r := rand.New(rand.NewPCG(1, 2))
s := sample()
text := New(s)
pieces := []string{"", "x", "\n", "é\n世", "😀", strings.Repeat("abc\n", 2000)}
for step := range 400 {
a := r.IntN(len(s) + 1)
b := a + r.IntN(min(len(s)-a, 5000)+1)
// Keep the edges on runes, as the editor does.
for a > 0 && !utf8.RuneStart(s[a]) {
a--
}
for b < len(s) && !utf8.RuneStart(s[b]) {
b++
}
in := pieces[r.IntN(len(pieces))]
old := text
text = text.Replace(a, b, in)
s = s[:a] + in + s[b:]
if old.Same(text) {
t.Fatalf("step %d: Replace returned the same tree", step)
}
if err := Check(text.Root()); err != nil {
t.Fatalf("step %d: %v", step, err)
}
if text.Len() != len(s) || text.String() != s {
t.Fatalf("step %d: text differs from the string", step)
}
if step%40 == 0 {
checkLines(t, text, s)
}
}
checkLines(t, text, s)
}
func checkLines(t *testing.T, text Text, s string) {
t.Helper()
lines := strings.Split(s, "\n")
if text.LineCount() != len(lines) {
t.Fatalf("LineCount = %d, want %d", text.LineCount(), len(lines))
}
off := 0
for i, l := range lines {
if text.LineStart(i) != off || text.LineEnd(i) != off+len(l) || text.Line(i) != l {
t.Fatalf("line %d: start %d end %d %q, want %d %d %q", i, text.LineStart(i), text.LineEnd(i), text.Line(i), off, off+len(l), l)
}
if text.LineOf(off) != i || text.LineOf(off+len(l)) != i {
t.Fatalf("LineOf(%d) = %d, want %d", off, text.LineOf(off), i)
}
off += len(l) + 1
}
}
func TestEachLine(t *testing.T) {
testEachLine(t, sample())
// Lines of up to 90 KB among them, for batches cut short by bytes.
lines := strings.Split(sample(), "\n")
for i := 0; i < len(lines); i += 37 {
lines[i] += strings.Repeat("x", i*30)
}
testEachLine(t, strings.Join(lines, "\n"))
}
func testEachLine(t *testing.T, s string) {
text := New(s)
lines := strings.Split(s, "\n")
starts := make([]int, len(lines))
for i := 1; i < len(lines); i++ {
starts[i] = starts[i-1] + len(lines[i-1]) + 1
}
for _, r := range [][2]int{{0, len(lines)}, {0, 1}, {255, 257}, {700, 1300}, {len(lines) - 3, len(lines) + 10}, {-5, 3}} {
next := max(0, r[0])
text.EachLine(r[0], r[1], func(i, start int, l string) bool {
if i != next || start != starts[i] || l != lines[i] {
t.Fatalf("EachLine(%d, %d): line %d at %d %q, want %d at %d %q", r[0], r[1], i, start, l, next, starts[next], lines[next])
}
next++
return true
})
if want := min(r[1], len(lines)); next != want {
t.Fatalf("EachLine(%d, %d) stopped at %d, want %d", r[0], r[1], next, want)
}
}
for _, r := range [][2]int{{len(lines) - 1, 0}, {0, 0}, {600, 300}, {len(lines) + 10, len(lines) - 4}, {3, -5}} {
next := min(r[0], len(lines)-1)
text.EachLineBack(r[0], r[1], func(i, start int, l string) bool {
if i != next || start != starts[i] || l != lines[i] {
t.Fatalf("EachLineBack(%d, %d): line %d at %d %q, want %d at %d %q", r[0], r[1], i, start, l, next, starts[next], lines[next])
}
next--
return true
})
if want := max(r[1], 0) - 1; next != want {
t.Fatalf("EachLineBack(%d, %d) stopped at %d, want %d", r[0], r[1], next, want)
}
}
// Returning false stops at once.
n := 0
text.EachLine(0, len(lines), func(i, _ int, _ string) bool { n++; return i < 300 })
if n != 301 {
t.Fatalf("EachLine called fn %d times after a stop, want 301", n)
}
n = 0
text.EachLineBack(len(lines)-1, 0, func(i, _ int, _ string) bool { n++; return false })
if n != 1 {
t.Fatalf("EachLineBack called fn %d times after a stop, want 1", n)
}
}
func TestUTF16(t *testing.T) {
s := sample()
text := New(s)
units := 0
for off, r := range s {
if got := text.UTF16(off); got != units {
t.Fatalf("UTF16(%d) = %d, want %d", off, got, units)
}
if got := text.FromUTF16(units); got != off {
t.Fatalf("FromUTF16(%d) = %d, want %d", units, got, off)
}
if got, n := text.RuneAt(off); got != r || n != utf8.RuneLen(r) {
t.Fatalf("RuneAt(%d) = %q %d, want %q", off, got, n, r)
}
units += len(utf16.Encode([]rune{r}))
}
if text.UTF16(len(s)) != units || text.FromUTF16(units) != len(s) {
t.Fatalf("UTF16 of the end = %d, want %d", text.UTF16(len(s)), units)
}
if r, n := text.RuneBefore(len(s)); r != 't' || n != 1 {
t.Fatalf("RuneBefore(end) = %q %d", r, n)
}
}
func TestChunks(t *testing.T) {
s := sample()
text := New(s)
for _, r := range [][2]int{{0, len(s)}, {10, 10}, {ChunkBytes - 3, ChunkBytes + 3}, {5000, 20000}} {
var b strings.Builder
text.Chunks(r[0], r[1], func(c string) bool { b.WriteString(c); return true })
if b.String() != s[r[0]:r[1]] || text.Slice(r[0], r[1]) != s[r[0]:r[1]] {
t.Fatalf("Chunks(%d, %d) differs from the string", r[0], r[1])
}
}
var w strings.Builder
if _, err := WriteTo(text.Root(), &w); err != nil || w.String() != s {
t.Fatalf("WriteTo differs from the string: %v", err)
}
}
func TestByteAfterRunes(t *testing.T) {
s := sample()
n := New(s).Root()
starts := []int{} // the byte each rune starts at, and the end
for i := range s {
starts = append(starts, i)
}
starts = append(starts, len(s))
r := rand.New(rand.NewPCG(5, 6))
for range 2000 {
from := r.IntN(len(starts))
count := r.IntN(3*ChunkBytes) - 2
want := starts[min(max(from+count, from), len(starts)-1)]
if got := ByteAfterRunes(n, starts[from], count); got != want {
t.Fatalf("ByteAfterRunes(%d, %d) = %d, want %d", starts[from], count, got, want)
}
}
if got := ByteAfterRunes(nil, 0, 3); got != 0 {
t.Fatalf("ByteAfterRunes of the empty text = %d", got)
}
}
+176
View File
@@ -0,0 +1,176 @@
package rope
import (
"strings"
"unicode/utf8"
)
// Text is a version of a text, read by bytes and lines, as code editing
// counts. Its zero value is the empty text.
type Text struct{ n *Node }
// New returns the text s.
func New(s string) Text { return Text{Build(s)} }
// TextOf returns the text of a tree.
func TextOf(n *Node) Text { return Text{n} }
// Root returns the tree of the text.
func (t Text) Root() *Node { return t.n }
// Same reports whether t and u are the same version, without comparing
// their bytes.
func (t Text) Same(u Text) bool { return t.n == u.n }
func (t Text) Len() int { return Sum(t.n).Bytes }
func (t Text) LineCount() int { return Sum(t.n).Lines + 1 }
// LineStart returns the byte line starts at, clamped.
func (t Text) LineStart(line int) int { return LineStart(t.n, line) }
// LineEnd returns the byte line ends at, before its newline.
func (t Text) LineEnd(line int) int {
if line+1 < t.LineCount() {
return LineStart(t.n, line+1) - 1
}
return t.Len()
}
// LineOf returns the line holding byte off.
func (t Text) LineOf(off int) int { return Prefix(t.n, off).Lines }
// Slice returns bytes a to b.
func (t Text) Slice(a, b int) string {
a, b = max(0, min(a, t.Len())), max(0, min(b, t.Len()))
if a >= b {
return ""
}
var out strings.Builder
out.Grow(b - a)
Read(t.n, a, b, &out)
return out.String()
}
// Line returns line i, without its newline.
func (t Text) Line(i int) string { return t.Slice(t.LineStart(i), t.LineEnd(i)) }
func (t Text) String() string { return t.Slice(0, t.Len()) }
// ByteAt returns the byte at off, or 0 past the end.
func (t Text) ByteAt(off int) byte {
n := t.n
if off < 0 || off >= Sum(n).Bytes {
return 0
}
for n.left != nil {
if off < n.left.sum.Bytes {
n = n.left
} else {
off -= n.left.sum.Bytes
n = n.right
}
}
return n.text[off]
}
// RuneAt returns the rune starting at off and its size, 0 and 0 at the
// end.
func (t Text) RuneAt(off int) (rune, int) {
if off < 0 || off >= t.Len() {
return 0, 0
}
if b := t.ByteAt(off); b < utf8.RuneSelf {
return rune(b), 1
}
return utf8.DecodeRuneInString(t.Slice(off, off+utf8.UTFMax))
}
// RuneBefore returns the rune ending at off and its size.
func (t Text) RuneBefore(off int) (rune, int) {
if off <= 0 {
return 0, 0
}
if b := t.ByteAt(off - 1); b < utf8.RuneSelf {
return rune(b), 1
}
return utf8.DecodeLastRuneInString(t.Slice(max(0, off-utf8.UTFMax), off))
}
// UTF16 returns how many UTF-16 units come before byte off.
func (t Text) UTF16(off int) int { return Prefix(t.n, off).Units }
// FromUTF16 returns the byte of UTF-16 unit u, rounded down to a rune.
func (t Text) FromUTF16(u int) int { return ByteOfUnit(t.n, u) }
// Chunks calls fn with bytes a to b, a piece at a time, until fn returns
// false.
func (t Text) Chunks(a, b int, fn func(s string) bool) { Chunks(t.n, a, b, fn) }
// Replace returns the text with bytes a to b replaced by s.
func (t Text) Replace(a, b int, s string) Text { return Text{ReplaceBytes(t.n, a, b, s)} }
const batchBytes = 64 << 10
// EachLine calls fn with lines from to to, exclusive, and the byte each
// starts at, reading them a batch at a time rather than finding each in
// the tree, until fn returns false. Batches of long lines are short, so
// that fn stopping early leaves little copied for nothing.
func (t Text) EachLine(from, to int, fn func(i, start int, s string) bool) {
to = min(to, t.LineCount())
for i := max(0, from); i < to; {
a := t.LineStart(i)
end := min(i+256, to, max(i+1, t.LineOf(a+batchBytes)))
text := t.Slice(a, t.LineEnd(end-1))
for i < end {
n := strings.IndexByte(text, '\n')
if n < 0 {
n = len(text)
}
if !fn(i, a, text[:n]) {
return
}
a += n + 1
if n < len(text) {
text = text[n+1:]
} else {
text = ""
}
i++
}
}
}
// EachLineBack calls fn with lines from down to to, inclusive, as
// EachLine does going up.
func (t Text) EachLineBack(from, to int, fn func(i, start int, s string) bool) {
from = min(from, t.LineCount()-1)
to = max(0, to)
for i := from; i >= to; {
first := max(i-255, to)
if b := t.LineEnd(i) - batchBytes; b > 0 {
first = max(first, min(i, t.LineOf(b)+1))
}
a := t.LineStart(first)
text := t.Slice(a, t.LineEnd(i))
// Where the batch's lines start, to go back through them.
starts := make([]int, 0, i-first+1)
for at := 0; ; {
starts = append(starts, at)
n := strings.IndexByte(text[at:], '\n')
if n < 0 {
break
}
at += n + 1
}
for k := len(starts) - 1; k >= 0 && i >= first; k-- {
end := len(text)
if k+1 < len(starts) {
end = starts[k+1] - 1
}
if !fn(i, a+starts[k], text[starts[k]:end]) {
return
}
i--
}
}
}