-
-
Notifications
You must be signed in to change notification settings - Fork 365
Improve support for multiple blame ranges #1976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
holodorum
wants to merge
4
commits into
GitoxideLabs:main
Choose a base branch
from
holodorum:feature/blame-ranges-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−96
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,9 +57,9 @@ impl BlameRanges { | |
/// | ||
/// @param range: A 1-based inclusive range. | ||
/// @return: A `BlameRanges` instance representing the range. | ||
pub fn from_one_based_inclusive_range(range: RangeInclusive<u32>) -> Self { | ||
let zero_based_range = Self::inclusive_to_zero_based_exclusive(range); | ||
Self::PartialFile(vec![zero_based_range]) | ||
pub fn from_one_based_inclusive_range(range: RangeInclusive<u32>) -> Result<Self, Error> { | ||
let zero_based_range = Self::inclusive_to_zero_based_exclusive(range)?; | ||
Ok(Self::PartialFile(vec![zero_based_range])) | ||
} | ||
|
||
/// Create from multiple ranges. | ||
|
@@ -71,9 +71,9 @@ impl BlameRanges { | |
/// | ||
/// @param ranges: A vec of 1-based inclusive range. | ||
/// @return: A `BlameRanges` instance representing the range. | ||
pub fn from_one_based_inclusive_ranges(ranges: Vec<RangeInclusive<u32>>) -> Self { | ||
pub fn from_one_based_inclusive_ranges(ranges: Vec<RangeInclusive<u32>>) -> Result<Self, Error> { | ||
if ranges.is_empty() { | ||
return Self::WholeFile; | ||
return Ok(Self::WholeFile); | ||
} | ||
|
||
let zero_based_ranges = ranges | ||
|
@@ -82,16 +82,19 @@ impl BlameRanges { | |
.collect::<Vec<_>>(); | ||
let mut result = Self::PartialFile(vec![]); | ||
for range in zero_based_ranges { | ||
let _ = result.merge_range(range); | ||
let _ = result.merge_zero_based_exclusive_range(range?); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that the possibility of an error is explicitly ignored here, also seems to nudge toward making |
||
} | ||
result | ||
Ok(result) | ||
} | ||
|
||
/// Convert a 1-based inclusive range to a 0-based exclusive range. | ||
fn inclusive_to_zero_based_exclusive(range: RangeInclusive<u32>) -> Range<u32> { | ||
fn inclusive_to_zero_based_exclusive(range: RangeInclusive<u32>) -> Result<Range<u32>, Error> { | ||
if range.start() == &0 { | ||
return Err(Error::InvalidOneBasedLineRange); | ||
} | ||
let start = range.start() - 1; | ||
holodorum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let end = *range.end(); | ||
start..end | ||
Ok(start..end) | ||
} | ||
} | ||
|
||
|
@@ -101,44 +104,43 @@ impl BlameRanges { | |
/// The range should be 1-based inclusive. | ||
/// If the new range overlaps with or is adjacent to an existing range, | ||
/// they will be merged into a single range. | ||
pub fn add_range(&mut self, new_range: RangeInclusive<u32>) -> Result<(), Error> { | ||
pub fn add_one_based_inclusive_range(&mut self, new_range: RangeInclusive<u32>) -> Result<(), Error> { | ||
match self { | ||
Self::PartialFile(_) => { | ||
let zero_based_range = Self::inclusive_to_zero_based_exclusive(new_range); | ||
self.merge_range(zero_based_range) | ||
let zero_based_range = Self::inclusive_to_zero_based_exclusive(new_range)?; | ||
self.merge_zero_based_exclusive_range(zero_based_range) | ||
} | ||
_ => Err(Error::InvalidOneBasedLineRange), | ||
Self::WholeFile => Err(Error::InvalidRangeAddition), | ||
} | ||
} | ||
|
||
/// Attempts to merge the new range with any existing ranges. | ||
/// If no merge is possible, add it as a new range. | ||
fn merge_range(&mut self, new_range: Range<u32>) -> Result<(), Error> { | ||
fn merge_zero_based_exclusive_range(&mut self, new_range: Range<u32>) -> Result<(), Error> { | ||
match self { | ||
Self::PartialFile(ref mut ranges) => { | ||
// Check if this range can be merged with any existing range | ||
for range in &mut *ranges { | ||
// Check if ranges overlap | ||
if new_range.start <= range.end && range.start <= new_range.end { | ||
*range = range.start.min(new_range.start)..range.end.max(new_range.end); | ||
return Ok(()); | ||
} | ||
// Check if ranges are adjacent | ||
if new_range.start == range.end || range.start == new_range.end { | ||
*range = range.start.min(new_range.start)..range.end.max(new_range.end); | ||
return Ok(()); | ||
} | ||
} | ||
// If no overlap or adjacency found, add it as a new range | ||
ranges.push(new_range); | ||
// Partition ranges into those that don't overlap/aren't adjacent and those that do | ||
let (mut non_overlapping, overlapping): (Vec<_>, Vec<_>) = ranges.drain(..).partition(|range| { | ||
// Check if ranges DON'T overlap and are NOT adjacent | ||
new_range.end < range.start || range.end < new_range.start | ||
}); | ||
|
||
// Fold all overlapping ranges together with the new range | ||
let merged_range = overlapping.into_iter().fold(new_range, |acc, range| { | ||
acc.start.min(range.start)..acc.end.max(range.end) | ||
}); | ||
|
||
// Add back non-overlapping ranges and the merged range | ||
non_overlapping.push(merged_range); | ||
*ranges = non_overlapping; | ||
Ok(()) | ||
} | ||
_ => Err(Error::InvalidOneBasedLineRange), | ||
Self::WholeFile => Err(Error::InvalidRangeAddition), | ||
} | ||
} | ||
|
||
/// Convert the ranges to a vector of `Range<u32>`. | ||
pub fn to_ranges(&self, max_lines: u32) -> Vec<Range<u32>> { | ||
pub fn to_zero_based_exclusive_ranges(&self, max_lines: u32) -> Vec<Range<u32>> { | ||
match self { | ||
Self::WholeFile => { | ||
let full_range = 0..max_lines; | ||
|
@@ -345,10 +347,9 @@ pub struct UnblamedHunk { | |
} | ||
|
||
impl UnblamedHunk { | ||
/// Create a new instance | ||
pub fn new(range: Range<u32>, suspect: ObjectId) -> Self { | ||
let range_start = range.start; | ||
let range_end = range.end; | ||
pub fn new(from_range_in_blamed_file: Range<u32>, suspect: ObjectId) -> Self { | ||
let range_start = from_range_in_blamed_file.start; | ||
let range_end = from_range_in_blamed_file.end; | ||
|
||
UnblamedHunk { | ||
range_in_blamed_file: range_start..range_end, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.