gitstack

grindlemire/go-tui code browser

2.5 KB Go 117 lines 2026-06-12 · 0ad9a5a raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gopls

import (
	"testing"
)

func TestFindMappingForGoPosition(t *testing.T) {
	type tc struct {
		mappings []Mapping
		goLine   int
		goCol    int
		want     *Mapping
	}

	m1 := Mapping{TuiLine: 2, TuiCol: 8, GoLine: 10, GoCol: 5, Length: 6}
	m2 := Mapping{TuiLine: 4, TuiCol: 1, GoLine: 12, GoCol: 0, Length: 3}

	tests := map[string]tc{
		"exact start": {
			mappings: []Mapping{m1, m2},
			goLine:   10,
			goCol:    5,
			want:     &m1,
		},
		"inclusive exclusive end boundary": {
			mappings: []Mapping{m1, m2},
			goLine:   10,
			goCol:    11, // GoCol + Length
			want:     &m1,
		},
		"second mapping": {
			mappings: []Mapping{m1, m2},
			goLine:   12,
			goCol:    2,
			want:     &m2,
		},
		"wrong line": {
			mappings: []Mapping{m1, m2},
			goLine:   11,
			goCol:    5,
			want:     nil,
		},
		"before mapping start": {
			mappings: []Mapping{m1},
			goLine:   10,
			goCol:    4,
			want:     nil,
		},
		"past exclusive end": {
			mappings: []Mapping{m1},
			goLine:   10,
			goCol:    12,
			want:     nil,
		},
		"empty source map": {
			mappings: nil,
			goLine:   10,
			goCol:    5,
			want:     nil,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			sm := NewSourceMap()
			for _, m := range tt.mappings {
				sm.AddMapping(m)
			}

			got := sm.FindMappingForGoPosition(tt.goLine, tt.goCol)
			if tt.want == nil {
				if got != nil {
					t.Errorf("got %+v, want nil", got)
				}
				return
			}
			if got == nil {
				t.Fatalf("got nil, want %+v", tt.want)
			}
			if *got != *tt.want {
				t.Errorf("got %+v, want %+v", *got, *tt.want)
			}
		})
	}
}

func TestVirtualFileCacheAll(t *testing.T) {
	c := NewVirtualFileCache()

	if got := c.All(); len(got) != 0 {
		t.Errorf("All() on empty cache = %v, want empty", got)
	}

	smA := NewSourceMap()
	c.Put("file:///a.gsx", "file:///a_gsx_generated.go", "package a", smA, 1)
	c.Put("file:///b.gsx", "file:///b_gsx_generated.go", "package b", NewSourceMap(), 2)

	all := c.All()
	if len(all) != 2 {
		t.Fatalf("All() returned %d files, want 2", len(all))
	}

	byTui := make(map[string]*CachedVirtualFile, len(all))
	for _, f := range all {
		byTui[f.TuiURI] = f
	}
	a := byTui["file:///a.gsx"]
	if a == nil {
		t.Fatal("All() missing file:///a.gsx")
	}
	if a.GoURI != "file:///a_gsx_generated.go" || a.Content != "package a" || a.Version != 1 || a.SourceMap != smA {
		t.Errorf("cached file a = %+v, want original fields preserved", a)
	}
	if byTui["file:///b.gsx"] == nil {
		t.Error("All() missing file:///b.gsx")
	}
}