| 🛟 Updated. 28d5985 k33g 21h ago | 1 | package buffer_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 📦 Turbo Core f3ade8d k33g 13h ago | 6 | "rickub.com/turbo-editors/turbo-core/buffer" |
| 🛟 Updated. 28d5985 k33g 21h ago | 7 | ) |
| 8 | |
| 9 | // Typing into a buffer and reading the result back. |
| 10 | func ExampleBuffer_Insert() { |
| 11 | b := buffer.New() |
| 12 | b.Insert("package main") |
| 13 | b.InsertNewlineAndIndent() |
| 14 | b.Insert("\nfunc main() {}") |
| 15 | |
| 16 | fmt.Println(b.Text()) |
| 17 | // Output: |
| 18 | // package main |
| 19 | // |
| 20 | // func main() {} |
| 21 | } |
| 22 | |
| 23 | // A run of typed characters is one undo step, not one per keystroke. |
| 24 | func ExampleBuffer_Undo() { |
| 25 | b := buffer.NewFromString("var ") |
| 26 | b.MoveBufferEnd() |
| 27 | for _, r := range "count" { |
| 28 | b.InsertRune(r) |
| 29 | } |
| 30 | fmt.Println(b.Text()) |
| 31 | |
| 32 | b.Undo() |
| 33 | fmt.Println(b.Text()) |
| 34 | // Output: |
| 35 | // var count |
| 36 | // var |
| 37 | } |
| 38 | |
| 39 | // Selecting a span and replacing it in one go. |
| 40 | func ExampleBuffer_SelectedText() { |
| 41 | b := buffer.NewFromString("hello world") |
| 42 | b.SetCursor(buffer.Position{Line: 0, Col: 6}) |
| 43 | b.StartSelection() |
| 44 | b.Extend(b.MoveLineEnd) |
| 45 | |
| 46 | fmt.Println(b.SelectedText()) |
| 47 | |
| 48 | b.Insert("gophers") |
| 49 | fmt.Println(b.Text()) |
| 50 | // Output: |
| 51 | // world |
| 52 | // hello gophers |
| 53 | } |
| 54 | |
| 55 | // Tabs occupy several screen columns, so rune columns and screen columns are |
| 56 | // not the same thing. |
| 57 | func ExampleBuffer_DisplayColumn() { |
| 58 | b := buffer.NewFromString("\tif err != nil {") |
| 59 | b.SetTabWidth(4) |
| 60 | |
| 61 | fmt.Println(b.DisplayColumn(0, 1)) // just after the tab |
| 62 | fmt.Println(b.RuneColumn(0, 4)) // back from the screen column |
| 63 | // Output: |
| 64 | // 4 |
| 65 | // 1 |
| 66 | } |