Get retrieves a copy of a document to avoid race conditions The returned document is a snapshot and modifications won't affect the original
(uri string)
| 247 | // Get retrieves a copy of a document to avoid race conditions |
| 248 | // The returned document is a snapshot and modifications won't affect the original |
| 249 | func (dm *DocumentManager) Get(uri string) (*Document, bool) { |
| 250 | dm.mu.RLock() |
| 251 | defer dm.mu.RUnlock() |
| 252 | doc, ok := dm.documents[uri] |
| 253 | if !ok { |
| 254 | return nil, false |
| 255 | } |
| 256 | // Return a copy to prevent race conditions when accessing fields after lock release |
| 257 | docCopy := &Document{ |
| 258 | URI: doc.URI, |
| 259 | LanguageID: doc.LanguageID, |
| 260 | Version: doc.Version, |
| 261 | Content: doc.Content, |
| 262 | Lines: make([]string, len(doc.Lines)), |
| 263 | } |
| 264 | copy(docCopy.Lines, doc.Lines) |
| 265 | return docCopy, true |
| 266 | } |
| 267 | |
| 268 | // GetContent retrieves a document's content |
| 269 | func (dm *DocumentManager) GetContent(uri string) (string, bool) { |