Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions crates/fff-query-parser/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,26 @@ fn try_parse_column_position(location: &str) -> Option<Location> {

/// Parses various location formats like file:12, file:12:4, file:12-114
fn parse_column_location(query: &str) -> Option<(&str, Location)> {
let (file_path, location_part) = query.split_once(':')?;

if let Some(range_location) = try_parse_column_range(location_part) {
return Some((file_path, range_location));
}

if let Some(position_location) = try_parse_column_position(location_part) {
return Some((file_path, position_location));
}

if let Ok(line_location) = location_part.parse::<i32>() {
return Some((file_path, Location::Line(line_location)));
// Left to right, because `file:12:4` splits at its first colon. A Windows
// drive letter (`C:\...`) just makes that first colon fail and we move on.
let mut from = 0;
while let Some(offset) = query[from..].find(':') {
let at = from + offset;
let location_part = &query[at + 1..];

if let Some(range_location) = try_parse_column_range(location_part) {
return Some((&query[..at], range_location));
}

if let Some(position_location) = try_parse_column_position(location_part) {
return Some((&query[..at], position_location));
}

if let Ok(line_location) = location_part.parse::<i32>() {
return Some((&query[..at], Location::Line(line_location)));
}

from = at + 1;
}

None
Expand Down Expand Up @@ -262,4 +270,26 @@ mod tests {
assert_eq!(parse_location("file:-"), ("file", None));
assert_eq!(parse_location("file("), ("file", None));
}

#[test]
fn parses_location_after_a_windows_drive_letter() {
assert_eq!(
parse_location(r"C:\Users\me\file.rs:12"),
(r"C:\Users\me\file.rs", Some(Location::Line(12)))
);
assert_eq!(
parse_location(r"C:\src\main.rs:12:4"),
(
r"C:\src\main.rs",
Some(Location::Position { line: 12, col: 4 })
)
);

// A colon that starts no location still leaves the query untouched.
assert_eq!(
parse_location(r"C:\Users\me\file.rs"),
(r"C:\Users\me\file.rs", None)
);
assert_eq!(parse_location("foo:bar"), ("foo:bar", None));
}
}