mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-08 05:35:22 +00:00
Add with_col_offset and with_row_offset to Location
This commit is contained in:
parent
b6647b0171
commit
72185fecd5
4 changed files with 44 additions and 8 deletions
|
@ -58,6 +58,34 @@ impl Location {
|
|||
self.row += 1;
|
||||
self.column = 0;
|
||||
}
|
||||
|
||||
pub fn with_col_offset<T: TryInto<isize>>(&self, offset: T) -> Self
|
||||
where
|
||||
<T as TryInto<isize>>::Error: std::fmt::Debug,
|
||||
{
|
||||
let column = (self.column as isize
|
||||
+ offset
|
||||
.try_into()
|
||||
.expect("offset should be able to convert to isize")) as u32;
|
||||
Location {
|
||||
row: self.row,
|
||||
column,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_row_offset<T: TryInto<isize>>(&self, offset: T) -> Self
|
||||
where
|
||||
<T as TryInto<isize>>::Error: std::fmt::Debug,
|
||||
{
|
||||
let row = (self.row as isize
|
||||
+ offset
|
||||
.try_into()
|
||||
.expect("offset should be able to convert to isize")) as u32;
|
||||
Location {
|
||||
row,
|
||||
column: self.column,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -77,4 +105,16 @@ mod tests {
|
|||
assert!(Location::new(1, 1) < Location::new(2, 1));
|
||||
assert!(Location::new(1, 2) < Location::new(2, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_col_offset() {
|
||||
assert_eq!(Location::new(1, 1).with_col_offset(1), Location::new(1, 2));
|
||||
assert_eq!(Location::new(1, 1).with_col_offset(-1), Location::new(1, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_row_offset() {
|
||||
assert_eq!(Location::new(1, 1).with_row_offset(1), Location::new(2, 1));
|
||||
assert_eq!(Location::new(1, 1).with_row_offset(-1), Location::new(0, 1));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue