Skip to content

Commit b8d1d53

Browse files
authored
Merge pull request #472 from epage/suggestion
refactor(render): Minor suggestion cleanup
2 parents 2a69bf8 + 60f9a2e commit b8d1d53

2 files changed

Lines changed: 60 additions & 42 deletions

File tree

‎src/renderer/render.rs‎

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,14 +1478,13 @@ fn emit_suggestion_default(
14781478
) {
14791479
let buffer_offset = buffer.num_lines();
14801480
let mut row_num = buffer_offset + usize::from(!matches_previous_suggestion);
1481-
let (complete, parts, highlights, replaced_highlights) = spliced_lines;
1482-
let is_multiline = complete.lines().count() > 1;
1481+
let is_multiline = spliced_lines.complete.lines().count() > 1;
14831482

14841483
if suggestion.path.as_ref() != primary_path
14851484
&& let Some(path) = suggestion.path.as_ref()
14861485
&& !matches_previous_suggestion
14871486
{
1488-
let (loc, _) = sm.span_to_locations(parts[0].span.clone());
1487+
let (loc, _) = sm.span_to_locations(spliced_lines.patches[0].span.clone());
14891488
// --> file.rs:line:col
14901489
// |
14911490
for _ in 0..max_line_num_len {
@@ -1520,17 +1519,27 @@ fn emit_suggestion_default(
15201519
row_num += 1;
15211520
}
15221521

1523-
let lo = parts.iter().map(|p| p.span.start).min().unwrap();
1524-
let hi = parts.iter().map(|p| p.span.end).max().unwrap();
1522+
let lo = spliced_lines
1523+
.patches
1524+
.iter()
1525+
.map(|p| p.span.start)
1526+
.min()
1527+
.unwrap();
1528+
let hi = spliced_lines
1529+
.patches
1530+
.iter()
1531+
.map(|p| p.span.end)
1532+
.max()
1533+
.unwrap();
15251534

15261535
let file_lines = sm.span_to_lines(lo..hi);
15271536
let (line_start, line_end) = if suggestion.fold {
15281537
// We use the original span to get original line_start
1529-
sm.span_to_locations(parts[0].original_span.clone())
1538+
sm.span_to_locations(spliced_lines.patches[0].original_span.clone())
15301539
} else {
15311540
sm.span_to_locations(0..sm.source.len())
15321541
};
1533-
let mut lines = complete.lines();
1542+
let mut lines = spliced_lines.complete.lines();
15341543
if lines.clone().next().is_none() {
15351544
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
15361545
for line in line_start.line..=line_end.line {
@@ -1556,7 +1565,9 @@ fn emit_suggestion_default(
15561565
row_num += line_end.line - line_start.line;
15571566
}
15581567
let mut unhighlighted_lines = Vec::new();
1559-
for (line_pos, (line, highlight_parts)) in lines.by_ref().zip(highlights).enumerate() {
1568+
for (line_pos, (line, highlight_parts)) in
1569+
lines.by_ref().zip(spliced_lines.highlights).enumerate()
1570+
{
15601571
// Remember lines that are not highlighted to hide them if needed
15611572
if highlight_parts.is_empty() && suggestion.fold {
15621573
unhighlighted_lines.push((line_pos, line));
@@ -1646,7 +1657,7 @@ fn emit_suggestion_default(
16461657
buffer,
16471658
&mut row_num,
16481659
&highlight_parts,
1649-
&replaced_highlights,
1660+
&spliced_lines.replaced_highlights,
16501661
line_pos + line_start.line,
16511662
line,
16521663
show_code_change,
@@ -1665,7 +1676,7 @@ fn emit_suggestion_default(
16651676
if let DisplaySuggestion::Diff | DisplaySuggestion::Underline | DisplaySuggestion::Add =
16661677
show_code_change
16671678
{
1668-
for part in parts {
1679+
for part in spliced_lines.patches {
16691680
let (span_start, span_end) = sm.span_to_locations(part.span.clone());
16701681
let span_start_pos = span_start.display;
16711682
let span_end_pos = span_end.display;
@@ -2732,36 +2743,38 @@ fn pre_process<'a>(
27322743
}
27332744
Element::Suggestion(suggestion) => {
27342745
let sm = SourceMap::new(&suggestion.source, suggestion.line_start);
2735-
if let Some((complete, patches, highlights, replaced_highlights)) =
2746+
if let Some(spliced_lines) =
27362747
sm.splice_lines(suggestion.markers.clone(), suggestion.fold)
27372748
{
2738-
let display_suggestion = DisplaySuggestion::new(&complete, &patches, &sm);
2739-
2740-
if suggestion.fold {
2741-
if let Some(first) = patches.first() {
2742-
let (l_start, _) =
2743-
sm.span_to_locations(first.original_span.clone());
2744-
let nc = newline_count(&complete);
2745-
let sugg_max_line_num = match display_suggestion {
2746-
DisplaySuggestion::Underline => l_start.line,
2747-
DisplaySuggestion::Diff => {
2748-
let file_lines = sm.span_to_lines(first.span.clone());
2749-
file_lines
2750-
.last()
2751-
.map_or(l_start.line + nc, |line| line.line_index)
2752-
}
2753-
DisplaySuggestion::None => l_start.line + nc,
2754-
DisplaySuggestion::Add => l_start.line + nc,
2755-
};
2756-
if suggestion.line_numbering {
2749+
let display_suggestion = DisplaySuggestion::new(
2750+
&spliced_lines.complete,
2751+
&spliced_lines.patches,
2752+
&sm,
2753+
);
2754+
2755+
if suggestion.line_numbering {
2756+
if suggestion.fold {
2757+
if let Some(first) = spliced_lines.patches.first() {
2758+
let (l_start, _) =
2759+
sm.span_to_locations(first.original_span.clone());
2760+
let nc = newline_count(&spliced_lines.complete);
2761+
let sugg_max_line_num = match display_suggestion {
2762+
DisplaySuggestion::Underline => l_start.line,
2763+
DisplaySuggestion::Diff => {
2764+
let file_lines = sm.span_to_lines(first.span.clone());
2765+
file_lines
2766+
.last()
2767+
.map_or(l_start.line + nc, |line| line.line_index)
2768+
}
2769+
DisplaySuggestion::None => l_start.line + nc,
2770+
DisplaySuggestion::Add => l_start.line + nc,
2771+
};
27572772
max_line_num =
27582773
Some(max(sugg_max_line_num, max_line_num.unwrap_or(0)));
27592774
}
2760-
}
2761-
} else {
2762-
if suggestion.line_numbering {
2775+
} else {
27632776
max_line_num = Some(max(
2764-
suggestion.line_start + newline_count(&complete),
2777+
suggestion.line_start + newline_count(&spliced_lines.complete),
27652778
max_line_num.unwrap_or(0),
27662779
));
27672780
}
@@ -2770,7 +2783,7 @@ fn pre_process<'a>(
27702783
elements.push(PreProcessedElement::Suggestion((
27712784
suggestion,
27722785
sm,
2773-
(complete, patches, highlights, replaced_highlights),
2786+
spliced_lines,
27742787
display_suggestion,
27752788
)));
27762789
}

‎src/renderer/source_map.rs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,12 @@ impl<'a> SourceMap<'a> {
557557
if highlights.iter().all(|parts| parts.is_empty()) {
558558
None
559559
} else {
560-
Some((buf, trimmed_patches, highlights, replaced_highlights))
560+
Some(SplicedLines {
561+
complete: buf,
562+
patches: trimmed_patches,
563+
highlights,
564+
replaced_highlights,
565+
})
561566
}
562567
}
563568
}
@@ -714,15 +719,15 @@ impl<'a> Iterator for CursorLines<'a> {
714719
}
715720
}
716721

717-
pub(crate) type SplicedLines<'a> = (
718-
String,
719-
Vec<TrimmedPatch<'a>>,
722+
pub(crate) struct SplicedLines<'a> {
723+
pub(crate) complete: String,
724+
pub(crate) patches: Vec<TrimmedPatch<'a>>,
720725
// Char spans to highlight per line of the post-substitution output.
721-
Vec<Vec<SubstitutionHighlight>>,
726+
pub(crate) highlights: Vec<Vec<SubstitutionHighlight>>,
722727
// Char spans of the replaced (original) code, per original line in the
723728
// bounding range covered by the splice.
724-
Vec<Vec<SubstitutionHighlight>>,
725-
);
729+
pub(crate) replaced_highlights: Vec<Vec<SubstitutionHighlight>>,
730+
}
726731

727732
/// Used to translate between `Span`s and byte positions within a single output line in highlighted
728733
/// code of structured suggestions.

0 commit comments

Comments
 (0)