Skip to content

Commit 20c3523

Browse files
committed
Fix SourceEngine cache to refresh buffers after file edits
1 parent df6839d commit 20c3523

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

sway-types/src/source_engine.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,26 @@ impl SourceEngine {
6464
.contains("<autogenerated>")
6565
}
6666

67+
/// Fetch the cached source buffer for `source_id`, replacing it if the caller
68+
/// provides newer text so future span lookups observe the latest file contents.
6769
pub fn get_or_create_source_buffer(
6870
&self,
6971
source_id: &SourceId,
7072
source: span::Source,
7173
) -> span::Source {
7274
let mut map = self.source_to_buffer_map.write();
73-
map.entry(*source_id).or_insert(source).clone()
75+
76+
if let Some(existing) = map.get_mut(source_id) {
77+
// Replace the cached source if the contents have changed so that
78+
// subsequent spans reflect the latest file text.
79+
if existing.text != source.text {
80+
*existing = source;
81+
}
82+
existing.clone()
83+
} else {
84+
map.insert(*source_id, source.clone());
85+
source
86+
}
7487
}
7588

7689
/// This function retrieves an integer-based source ID for a provided path buffer.

0 commit comments

Comments
 (0)