| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"iter" |
| 9 |
"path/filepath" |
| 10 |
|
| 11 |
"golang.org/x/tools/gopls/internal/file" |
| 12 |
"golang.org/x/tools/gopls/internal/protocol" |
| 13 |
"golang.org/x/tools/gopls/internal/util/persistent" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
type fileMap struct { |
| 20 |
files *persistent.Map[protocol.DocumentURI, file.Handle] |
| 21 |
overlays *persistent.Map[protocol.DocumentURI, *overlay] |
| 22 |
dirs *persistent.Set[string] |
| 23 |
} |
| 24 |
|
| 25 |
func newFileMap() *fileMap { |
| 26 |
return &fileMap{ |
| 27 |
files: new(persistent.Map[protocol.DocumentURI, file.Handle]), |
| 28 |
overlays: new(persistent.Map[protocol.DocumentURI, *overlay]), |
| 29 |
dirs: new(persistent.Set[string]), |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
func (m *fileMap) clone(changes map[protocol.DocumentURI]file.Handle) *fileMap { |
| 36 |
m2 := &fileMap{ |
| 37 |
files: m.files.Clone(), |
| 38 |
overlays: m.overlays.Clone(), |
| 39 |
} |
| 40 |
if m.dirs != nil { |
| 41 |
m2.dirs = m.dirs.Clone() |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
for uri, fh := range changes { |
| 55 |
if !fileExists(fh) { |
| 56 |
m2.delete(uri) |
| 57 |
} |
| 58 |
} |
| 59 |
for uri, fh := range changes { |
| 60 |
if fileExists(fh) { |
| 61 |
m2.set(uri, fh) |
| 62 |
} |
| 63 |
} |
| 64 |
return m2 |
| 65 |
} |
| 66 |
|
| 67 |
func (m *fileMap) destroy() { |
| 68 |
m.files.Destroy() |
| 69 |
m.overlays.Destroy() |
| 70 |
if m.dirs != nil { |
| 71 |
m.dirs.Destroy() |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func (m *fileMap) get(key protocol.DocumentURI) (file.Handle, bool) { |
| 78 |
return m.files.Get(key) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func (m *fileMap) all() iter.Seq2[protocol.DocumentURI, file.Handle] { |
| 83 |
return m.files.All() |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
func (m *fileMap) set(key protocol.DocumentURI, fh file.Handle) { |
| 89 |
m.files.Set(key, fh, nil) |
| 90 |
|
| 91 |
|
| 92 |
if o, ok := fh.(*overlay); ok { |
| 93 |
m.overlays.Set(key, o, nil) |
| 94 |
} else { |
| 95 |
|
| 96 |
|
| 97 |
m.overlays.Delete(key) |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
if m.dirs != nil { |
| 102 |
m.addDirs(key) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
func (m *fileMap) addDirs(u protocol.DocumentURI) { |
| 108 |
dir := u.DirPath() |
| 109 |
for dir != "" && !m.dirs.Contains(dir) { |
| 110 |
m.dirs.Add(dir) |
| 111 |
dir = filepath.Dir(dir) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func (m *fileMap) delete(key protocol.DocumentURI) { |
| 118 |
m.files.Delete(key) |
| 119 |
m.overlays.Delete(key) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
if m.dirs != nil { |
| 126 |
m.dirs.Destroy() |
| 127 |
m.dirs = nil |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (m *fileMap) getOverlays() []*overlay { |
| 133 |
var overlays []*overlay |
| 134 |
for _, o := range m.overlays.All() { |
| 135 |
overlays = append(overlays, o) |
| 136 |
} |
| 137 |
return overlays |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
func (m *fileMap) getDirs() *persistent.Set[string] { |
| 145 |
if m.dirs == nil { |
| 146 |
m.dirs = new(persistent.Set[string]) |
| 147 |
for uri := range m.files.All() { |
| 148 |
m.addDirs(uri) |
| 149 |
} |
| 150 |
} |
| 151 |
return m.dirs |
| 152 |
} |