1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::collections::HashMap;
use sem_core::model::entity::SemanticEntity;
use crate::conflict::MarkerFormat;
use crate::merge::ResolvedEntity;
use crate::region::FileRegion;
/// Reconstruct a merged file from resolved entities and merged interstitials.
///
/// Uses "ours" region ordering as the skeleton. Inserts theirs-only additions
/// at their relative position (after the entity that precedes them in theirs).
pub fn reconstruct(
ours_regions: &[FileRegion],
theirs_regions: &[FileRegion],
theirs_entities: &[SemanticEntity],
ours_entity_map: &HashMap<&str, &SemanticEntity>,
resolved_entities: &HashMap<String, ResolvedEntity>,
merged_interstitials: &HashMap<String, String>,
marker_format: &MarkerFormat,
theirs_rename_base_ids: &std::collections::HashSet<String>,
) -> String {
let mut output = String::new();
// Track which entity IDs we've emitted (from ours skeleton)
let mut emitted_entities: std::collections::HashSet<String> = std::collections::HashSet::new();
// Identify theirs-only entities (not in ours, and not renamed versions of ours entities)
let theirs_only: Vec<&SemanticEntity> = theirs_entities
.iter()
.filter(|e| {
!ours_entity_map.contains_key(e.id.as_str())
&& !theirs_rename_base_ids.contains(&e.id)
})
.collect();
// Build a map of theirs-only entities by what precedes them in theirs ordering
let mut theirs_insertions: HashMap<Option<String>, Vec<&SemanticEntity>> = HashMap::new();
for entity in &theirs_only {
let predecessor = find_predecessor_in_regions(theirs_regions, &entity.id);
theirs_insertions
.entry(predecessor)
.or_default()
.push(entity);
}
// Walk ours regions as skeleton
for region in ours_regions {
match region {
FileRegion::Interstitial(interstitial) => {
// Use merged interstitial if available, otherwise ours
if let Some(merged) = merged_interstitials.get(&interstitial.position_key) {
output.push_str(merged);
} else {
output.push_str(&interstitial.content);
}
}
FileRegion::Entity(entity_region) => {
// Before emitting ours entity, check if there are theirs-only insertions
// that should go before this entity (predecessor is the entity before this one)
// Emit the resolved entity
if let Some(resolved) = resolved_entities.get(&entity_region.entity_id) {
match resolved {
ResolvedEntity::Clean(region) => {
output.push_str(®ion.content);
if !region.content.is_empty() && !region.content.ends_with('\n') {
output.push('\n');
}
}
ResolvedEntity::Conflict(conflict) => {
output.push_str(&conflict.to_conflict_markers(marker_format));
}
ResolvedEntity::ScopedConflict { content, .. } => {
output.push_str(content);
if !content.is_empty() && !content.ends_with('\n') {
output.push('\n');
}
}
ResolvedEntity::Deleted => {
// Skip deleted entities
}
}
} else {
// Entity not in resolved map — keep ours content
output.push_str(&entity_region.content);
if !entity_region.content.is_empty()
&& !entity_region.content.ends_with('\n')
{
output.push('\n');
}
}
emitted_entities.insert(entity_region.entity_id.clone());
// Insert theirs-only entities that should come after this entity.
// Chase the chain: if we insert X after this entity, also insert
// anything whose predecessor is X, then anything after that, etc.
// This handles multiple sequential additions (e.g. adding 3 keys
// at the end of a JSON file).
let mut current_pred = Some(entity_region.entity_id.clone());
while let Some(ref pred) = current_pred {
if let Some(insertions) = theirs_insertions.get(&Some(pred.clone())) {
let mut next_pred: Option<String> = None;
for theirs_entity in insertions {
if emitted_entities.contains(&theirs_entity.id) {
continue;
}
if let Some(resolved) = resolved_entities.get(&theirs_entity.id) {
match resolved {
ResolvedEntity::Clean(region) => {
// Only add blank-line separator for multi-line entities
// (functions, methods). Single-line entities (JSON props,
// struct fields) don't need one.
if region.content.trim_end().contains('\n') {
output.push('\n');
}
output.push_str(®ion.content);
if !region.content.is_empty()
&& !region.content.ends_with('\n')
{
output.push('\n');
}
}
ResolvedEntity::Conflict(conflict) => {
output.push('\n');
output.push_str(&conflict.to_conflict_markers(marker_format));
}
ResolvedEntity::ScopedConflict { content, .. } => {
output.push('\n');
output.push_str(content);
if !content.is_empty() && !content.ends_with('\n') {
output.push('\n');
}
}
ResolvedEntity::Deleted => {}
}
}
emitted_entities.insert(theirs_entity.id.clone());
next_pred = Some(theirs_entity.id.clone());
}
current_pred = next_pred;
} else {
break;
}
}
}
}
}
// Emit any theirs-only entities whose predecessor was None (should go at the start)
// or whose predecessor wasn't found — append at the end
if let Some(insertions) = theirs_insertions.get(&None) {
for theirs_entity in insertions {
if !emitted_entities.contains(&theirs_entity.id) {
if let Some(resolved) = resolved_entities.get(&theirs_entity.id) {
emit_resolved(&mut output, resolved, marker_format);
}
emitted_entities.insert(theirs_entity.id.clone());
}
}
}
// Any remaining theirs-only entities not yet emitted (predecessor entity was deleted, etc.)
for (pred, insertions) in &theirs_insertions {
if pred.is_none() {
continue; // Already handled above
}
for theirs_entity in insertions {
if !emitted_entities.contains(&theirs_entity.id) {
if let Some(resolved) = resolved_entities.get(&theirs_entity.id) {
emit_resolved(&mut output, resolved, marker_format);
}
emitted_entities.insert(theirs_entity.id.clone());
}
}
}
output
}
/// Emit a resolved entity into the output (for theirs-only insertions).
fn emit_resolved(output: &mut String, resolved: &ResolvedEntity, marker_format: &MarkerFormat) {
match resolved {
ResolvedEntity::Clean(region) => {
if !output.is_empty() && !output.ends_with('\n') {
output.push('\n');
}
output.push('\n');
output.push_str(®ion.content);
if !region.content.is_empty() && !region.content.ends_with('\n') {
output.push('\n');
}
}
ResolvedEntity::Conflict(conflict) => {
if !output.is_empty() && !output.ends_with('\n') {
output.push('\n');
}
output.push('\n');
output.push_str(&conflict.to_conflict_markers(marker_format));
}
ResolvedEntity::ScopedConflict { content, .. } => {
if !output.is_empty() && !output.ends_with('\n') {
output.push('\n');
}
output.push('\n');
output.push_str(content);
if !content.is_empty() && !content.ends_with('\n') {
output.push('\n');
}
}
ResolvedEntity::Deleted => {}
}
}
/// Find the entity ID that precedes the given entity in a region list.
fn find_predecessor_in_regions(regions: &[FileRegion], entity_id: &str) -> Option<String> {
let mut last_entity_id: Option<String> = None;
for region in regions {
if let FileRegion::Entity(e) = region {
if e.entity_id == entity_id {
return last_entity_id;
}
last_entity_id = Some(e.entity_id.clone());
}
}
None
}