Skip to content

Commit b9c4868

Browse files
authored
terminal: Support trailing :description or error message after file path (#26401)
Closes #25086 Release Notes: - Fixed a bug where file paths in the built in terminal of the format `path/to/file.ext:row:col:description or error message` would not be correctly identified as file paths due to the colon & additional text at the end
1 parent 570c396 commit b9c4868

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

crates/terminal/src/terminal.rs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -929,18 +929,51 @@ impl Terminal {
929929
} else if let Some(word_match) = regex_match_at(term, point, &mut self.word_regex) {
930930
let file_path = term.bounds_to_string(*word_match.start(), *word_match.end());
931931

932-
let (sanitized_match, sanitized_word) =
932+
let (sanitized_match, sanitized_word) = 'sanitize: {
933+
let mut word_match = word_match;
934+
let mut file_path = file_path;
935+
933936
if is_path_surrounded_by_common_symbols(&file_path) {
934-
(
935-
Match::new(
936-
word_match.start().add(term, Boundary::Cursor, 1),
937-
word_match.end().sub(term, Boundary::Cursor, 1),
938-
),
939-
file_path[1..file_path.len() - 1].to_owned(),
940-
)
941-
} else {
942-
(word_match, file_path)
943-
};
937+
word_match = Match::new(
938+
word_match.start().add(term, Boundary::Cursor, 1),
939+
word_match.end().sub(term, Boundary::Cursor, 1),
940+
);
941+
file_path = file_path[1..file_path.len() - 1].to_owned();
942+
}
943+
944+
let mut colon_count = 0;
945+
for c in file_path.chars() {
946+
if c == ':' {
947+
colon_count += 1;
948+
}
949+
}
950+
// strip trailing comment after colon in case of
951+
// file/at/path.rs:row:column:description or error message
952+
// so that the file path is `file/at/path.rs:row:column`
953+
if colon_count > 2 {
954+
let last_index = file_path.rfind(':').unwrap();
955+
let prev_is_digit = last_index > 0
956+
&& file_path
957+
.chars()
958+
.nth(last_index - 1)
959+
.map_or(false, |c| c.is_ascii_digit());
960+
let next_is_digit = last_index < file_path.len() - 1
961+
&& file_path
962+
.chars()
963+
.nth(last_index + 1)
964+
.map_or(true, |c| c.is_ascii_digit());
965+
if prev_is_digit && !next_is_digit {
966+
let stripped_len = file_path.len() - last_index;
967+
word_match = Match::new(
968+
*word_match.start(),
969+
word_match.end().sub(term, Boundary::Cursor, stripped_len),
970+
);
971+
file_path = file_path[0..last_index].to_owned();
972+
}
973+
}
974+
975+
break 'sanitize (word_match, file_path);
976+
};
944977

945978
Some((sanitized_word, false, sanitized_match))
946979
} else {

0 commit comments

Comments
 (0)