Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type snapshot struct {
project *Project
positionEncoding lsproto.PositionEncodingKind
program *compiler.Program
lineMaps collections.SyncMap[*ast.SourceFile, *ls.LineMap]
}

Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The lineMaps cache is unbounded and may retain entries even after files are closed or snapshots are discarded. Consider implementing eviction logic or clearing entries when files are removed to prevent potential memory growth.

Suggested change
// RemoveLineMap removes the line map for a given source file from the cache.
func (s *snapshot) RemoveLineMap(file *ast.SourceFile) {
s.lineMaps.Delete(file)
}

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, these snapshots are request-scoped

// GetLineMap implements ls.Host.
Expand All @@ -51,7 +52,13 @@ func (s *snapshot) GetLineMap(fileName string) *ls.LineMap {
if s.project.getFileVersion(file) == scriptInfo.Version() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't really understand why we're doing this at all; it seems to me that we should not cache line maps on script infos at all, instead do it per-source-file, and then just always use that. But I seem to remember someone saying that was wrong for some reason, so, this hack seems better than nothing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall a conversation around line maps differing between LS and CLI

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a different thing but yeah they do.

return scriptInfo.LineMap()
}
return ls.ComputeLineStarts(file.Text())
// The version changed; recompute the line map.
// !!! This shouldn't happen so often, but does. Probably removable once snapshotting is finished.
if cached, ok := s.lineMaps.Load(file); ok {
return cached
}
lineMap, _ := s.lineMaps.LoadOrStore(file, ls.ComputeLineStarts(file.Text()))
return lineMap
}

// GetPositionEncoding implements ls.Host.
Expand Down