Skip to content

Commit

Permalink
lsp: Return copies of cache maps
Browse files Browse the repository at this point in the history
Addressing: #750

Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 committed May 28, 2024
1 parent 372185b commit 3724aa4
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions internal/lsp/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ func (c *Cache) GetAllFiles() map[string]string {
c.fileContentsMu.Lock()
defer c.fileContentsMu.Unlock()

return c.fileContents
cp := make(map[string]string, len(c.fileContents))
for k, v := range c.fileContents {
cp[k] = v
}

return cp
}

func (c *Cache) GetFileContents(uri string) (string, bool) {
Expand All @@ -107,7 +112,12 @@ func (c *Cache) GetAllModules() map[string]*ast.Module {
c.moduleMu.Lock()
defer c.moduleMu.Unlock()

return c.modules
cp := make(map[string]*ast.Module, len(c.modules))
for k, v := range c.modules {
cp[k] = v
}

return cp
}

func (c *Cache) GetModule(uri string) (*ast.Module, bool) {
Expand Down Expand Up @@ -208,7 +218,12 @@ func (c *Cache) GetAllBuiltInPositions() map[string]map[uint][]types.BuiltinPosi
c.builtinPositionsMu.Lock()
defer c.builtinPositionsMu.Unlock()

return c.builtinPositionsFile
cp := make(map[string]map[uint][]types.BuiltinPosition, len(c.builtinPositionsFile))
for k, v := range c.builtinPositionsFile {
cp[k] = v
}

return cp
}

func (c *Cache) SetFileRefs(uri string, items map[string]types.Ref) {
Expand All @@ -229,7 +244,18 @@ func (c *Cache) GetAllFileRefs() map[string]map[string]types.Ref {
c.fileRefMu.Lock()
defer c.fileRefMu.Unlock()

return c.fileRefs
cp := make(map[string]map[string]types.Ref, len(c.fileRefs))

for k, v := range c.fileRefs {
innerCopy := make(map[string]types.Ref, len(v))
for innerK, innerV := range v {
innerCopy[innerK] = innerV
}

cp[k] = innerCopy
}

return cp
}

// Delete removes all cached data for a given URI.
Expand Down

0 comments on commit 3724aa4

Please # to comment.