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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use ratatui::{
prelude::*,
style::{Modifier, Style},
text::{Line, Span, Text},
widgets::{Block, BorderType, Borders, Paragraph},
Frame,
};
use std::collections::{HashMap, HashSet};
use super::render::render_file_list;
use super::types::*;
pub fn prepare_rebase_changes(app: &mut App) {
app.rebase_changes.clear();
for file_name in &app.file_names {
if let Some((base_lines, head_lines)) = app.file_changes.get(file_name) {
let mut changes = Vec::new();
// Build a lookup map for O(1) context line access
let base_line_map: HashMap<usize, &String> =
base_lines.iter().map(|(n, l)| (*n, l)).collect();
let head_line_map: HashMap<usize, &String> =
head_lines.iter().map(|(n, l)| (*n, l)).collect();
let get_context =
|line_map: &HashMap<usize, &String>, line_num: usize| -> Vec<String> {
let mut context = Vec::new();
let start = if line_num > 3 { line_num - 3 } else { 1 };
for i in start..line_num {
if let Some(line) = line_map.get(&i) {
context.push(format!("{}: {}", i, line));
}
}
for i in line_num + 1..=line_num + 3 {
if let Some(line) = line_map.get(&i) {
context.push(format!("{}: {}", i, line));
}
}
context
};
// First, find corresponding deleted/added lines to pair them.
// Walk base and head in parallel: within each change block
// (consecutive deletions in base / additions in head between
// context lines) pair them sequentially — first deletion with
// first addition, second with second, etc.
let mut paired_changes: HashMap<usize, usize> = HashMap::new();
let mut used_head_nums: HashSet<usize> = HashSet::new();
// Map line numbers to their content for easier lookup later
let mut head_map = HashMap::new();
for (line_num, line) in head_lines {
if line.starts_with('+') {
head_map.insert(*line_num, line.clone());
}
}
{
let mut bi = 0;
let mut hi = 0;
loop {
// Collect a run of deletions from base
let mut del_run: Vec<usize> = Vec::new();
while bi < base_lines.len() && base_lines[bi].1.starts_with('-') {
del_run.push(base_lines[bi].0);
bi += 1;
}
// Collect a run of additions from head
let mut add_run: Vec<usize> = Vec::new();
while hi < head_lines.len() && head_lines[hi].1.starts_with('+') {
add_run.push(head_lines[hi].0);
hi += 1;
}
// Pair them sequentially
let pairs = del_run.len().min(add_run.len());
for i in 0..pairs {
paired_changes.insert(del_run[i], add_run[i]);
used_head_nums.insert(add_run[i]);
}
// Remaining additions beyond the paired count stay out of
// used_head_nums, so they become standalone insert changes below.
// Skip past the next context line on both sides
let base_done = bi >= base_lines.len();
let head_done = hi >= head_lines.len();
if base_done && head_done {
break;
}
if !base_done {
bi += 1;
}
if !head_done {
hi += 1;
}
}
}
// Build head→base insertion position mapping by aligning
// context lines between the two sides of the diff.
let mut base_insert_positions: HashMap<usize, usize> = HashMap::new();
{
let mut bi = 0;
let mut last_base_pos = 0usize;
for (h_num, h_line) in head_lines {
if h_line.starts_with('+') {
// Addition: insert after the last aligned base position
base_insert_positions.insert(*h_num, last_base_pos + 1);
} else {
// Context line: skip past any '-' lines in base
while bi < base_lines.len() && base_lines[bi].1.starts_with('-') {
bi += 1;
}
if bi < base_lines.len() {
last_base_pos = base_lines[bi].0;
bi += 1;
}
}
}
}
// Add removed lines from base with their paired added lines
for (line_num, line) in base_lines {
if line.starts_with('-') {
let context = get_context(&base_line_map, *line_num);
// Check if this line has a paired addition
let paired_head_num = paired_changes.get(line_num);
let paired_content = paired_head_num
.and_then(|head_num| head_map.get(head_num))
.cloned();
changes.push(Change {
line_num: *line_num,
content: line.clone(),
paired_content,
state: ChangeState::Unselected,
is_base: true,
context,
base_insert_pos: None,
});
}
}
// Add added lines from head that weren't paired
for (line_num, line) in head_lines {
if line.starts_with('+') && !used_head_nums.contains(line_num) {
let context = get_context(&head_line_map, *line_num);
let base_pos = base_insert_positions.get(line_num).copied();
changes.push(Change {
line_num: *line_num,
content: line.clone(),
paired_content: None,
state: ChangeState::Unselected,
is_base: false,
context,
base_insert_pos: base_pos,
});
}
}
// Sort by position in the base file so changes appear in
// file order. For base-side changes, use their base line number
// directly. For unpaired additions, use the computed base
// insertion position so they sort alongside nearby base changes.
changes.sort_by_key(|change| {
if change.is_base {
change.line_num
} else {
change.base_insert_pos.unwrap_or(change.line_num)
}
});
app.rebase_changes.insert(file_name.clone(), changes);
}
}
app.current_change_idx = 0;
}
pub fn render_rebase_ui(f: &mut Frame, app: &App, area: Rect) {
let t = &app.theme;
let content_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(20), Constraint::Percentage(80)])
.split(area);
render_file_list(f, app, content_chunks[0]);
if let Some(current_file) = app.file_names.get(app.current_file_idx) {
let rebase_block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(t.border_focused))
.title(Span::styled(
format!(" Rebase: {} ", current_file),
Style::default().fg(t.accent).add_modifier(Modifier::BOLD),
));
f.render_widget(&rebase_block, content_chunks[1]);
let inner_area = rebase_block.inner(content_chunks[1]);
if let Some(changes) = app.rebase_changes.get(current_file) {
if changes.is_empty() {
let msg = Paragraph::new(Span::styled(
"No changes to rebase in this file",
Style::default().fg(t.fg_dim),
))
.alignment(Alignment::Center);
f.render_widget(msg, inner_area);
return;
}
// Count states for progress
let accepted = changes
.iter()
.filter(|c| c.state == ChangeState::Accepted)
.count();
let rejected = changes
.iter()
.filter(|c| c.state == ChangeState::Rejected)
.count();
let remaining = changes.len() - accepted - rejected;
let rebase_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(2), // Progress
Constraint::Percentage(50), // Current change
Constraint::Min(0), // Context
])
.split(inner_area);
// Progress indicator
let progress_spans = vec![
Span::styled(" ", Style::default()),
Span::styled(
format!("Change {}/{}", app.current_change_idx + 1, changes.len()),
Style::default()
.fg(t.fg_bright)
.add_modifier(Modifier::BOLD),
),
Span::styled(" \u{2502} ", Style::default().fg(t.border_dim)),
Span::styled(
format!("{} accepted", accepted),
Style::default().fg(t.fg_added),
),
Span::styled(" ", Style::default()),
Span::styled(
format!("{} rejected", rejected),
Style::default().fg(t.fg_removed),
),
Span::styled(" ", Style::default()),
Span::styled(
format!("{} remaining", remaining),
Style::default().fg(t.fg_dim),
),
];
let progress = Paragraph::new(Line::from(progress_spans));
f.render_widget(progress, rebase_chunks[0]);
// Current change
if app.current_change_idx < changes.len() {
let current_change = &changes[app.current_change_idx];
let change_type = if current_change.is_base {
"Removed"
} else {
"Added"
};
let (state_symbol, state_color) = match current_change.state {
ChangeState::Unselected => ("\u{25cb}", t.fg_dim),
ChangeState::Accepted => ("\u{25cf}", t.fg_added),
ChangeState::Rejected => ("\u{25cf}", t.fg_removed),
};
let line_content = current_change
.content
.strip_prefix('+')
.or_else(|| current_change.content.strip_prefix('-'))
.unwrap_or(¤t_change.content);
let type_color = if current_change.is_base {
t.fg_removed
} else {
t.fg_added
};
let mut content_text = vec![
Line::from(vec![
Span::styled(
format!(" {} ", state_symbol),
Style::default().fg(state_color),
),
Span::styled(
format!("{} ", change_type),
Style::default().fg(type_color).add_modifier(Modifier::BOLD),
),
Span::styled(
format!("(line {})", current_change.line_num),
Style::default().fg(t.fg_dim),
),
]),
Line::from(""),
Line::from(Span::styled(
format!(" {}", line_content),
Style::default().fg(type_color),
)),
];
if let Some(paired) = ¤t_change.paired_content {
let paired_text = paired
.strip_prefix('+')
.or_else(|| paired.strip_prefix('-'))
.unwrap_or(paired);
content_text.push(Line::from(""));
content_text.push(Line::from(vec![
Span::styled(" \u{2192} ", Style::default().fg(t.fg_dim)),
Span::styled(
paired_text.to_owned(),
Style::default().fg(t.fg_added).add_modifier(Modifier::BOLD),
),
]));
if current_change.is_base {
content_text.push(Line::from(""));
content_text.push(Line::from(vec![
Span::styled(" ", Style::default()),
Span::styled("a", Style::default().fg(t.fg_key)),
Span::styled(" accept incoming ", Style::default().fg(t.fg_dim)),
Span::styled("x", Style::default().fg(t.fg_key)),
Span::styled(" keep current", Style::default().fg(t.fg_dim)),
]));
}
}
let change_block_widget = Block::default()
.title(Span::styled(
" Current Change ",
Style::default().fg(t.fg_key),
))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(t.border_focused));
let mut change_paragraph =
Paragraph::new(Text::from(content_text)).block(change_block_widget);
match current_change.state {
ChangeState::Accepted => {
change_paragraph =
change_paragraph.style(Style::default().bg(t.bg_accepted));
}
ChangeState::Rejected => {
change_paragraph =
change_paragraph.style(Style::default().bg(t.bg_rejected));
}
ChangeState::Unselected => {}
}
f.render_widget(change_paragraph, rebase_chunks[1]);
// Context section
let mut context_lines = vec![Line::from("")];
for line in ¤t_change.context {
context_lines.push(Line::from(Span::styled(
format!(" {}", line),
Style::default().fg(t.fg_dim),
)));
}
let context_block = Paragraph::new(Text::from(context_lines)).block(
Block::default()
.title(Span::styled(" Context ", Style::default().fg(t.fg_dim)))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(t.border_dim)),
);
f.render_widget(context_block, rebase_chunks[2]);
}
} else {
let msg = Paragraph::new(Span::styled(
"No changes found for this file",
Style::default().fg(t.fg_dim),
))
.alignment(Alignment::Center);
f.render_widget(msg, inner_area);
}
}
}