Skip to content

Commit 85431cc

Browse files
committed
Reimplement colorized data relocation hover diffs
1 parent 7b00a9e commit 85431cc

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

objdiff-core/src/diff/display.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,13 @@ pub enum SymbolNavigationKind {
325325
Extab,
326326
}
327327

328+
#[derive(Debug, Clone, Eq, PartialEq)]
328329
pub enum HoverItemColor {
329330
Normal, // Gray
330331
Emphasized, // White
331332
Special, // Blue
333+
Delete, // Red
334+
Insert, // Green
332335
}
333336

334337
pub enum HoverItem {
@@ -355,7 +358,12 @@ pub fn symbol_context(obj: &Object, symbol_index: usize) -> Vec<ContextItem> {
355358
out
356359
}
357360

358-
pub fn symbol_hover(obj: &Object, symbol_index: usize, addend: i64) -> Vec<HoverItem> {
361+
pub fn symbol_hover(
362+
obj: &Object,
363+
symbol_index: usize,
364+
addend: i64,
365+
color: HoverItemColor,
366+
) -> Vec<HoverItem> {
359367
let symbol = &obj.symbols[symbol_index];
360368
let addend_str = match addend.cmp(&0i64) {
361369
Ordering::Greater => format!("+{:x}", addend),
@@ -366,44 +374,44 @@ pub fn symbol_hover(obj: &Object, symbol_index: usize, addend: i64) -> Vec<Hover
366374
out.push(HoverItem::Text {
367375
label: "Name".into(),
368376
value: format!("{}{}", symbol.name, addend_str),
369-
color: HoverItemColor::Normal,
377+
color: color.clone(),
370378
});
371379
if let Some(demangled_name) = &symbol.demangled_name {
372380
out.push(HoverItem::Text {
373381
label: "Demangled".into(),
374382
value: demangled_name.into(),
375-
color: HoverItemColor::Normal,
383+
color: color.clone(),
376384
});
377385
}
378386
if let Some(section) = symbol.section {
379387
out.push(HoverItem::Text {
380388
label: "Section".into(),
381389
value: obj.sections[section].name.clone(),
382-
color: HoverItemColor::Normal,
390+
color: color.clone(),
383391
});
384392
out.push(HoverItem::Text {
385393
label: "Address".into(),
386394
value: format!("{:x}{}", symbol.address, addend_str),
387-
color: HoverItemColor::Normal,
395+
color: color.clone(),
388396
});
389397
if symbol.flags.contains(SymbolFlag::SizeInferred) {
390398
out.push(HoverItem::Text {
391399
label: "Size".into(),
392400
value: format!("{:x} (inferred)", symbol.size),
393-
color: HoverItemColor::Normal,
401+
color: color.clone(),
394402
});
395403
} else {
396404
out.push(HoverItem::Text {
397405
label: "Size".into(),
398406
value: format!("{:x}", symbol.size),
399-
color: HoverItemColor::Normal,
407+
color: color.clone(),
400408
});
401409
}
402410
if let Some(align) = symbol.align {
403411
out.push(HoverItem::Text {
404412
label: "Alignment".into(),
405413
value: align.get().to_string(),
406-
color: HoverItemColor::Normal,
414+
color: color.clone(),
407415
});
408416
}
409417
if let Some(address) = symbol.virtual_address {
@@ -443,22 +451,31 @@ pub fn relocation_context(
443451
out
444452
}
445453

446-
pub fn relocation_hover(obj: &Object, reloc: ResolvedRelocation) -> Vec<HoverItem> {
454+
pub fn relocation_hover(
455+
obj: &Object,
456+
reloc: ResolvedRelocation,
457+
color: HoverItemColor,
458+
) -> Vec<HoverItem> {
447459
let mut out = Vec::new();
448460
if let Some(name) = obj.arch.reloc_name(reloc.relocation.flags) {
449461
out.push(HoverItem::Text {
450462
label: "Relocation".into(),
451463
value: name.to_string(),
452-
color: HoverItemColor::Normal,
464+
color: color.clone(),
453465
});
454466
} else {
455467
out.push(HoverItem::Text {
456468
label: "Relocation".into(),
457469
value: format!("<{:?}>", reloc.relocation.flags),
458-
color: HoverItemColor::Normal,
470+
color: color.clone(),
459471
});
460472
}
461-
out.append(&mut symbol_hover(obj, reloc.relocation.target_symbol, reloc.relocation.addend));
473+
out.append(&mut symbol_hover(
474+
obj,
475+
reloc.relocation.target_symbol,
476+
reloc.relocation.addend,
477+
color.clone(),
478+
));
462479
out
463480
}
464481

@@ -545,7 +562,7 @@ pub fn instruction_hover(
545562
}
546563
if let Some(reloc) = resolved.relocation {
547564
out.push(HoverItem::Separator);
548-
out.append(&mut relocation_hover(obj, reloc));
565+
out.append(&mut relocation_hover(obj, reloc, HoverItemColor::Normal));
549566
if let Some(ty) = obj.arch.guess_data_type(resolved) {
550567
let literals = display_ins_data_literals(obj, resolved);
551568
if !literals.is_empty() {

objdiff-gui/src/views/data_diff.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use objdiff_core::{
55
diff::{
66
DataDiff, DataDiffKind, DataRelocationDiff,
77
data::resolve_relocation,
8-
display::{ContextItem, HoverItem, relocation_context, relocation_hover},
8+
display::{ContextItem, HoverItem, HoverItemColor, relocation_context, relocation_hover},
99
},
1010
obj::Object,
1111
};
@@ -29,11 +29,10 @@ fn data_row_hover(obj: &Object, diffs: &[(DataDiff, Vec<DataRelocationDiff>)]) -
2929
}
3030
prev_reloc = Some(reloc);
3131

32-
// TODO: Change hover text color depending on Insert/Delete/Replace kind
33-
// let color = get_color_for_diff_kind(reloc_diff.kind, appearance);
32+
let color = get_hover_item_color_for_diff_kind(reloc_diff.kind);
3433

3534
let reloc = resolve_relocation(&obj.symbols, reloc);
36-
out.append(&mut relocation_hover(obj, reloc));
35+
out.append(&mut relocation_hover(obj, reloc, color));
3736
}
3837
out
3938
}
@@ -97,6 +96,15 @@ fn get_color_for_diff_kind(diff_kind: DataDiffKind, appearance: &Appearance) ->
9796
}
9897
}
9998

99+
fn get_hover_item_color_for_diff_kind(diff_kind: DataDiffKind) -> HoverItemColor {
100+
match diff_kind {
101+
DataDiffKind::None => HoverItemColor::Normal,
102+
DataDiffKind::Replace => HoverItemColor::Special,
103+
DataDiffKind::Delete => HoverItemColor::Delete,
104+
DataDiffKind::Insert => HoverItemColor::Insert,
105+
}
106+
}
107+
100108
pub(crate) fn data_row_ui(
101109
ui: &mut egui::Ui,
102110
obj: Option<&Object>,

objdiff-gui/src/views/diff.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ pub fn hover_items_ui(ui: &mut Ui, items: Vec<HoverItem>, appearance: &Appearanc
795795
if !label.is_empty() {
796796
let label_color = match color {
797797
HoverItemColor::Special => appearance.replace_color,
798+
HoverItemColor::Delete => appearance.delete_color,
799+
HoverItemColor::Insert => appearance.insert_color,
798800
_ => appearance.highlight_color,
799801
};
800802
write_text(&label, label_color, &mut job, appearance.code_font.clone());

objdiff-gui/src/views/symbol_diff.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use objdiff_core::{
88
diff::{
99
ObjectDiff, SymbolDiff,
1010
display::{
11-
HighlightKind, SectionDisplay, SymbolFilter, SymbolNavigationKind, display_sections,
12-
symbol_context, symbol_hover,
11+
HighlightKind, HoverItemColor, SectionDisplay, SymbolFilter, SymbolNavigationKind,
12+
display_sections, symbol_context, symbol_hover,
1313
},
1414
},
1515
jobs::{Job, JobQueue, JobResult, create_scratch::CreateScratchResult, objdiff::ObjDiffResult},
@@ -512,7 +512,11 @@ pub fn symbol_hover_ui(
512512
ui.scope(|ui| {
513513
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
514514
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Wrap);
515-
hover_items_ui(ui, symbol_hover(ctx.obj, symbol_idx, 0), appearance);
515+
hover_items_ui(
516+
ui,
517+
symbol_hover(ctx.obj, symbol_idx, 0, HoverItemColor::Normal),
518+
appearance,
519+
);
516520
});
517521
}
518522

0 commit comments

Comments
 (0)