|
| 1 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package cache |
| 6 | + |
| 7 | +import ( |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "golang.org/x/tools/gopls/internal/lsp/source" |
| 11 | + "golang.org/x/tools/gopls/internal/span" |
| 12 | + "golang.org/x/tools/internal/persistent" |
| 13 | +) |
| 14 | + |
| 15 | +// A fileMap maps files in the snapshot, with some additional bookkeeping: |
| 16 | +// It keeps track of overlays as well as directories containing any observed |
| 17 | +// file. |
| 18 | +type fileMap struct { |
| 19 | + files *persistent.Map[span.URI, source.FileHandle] |
| 20 | + overlays *persistent.Map[span.URI, *Overlay] // the subset of files that are overlays |
| 21 | + dirs *persistent.Set[string] // all dirs containing files; if nil, dirs have not been initialized |
| 22 | +} |
| 23 | + |
| 24 | +func newFileMap() *fileMap { |
| 25 | + return &fileMap{ |
| 26 | + files: new(persistent.Map[span.URI, source.FileHandle]), |
| 27 | + overlays: new(persistent.Map[span.URI, *Overlay]), |
| 28 | + dirs: new(persistent.Set[string]), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func (m *fileMap) Clone() *fileMap { |
| 33 | + m2 := &fileMap{ |
| 34 | + files: m.files.Clone(), |
| 35 | + overlays: m.overlays.Clone(), |
| 36 | + } |
| 37 | + if m.dirs != nil { |
| 38 | + m2.dirs = m.dirs.Clone() |
| 39 | + } |
| 40 | + return m2 |
| 41 | +} |
| 42 | + |
| 43 | +func (m *fileMap) Destroy() { |
| 44 | + m.files.Destroy() |
| 45 | + m.overlays.Destroy() |
| 46 | + if m.dirs != nil { |
| 47 | + m.dirs.Destroy() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Get returns the file handle mapped by the given key, or (nil, false) if the |
| 52 | +// key is not present. |
| 53 | +func (m *fileMap) Get(key span.URI) (source.FileHandle, bool) { |
| 54 | + return m.files.Get(key) |
| 55 | +} |
| 56 | + |
| 57 | +// Range calls f for each (uri, fh) in the map. |
| 58 | +func (m *fileMap) Range(f func(uri span.URI, fh source.FileHandle)) { |
| 59 | + m.files.Range(f) |
| 60 | +} |
| 61 | + |
| 62 | +// Set stores the given file handle for key, updating overlays and directories |
| 63 | +// accordingly. |
| 64 | +func (m *fileMap) Set(key span.URI, fh source.FileHandle) { |
| 65 | + m.files.Set(key, fh, nil) |
| 66 | + |
| 67 | + // update overlays |
| 68 | + if o, ok := fh.(*Overlay); ok { |
| 69 | + m.overlays.Set(key, o, nil) |
| 70 | + } else { |
| 71 | + // Setting a non-overlay must delete the corresponding overlay, to preserve |
| 72 | + // the accuracy of the overlay set. |
| 73 | + m.overlays.Delete(key) |
| 74 | + } |
| 75 | + |
| 76 | + // update dirs |
| 77 | + if m.dirs == nil { |
| 78 | + m.initDirs() |
| 79 | + } else { |
| 80 | + m.addDirs(key) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func (m *fileMap) initDirs() { |
| 85 | + m.dirs = new(persistent.Set[string]) |
| 86 | + m.files.Range(func(u span.URI, _ source.FileHandle) { |
| 87 | + m.addDirs(u) |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +// addDirs adds all directories containing u to the dirs set. |
| 92 | +func (m *fileMap) addDirs(u span.URI) { |
| 93 | + dir := filepath.Dir(u.Filename()) |
| 94 | + for dir != "" && !m.dirs.Contains(dir) { |
| 95 | + m.dirs.Add(dir) |
| 96 | + dir = filepath.Dir(dir) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Delete removes a file from the map, and updates overlays and dirs |
| 101 | +// accordingly. |
| 102 | +func (m *fileMap) Delete(key span.URI) { |
| 103 | + m.files.Delete(key) |
| 104 | + m.overlays.Delete(key) |
| 105 | + |
| 106 | + // Deleting a file may cause the set of dirs to shrink; therefore we must |
| 107 | + // re-evaluate the dir set. |
| 108 | + // |
| 109 | + // Do this lazily, to avoid work if there are multiple deletions in a row. |
| 110 | + if m.dirs != nil { |
| 111 | + m.dirs.Destroy() |
| 112 | + m.dirs = nil |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// Overlays returns a new unordered array of overlay files. |
| 117 | +func (m *fileMap) Overlays() []*Overlay { |
| 118 | + var overlays []*Overlay |
| 119 | + m.overlays.Range(func(_ span.URI, o *Overlay) { |
| 120 | + overlays = append(overlays, o) |
| 121 | + }) |
| 122 | + return overlays |
| 123 | +} |
| 124 | + |
| 125 | +// Dirs reports returns the set of dirs observed by the fileMap. |
| 126 | +// |
| 127 | +// This operation mutates the fileMap. |
| 128 | +// The result must not be mutated by the caller. |
| 129 | +func (m *fileMap) Dirs() *persistent.Set[string] { |
| 130 | + if m.dirs == nil { |
| 131 | + m.initDirs() |
| 132 | + } |
| 133 | + return m.dirs |
| 134 | +} |
0 commit comments