| 1 |
use std::borrow::Cow; |
| 2 |
|
| 3 |
use lazy_static::lazy_static; |
| 4 |
use regex::Regex; |
| 5 |
use serde::Deserialize; |
| 6 |
|
| 7 |
use crate::ansi; |
| 8 |
use crate::config::{ |
| 9 |
delta_unreachable, GrepType, HunkHeaderIncludeCodeFragment, HunkHeaderIncludeFilePath, |
| 10 |
HunkHeaderIncludeLineNumber, |
| 11 |
}; |
| 12 |
use crate::delta::{State, StateMachine}; |
| 13 |
use crate::handlers::{self, ripgrep_json}; |
| 14 |
use crate::paint::{self, BgShouldFill, StyleSectionSpecifier}; |
| 15 |
use crate::style::Style; |
| 16 |
use crate::utils::{process, tabs}; |
| 17 |
|
| 18 |
use super::hunk_header::HunkHeaderIncludeHunkLabel; |
| 19 |
|
| 20 |
#[derive(Debug, PartialEq, Eq)] |
| 21 |
pub struct GrepLine<'b> { |
| 22 |
pub grep_type: GrepType, |
| 23 |
pub path: Cow<'b, str>, |
| 24 |
pub line_number: Option<usize>, |
| 25 |
pub line_type: LineType, |
| 26 |
pub code: Cow<'b, str>, |
| 27 |
pub submatches: Option<Vec<(usize, usize)>>, |
| 28 |
} |
| 29 |
|
| 30 |
impl GrepLine<'_> { |
| 31 |
fn expand_tabs(&mut self, tab_cfg: &tabs::TabCfg) { |
| 32 |
let old_len = self.code.len(); |
| 33 |
self.code = tabs::expand(&self.code, tab_cfg).into(); |
| 34 |
let shift = self.code.len().saturating_sub(old_len); |
| 35 |
// HACK: it is not necessarily the case that all submatch coordinates |
| 36 |
// should be shifted in this way. It should be true in a common case of: |
| 37 |
// (a) the only tabs were at the beginning of the line, and (b) the user |
| 38 |
// was not searching for tabs. |
| 39 |
self.submatches = self.submatches.as_ref().map(|submatches| { |
| 40 |
submatches |
| 41 |
.iter() |
| 42 |
.map(|(a, b)| (a + shift, b + shift)) |
| 43 |
.collect() |
| 44 |
}); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)] |
| 49 |
#[serde(rename_all = "lowercase")] |
| 50 |
pub enum LineType { |
| 51 |
ContextHeader, |
| 52 |
Context, |
| 53 |
FileHeader, |
| 54 |
Match, |
| 55 |
Ignore, |
| 56 |
} |
| 57 |
|
| 58 |
struct GrepOutputConfig { |
| 59 |
add_navigate_marker_to_matches: bool, |
| 60 |
render_context_header_as_hunk_header: bool, |
| 61 |
pad_line_number: bool, |
| 62 |
} |
| 63 |
|
| 64 |
lazy_static! { |
| 65 |
static ref OUTPUT_CONFIG: GrepOutputConfig = make_output_config(); |
| 66 |
} |
| 67 |
|
| 68 |
impl LineType { |
| 69 |
fn file_path_separator(&self) -> &str { |
| 70 |
// grep, rg, and git grep use ":" for matching lines |
| 71 |
// and "-" for non-matching lines (and `git grep -W` |
| 72 |
// uses "=" for a context header line). |
| 73 |
match self { |
| 74 |
LineType::Match => ":", |
| 75 |
LineType::Context => "-", |
| 76 |
LineType::ContextHeader => "=", |
| 77 |
LineType::Ignore | LineType::FileHeader => "", |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
impl StateMachine<'_> { |
| 83 |
// If this is a line of grep output then render it accordingly. |
| 84 |
pub fn handle_grep_line(&mut self) -> std::io::Result<bool> { |
| 85 |
self.painter.emit()?; |
| 86 |
|
| 87 |
let (previous_path, previous_line_type, previous_line, try_parse) = match &self.state { |
| 88 |
State::Grep(_, line_type, path, line_number) => { |
| 89 |
(Some(path.clone()), Some(line_type), line_number, true) |
| 90 |
} |
| 91 |
State::Unknown => (None, None, &None, true), |
| 92 |
_ => (None, None, &None, false), |
| 93 |
}; |
| 94 |
if !try_parse { |
| 95 |
return Ok(false); |
| 96 |
} |
| 97 |
|
| 98 |
// Try parse_raw_grep_line on raw_line, and fall back to parse_grep_line |
| 99 |
let raw_line = self.raw_line.clone(); // TODO: avoid clone |
| 100 |
let line; |
| 101 |
let grep_line = if let Some(grep_line) = parse_raw_grep_line(&raw_line) { |
| 102 |
grep_line |
| 103 |
} else { |
| 104 |
line = self.line.clone(); // TODO: avoid clone |
| 105 |
if let Some(grep_line) = parse_grep_line(&line) { |
| 106 |
grep_line |
| 107 |
} else { |
| 108 |
return Ok(false); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
if matches!(grep_line.line_type, LineType::Ignore) { |
| 113 |
return Ok(true); |
| 114 |
} |
| 115 |
let first_path = previous_path.is_none(); |
| 116 |
let new_path = first_path || previous_path.as_deref() != Some(&grep_line.path); |
| 117 |
let line_number_jump = previous_line < &grep_line.line_number.as_ref().map(|n| n - 1); |
| 118 |
// Emit a '--' section separator when output contains context lines (i.e. *grep option -A, -B, -C is in effect). |
| 119 |
let new_section = !new_path |
| 120 |
&& (previous_line_type == Some(&LineType::Context) |
| 121 |
|| grep_line.line_type == LineType::Context) |
| 122 |
&& line_number_jump; |
| 123 |
if new_path { |
| 124 |
self.painter.set_syntax(Some(grep_line.path.as_ref())); |
| 125 |
} |
| 126 |
if new_path || new_section { |
| 127 |
self.painter.set_highlighter() |
| 128 |
} |
| 129 |
self.state = State::Grep( |
| 130 |
self.config |
| 131 |
.grep_output_type |
| 132 |
.clone() |
| 133 |
.unwrap_or_else(|| grep_line.grep_type.clone()), |
| 134 |
grep_line.line_type, |
| 135 |
grep_line.path.to_string(), |
| 136 |
grep_line.line_number, |
| 137 |
); |
| 138 |
match &self.state { |
| 139 |
State::Grep(GrepType::Ripgrep, _, _, _) => { |
| 140 |
self.emit_ripgrep_format_grep_line(grep_line, new_path, first_path, new_section) |
| 141 |
} |
| 142 |
State::Grep(GrepType::Classic, _, _, _) => { |
| 143 |
self.emit_classic_format_grep_line(grep_line) |
| 144 |
} |
| 145 |
_ => delta_unreachable("Impossible state while handling grep line."), |
| 146 |
}?; |
| 147 |
Ok(true) |
| 148 |
} |
| 149 |
|
| 150 |
// Emulate ripgrep output: each section of hits from the same path has a header line, |
| 151 |
// and sections are separated by a blank line. Set language whenever path changes. |
| 152 |
fn emit_ripgrep_format_grep_line( |
| 153 |
&mut self, |
| 154 |
mut grep_line: GrepLine, |
| 155 |
new_path: bool, |
| 156 |
first_path: bool, |
| 157 |
new_section: bool, |
| 158 |
) -> std::io::Result<()> { |
| 159 |
if new_path { |
| 160 |
// Emit new path header line |
| 161 |
if !first_path { |
| 162 |
writeln!(self.painter.writer)?; |
| 163 |
} |
| 164 |
handlers::hunk_header::write_line_of_code_with_optional_path_and_line_number( |
| 165 |
"", |
| 166 |
&[(0, 0)], |
| 167 |
None, |
| 168 |
&mut self.painter, |
| 169 |
&self.line, |
| 170 |
&grep_line.path, |
| 171 |
self.config.ripgrep_header_style.decoration_style, |
| 172 |
&self.config.grep_file_style, |
| 173 |
&self.config.grep_line_number_style, |
| 174 |
&HunkHeaderIncludeFilePath::Yes, |
| 175 |
&HunkHeaderIncludeLineNumber::No, |
| 176 |
&HunkHeaderIncludeHunkLabel::Yes, |
| 177 |
&HunkHeaderIncludeCodeFragment::YesNoSpace, |
| 178 |
"", |
| 179 |
self.config, |
| 180 |
)? |
| 181 |
} |
| 182 |
if new_section { |
| 183 |
writeln!(self.painter.writer, "--")?; |
| 184 |
} |
| 185 |
// Emit the actual grep hit line |
| 186 |
let code_style_sections = match (&grep_line.line_type, &grep_line.submatches) { |
| 187 |
(LineType::Match, Some(_)) => { |
| 188 |
// We expand tabs at this late stage because |
| 189 |
// the tabs are escaped in the JSON, so |
| 190 |
// expansion must come after JSON parsing. |
| 191 |
// (At the time of writing, we are in this |
| 192 |
// arm iff we are handling `ripgrep --json` |
| 193 |
// output.) |
| 194 |
grep_line.expand_tabs(&self.config.tab_cfg); |
| 195 |
make_style_sections( |
| 196 |
&grep_line.code, |
| 197 |
&grep_line.submatches.unwrap(), |
| 198 |
self.config.grep_match_word_style, |
| 199 |
self.config.grep_match_line_style, |
| 200 |
) |
| 201 |
} |
| 202 |
(LineType::Match, None) => { |
| 203 |
// HACK: We need tabs expanded, and we need |
| 204 |
// the &str passed to |
| 205 |
// `get_code_style_sections` to live long |
| 206 |
// enough. But at this point it is guaranteed |
| 207 |
// that this handler is going to handle this |
| 208 |
// line, so mutating it is acceptable. |
| 209 |
self.raw_line = tabs::expand(&self.raw_line, &self.config.tab_cfg); |
| 210 |
get_code_style_sections( |
| 211 |
&self.raw_line, |
| 212 |
self.config.grep_match_word_style, |
| 213 |
self.config.grep_match_line_style, |
| 214 |
&grep_line.path, |
| 215 |
grep_line.line_number, |
| 216 |
) |
| 217 |
.unwrap_or(StyleSectionSpecifier::Style( |
| 218 |
self.config.grep_match_line_style, |
| 219 |
)) |
| 220 |
} |
| 221 |
_ => StyleSectionSpecifier::Style(self.config.grep_context_line_style), |
| 222 |
}; |
| 223 |
handlers::hunk_header::write_line_of_code_with_optional_path_and_line_number( |
| 224 |
&grep_line.code, |
| 225 |
&[(grep_line.line_number.unwrap_or(0), 0)], |
| 226 |
Some(code_style_sections), |
| 227 |
&mut self.painter, |
| 228 |
&self.line, |
| 229 |
&grep_line.path, |
| 230 |
crate::style::DecorationStyle::NoDecoration, |
| 231 |
&self.config.grep_file_style, |
| 232 |
&self.config.grep_line_number_style, |
| 233 |
&HunkHeaderIncludeFilePath::No, |
| 234 |
if grep_line.line_number.is_some() { |
| 235 |
&HunkHeaderIncludeLineNumber::Yes |
| 236 |
} else { |
| 237 |
&HunkHeaderIncludeLineNumber::No |
| 238 |
}, |
| 239 |
&HunkHeaderIncludeHunkLabel::No, |
| 240 |
&HunkHeaderIncludeCodeFragment::YesNoSpace, |
| 241 |
grep_line.line_type.file_path_separator(), |
| 242 |
self.config, |
| 243 |
) |
| 244 |
} |
| 245 |
|
| 246 |
fn emit_classic_format_grep_line(&mut self, grep_line: GrepLine) -> std::io::Result<()> { |
| 247 |
match ( |
| 248 |
&grep_line.line_type, |
| 249 |
OUTPUT_CONFIG.render_context_header_as_hunk_header, |
| 250 |
) { |
| 251 |
// Emit context header line (`git grep -W`) |
| 252 |
(LineType::ContextHeader, true) => { |
| 253 |
handlers::hunk_header::write_line_of_code_with_optional_path_and_line_number( |
| 254 |
&grep_line.code, |
| 255 |
&[(grep_line.line_number.unwrap_or(0), 0)], |
| 256 |
None, |
| 257 |
&mut self.painter, |
| 258 |
&self.line, |
| 259 |
&grep_line.path, |
| 260 |
self.config.classic_grep_header_style.decoration_style, |
| 261 |
&self.config.classic_grep_header_file_style, |
| 262 |
&self.config.grep_line_number_style, |
| 263 |
&self.config.hunk_header_style_include_file_path, |
| 264 |
&self.config.hunk_header_style_include_line_number, |
| 265 |
&HunkHeaderIncludeHunkLabel::Yes, |
| 266 |
&HunkHeaderIncludeCodeFragment::Yes, |
| 267 |
grep_line.line_type.file_path_separator(), |
| 268 |
self.config, |
| 269 |
)? |
| 270 |
} |
| 271 |
_ => { |
| 272 |
if self.config.navigate { |
| 273 |
write!( |
| 274 |
self.painter.writer, |
| 275 |
"{}", |
| 276 |
match ( |
| 277 |
&grep_line.line_type, |
| 278 |
OUTPUT_CONFIG.add_navigate_marker_to_matches |
| 279 |
) { |
| 280 |
(LineType::Match, true) => "• ", |
| 281 |
(_, true) => " ", |
| 282 |
_ => "", |
| 283 |
} |
| 284 |
)? |
| 285 |
} |
| 286 |
self._emit_classic_format_file_and_line_number(&grep_line)?; |
| 287 |
self._emit_classic_format_code(grep_line)?; |
| 288 |
} |
| 289 |
} |
| 290 |
Ok(()) |
| 291 |
} |
| 292 |
|
| 293 |
fn _emit_classic_format_file_and_line_number( |
| 294 |
&mut self, |
| 295 |
grep_line: &GrepLine, |
| 296 |
) -> std::io::Result<()> { |
| 297 |
let separator = if self.config.grep_separator_symbol == "keep" { |
| 298 |
grep_line.line_type.file_path_separator() |
| 299 |
} else { |
| 300 |
// But ":" results in a "file/path:number:" |
| 301 |
// construct that terminal emulators are more likely |
| 302 |
// to recognize and render as a clickable link. If |
| 303 |
// navigate is enabled then there is already a good |
| 304 |
// visual indicator of match lines (in addition to |
| 305 |
// the grep-match-style highlighting) and so we use |
| 306 |
// ":" for matches and non-matches alike. |
| 307 |
&self.config.grep_separator_symbol |
| 308 |
}; |
| 309 |
write!( |
| 310 |
self.painter.writer, |
| 311 |
"{}", |
| 312 |
paint::paint_file_path_with_line_number( |
| 313 |
grep_line.line_number, |
| 314 |
&grep_line.path, |
| 315 |
OUTPUT_CONFIG.pad_line_number, |
| 316 |
separator, |
| 317 |
true, |
| 318 |
Some(self.config.grep_file_style), |
| 319 |
Some(self.config.grep_line_number_style), |
| 320 |
self.config |
| 321 |
) |
| 322 |
)?; |
| 323 |
Ok(()) |
| 324 |
} |
| 325 |
|
| 326 |
fn _emit_classic_format_code(&mut self, mut grep_line: GrepLine) -> std::io::Result<()> { |
| 327 |
let code_style_sections = match (&grep_line.line_type, &grep_line.submatches) { |
| 328 |
(LineType::Match, Some(_)) => { |
| 329 |
// We expand tabs at this late stage because |
| 330 |
// the tabs are escaped in the JSON, so |
| 331 |
// expansion must come after JSON parsing. |
| 332 |
// (At the time of writing, we are in this |
| 333 |
// arm iff we are handling `ripgrep --json` |
| 334 |
// output.) |
| 335 |
grep_line.expand_tabs(&self.config.tab_cfg); |
| 336 |
make_style_sections( |
| 337 |
&grep_line.code, |
| 338 |
&grep_line.submatches.unwrap(), |
| 339 |
self.config.grep_match_word_style, |
| 340 |
self.config.grep_match_line_style, |
| 341 |
) |
| 342 |
} |
| 343 |
(LineType::Match, None) => { |
| 344 |
// HACK: We need tabs expanded, and we need |
| 345 |
// the &str passed to |
| 346 |
// `get_code_style_sections` to live long |
| 347 |
// enough. But at the point it is guaranteed |
| 348 |
// that this handler is going to handle this |
| 349 |
// line, so mutating it is acceptable. |
| 350 |
self.raw_line = tabs::expand(&self.raw_line, &self.config.tab_cfg); |
| 351 |
get_code_style_sections( |
| 352 |
&self.raw_line, |
| 353 |
self.config.grep_match_word_style, |
| 354 |
self.config.grep_match_line_style, |
| 355 |
&grep_line.path, |
| 356 |
grep_line.line_number, |
| 357 |
) |
| 358 |
.unwrap_or(StyleSectionSpecifier::Style( |
| 359 |
self.config.grep_match_line_style, |
| 360 |
)) |
| 361 |
} |
| 362 |
_ => StyleSectionSpecifier::Style(self.config.grep_context_line_style), |
| 363 |
}; |
| 364 |
self.painter.syntax_highlight_and_paint_line( |
| 365 |
&format!("{}\n", grep_line.code), |
| 366 |
code_style_sections, |
| 367 |
self.state.clone(), |
| 368 |
BgShouldFill::default(), |
| 369 |
); |
| 370 |
Ok(()) |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
fn make_style_sections<'a>( |
| 375 |
line: &'a str, |
| 376 |
submatches: &[(usize, usize)], |
| 377 |
match_style: Style, |
| 378 |
non_match_style: Style, |
| 379 |
) -> StyleSectionSpecifier<'a> { |
| 380 |
let mut sections = Vec::new(); |
| 381 |
let mut curr = 0; |
| 382 |
for (start_, end_) in submatches { |
| 383 |
let (start, end) = (*start_, *end_); |
| 384 |
if start > curr { |
| 385 |
sections.push((non_match_style, &line[curr..start])) |
| 386 |
}; |
| 387 |
sections.push((match_style, &line[start..end])); |
| 388 |
curr = end; |
| 389 |
} |
| 390 |
if curr < line.len() { |
| 391 |
sections.push((non_match_style, &line[curr..])) |
| 392 |
} |
| 393 |
StyleSectionSpecifier::StyleSections(sections) |
| 394 |
} |
| 395 |
|
| 396 |
// Return style sections describing colors received from git. |
| 397 |
fn get_code_style_sections<'b>( |
| 398 |
raw_line: &'b str, |
| 399 |
match_style: Style, |
| 400 |
non_match_style: Style, |
| 401 |
path: &str, |
| 402 |
line_number: Option<usize>, |
| 403 |
) -> Option<StyleSectionSpecifier<'b>> { |
| 404 |
if let Some(prefix_end) = ansi::ansi_preserving_index( |
| 405 |
raw_line, |
| 406 |
match line_number { |
| 407 |
Some(n) => format!("{path}:{n}:").len() - 1, |
| 408 |
None => path.len(), |
| 409 |
}, |
| 410 |
) { |
| 411 |
let match_style_sections = ansi::parse_style_sections(&raw_line[(prefix_end + 1)..]) |
| 412 |
.iter() |
| 413 |
.map(|(ansi_term_style, s)| { |
| 414 |
if ansi_term_style.is_bold |
| •415 |
&& ansi_term_style.foreground == Some(ansi_term::Color::Red) |
| 416 |
{ |
| 417 |
(match_style, *s) |
| 418 |
} else { |
| 419 |
(non_match_style, *s) |
| 420 |
} |
| 421 |
}) |
| 422 |
.collect(); |
| 423 |
Some(StyleSectionSpecifier::StyleSections(match_style_sections)) |
| 424 |
} else { |
| 425 |
None |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
fn make_output_config() -> GrepOutputConfig { |
| 430 |
match &*process::calling_process() { |
| 431 |
process::CallingProcess::GitGrep(command_line) |
| 432 |
if command_line.short_options.contains("-W") |
| 433 |
|| command_line.long_options.contains("--function-context") => |
| 434 |
{ |
| 435 |
// --function-context is in effect: i.e. the entire function is |
| 436 |
// being displayed. In that case we don't render the first line as a |
| 437 |
// header, since the second line is the true next line, and it will |
| 438 |
// be more readable to have these displayed normally. We do add the |
| 439 |
// navigate marker, since match lines will be surrounded by (many) |
| 440 |
// non-match lines. And, since we are printing (many) successive lines |
| 441 |
// of code, we pad line numbers <100 in order to maintain code |
| 442 |
// alignment up to line 9999. |
| 443 |
GrepOutputConfig { |
| 444 |
render_context_header_as_hunk_header: false, |
| 445 |
add_navigate_marker_to_matches: true, |
| 446 |
pad_line_number: true, |
| 447 |
} |
| 448 |
} |
| 449 |
process::CallingProcess::GitGrep(command_line) |
| 450 |
if command_line.short_options.contains("-p") |
| 451 |
|| command_line.long_options.contains("--show-function") => |
| 452 |
{ |
| 453 |
// --show-function is in effect, i.e. the function header is being |
| 454 |
// displayed, along with matches within the function. Therefore we |
| 455 |
// render the first line as a header, but we do not add the navigate |
| 456 |
// marker, since all non-header lines are matches. |
| 457 |
GrepOutputConfig { |
| 458 |
render_context_header_as_hunk_header: true, |
| 459 |
add_navigate_marker_to_matches: false, |
| 460 |
pad_line_number: true, |
| 461 |
} |
| 462 |
} |
| 463 |
_ => GrepOutputConfig { |
| 464 |
render_context_header_as_hunk_header: true, |
| 465 |
add_navigate_marker_to_matches: false, |
| 466 |
pad_line_number: true, |
| 467 |
}, |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
enum GrepLineRegex { |
| 472 |
WithColor, |
| 473 |
WithFileExtensionAndLineNumber, |
| 474 |
WithFileExtension, |
| 475 |
WithFileExtensionNoSpaces, |
| 476 |
WithoutSeparatorCharacters, |
| 477 |
} |
| 478 |
|
| 479 |
lazy_static! { |
| 480 |
static ref GREP_LINE_REGEX_ASSUMING_COLOR: Regex = |
| 481 |
make_grep_line_regex(GrepLineRegex::WithColor); |
| 482 |
} |
| 483 |
|
| 484 |
lazy_static! { |
| 485 |
static ref GREP_LINE_REGEX_ASSUMING_FILE_EXTENSION_AND_LINE_NUMBER: Regex = |
| 486 |
make_grep_line_regex(GrepLineRegex::WithFileExtensionAndLineNumber); |
| 487 |
} |
| 488 |
|
| 489 |
lazy_static! { |
| 490 |
static ref GREP_LINE_REGEX_ASSUMING_FILE_EXTENSION_NO_SPACES: Regex = |
| 491 |
make_grep_line_regex(GrepLineRegex::WithFileExtensionNoSpaces); |
| 492 |
} |
| 493 |
|
| 494 |
lazy_static! { |
| 495 |
static ref GREP_LINE_REGEX_ASSUMING_FILE_EXTENSION: Regex = |
| 496 |
make_grep_line_regex(GrepLineRegex::WithFileExtension); |
| 497 |
} |
| 498 |
|
| 499 |
lazy_static! { |
| 500 |
static ref GREP_LINE_REGEX_ASSUMING_NO_INTERNAL_SEPARATOR_CHARS: Regex = |
| 501 |
make_grep_line_regex(GrepLineRegex::WithoutSeparatorCharacters); |
| 502 |
} |
| 503 |
|
| 504 |
// See tests for example grep lines |
| 505 |
fn make_grep_line_regex(regex_variant: GrepLineRegex) -> Regex { |
| 506 |
// Grep tools such as `git grep` and `rg` emit lines like the following, |
| 507 |
// where "xxx" represents arbitrary code. Note that there are 3 possible |
| 508 |
// "separator characters": ':', '-', '='. |
| 509 |
|
| 510 |
// The format is ambiguous, but we attempt to parse it. |
| 511 |
|
| 512 |
// src/co-7-fig.rs:xxx |
| 513 |
// src/co-7-fig.rs:7:xxx |
| 514 |
// src/co-7-fig.rs-xxx |
| 515 |
// src/co-7-fig.rs-7-xxx |
| 516 |
// src/co-7-fig.rs=xxx |
| 517 |
// src/co-7-fig.rs=7=xxx |
| 518 |
|
| 519 |
// Makefile:xxx |
| 520 |
// Makefile:7:xxx |
| 521 |
// Makefile-xxx |
| 522 |
// Makefile-7-xxx |
| 523 |
|
| 524 |
// Make-7-file:xxx |
| 525 |
// Make-7-file:7:xxx |
| 526 |
// Make-7-file-xxx |
| 527 |
// Make-7-file-7-xxx |
| 528 |
|
| 529 |
let file_path = match regex_variant { |
| 530 |
GrepLineRegex::WithColor => { |
| 531 |
r" |
| 532 |
\x1b\[35m # starts with ANSI color sequence |
| 533 |
( # 1. file name |
| 534 |
[^\x1b]* # anything |
| 535 |
) |
| 536 |
\x1b\[m # ends with ANSI color reset |
| 537 |
" |
| 538 |
} |
| 539 |
GrepLineRegex::WithFileExtensionAndLineNumber | GrepLineRegex::WithFileExtension => { |
| 540 |
r" |
| 541 |
( # 1. file name (colons not allowed) |
| 542 |
[^:|\ ] # try to be strict about what a file path can start with |
| 543 |
[^:]* # anything |
| 544 |
[^\ ]\.[^.\ :=-]{1,10} # extension |
| 545 |
) |
| 546 |
" |
| 547 |
} |
| 548 |
GrepLineRegex::WithFileExtensionNoSpaces => { |
| 549 |
r" |
| 550 |
( # 1. file name (colons not allowed) |
| 551 |
[^:|\ ]+ # try to be strict about what a file path can start with |
| 552 |
[^\ ]\.[^.\ :=-]{1,6} # extension |
| 553 |
) |
| 554 |
" |
| 555 |
} |
| 556 |
GrepLineRegex::WithoutSeparatorCharacters => { |
| 557 |
r" |
| 558 |
( # 1. file name (colons not allowed) |
| 559 |
[^:|\ =-] # try to be strict about what a file path can start with |
| 560 |
[^:=-]* # anything except separators |
| 561 |
[^:\ ] # a file name cannot end with whitespace |
| 562 |
) |
| 563 |
" |
| 564 |
} |
| 565 |
}; |
| 566 |
|
| 567 |
let separator = match regex_variant { |
| 568 |
GrepLineRegex::WithColor => { |
| 569 |
r#" |
| 570 |
\x1b\[36m # starts with ANSI color sequence for separator |
| 571 |
(?: |
| 572 |
( |
| 573 |
: # 2. match marker |
| 574 |
\x1b\[m # ANSI color reset |
| 575 |
(?: # optional: line number followed by second match marker |
| 576 |
\x1b\[32m # ANSI color sequence for line number |
| 577 |
([0-9]+) # 3. line number |
| 578 |
\x1b\[m # ANSI color reset |
| 579 |
\x1b\[36m # ANSI color sequence for separator |
| 580 |
: # second match marker |
| 581 |
\x1b\[m # ANSI color reset |
| 582 |
)? |
| 583 |
) |
| 584 |
| |
| 585 |
( |
| 586 |
- # 4. nomatch marker |
| 587 |
\x1b\[m # ANSI color reset |
| 588 |
(?: # optional: line number followed by second nomatch marker |
| 589 |
\x1b\[32m # ANSI color sequence for line number |
| 590 |
([0-9]+) # 5. line number |
| 591 |
\x1b\[m # ANSI color reset |
| 592 |
\x1b\[36m # ANSI color sequence for separator |
| 593 |
- # second nomatch marker |
| 594 |
\x1b\[m # ANSI color reset |
| 595 |
)? |
| 596 |
) |
| 597 |
| |
| 598 |
( |
| 599 |
= # 6. context header marker |
| 600 |
\x1b\[m # ANSI color reset |
| 601 |
(?: # optional: line number followed by second context header marker |
| 602 |
\x1b\[32m # ANSI color sequence for line number |
| 603 |
([0-9]+) # 7. line number |
| 604 |
\x1b\[m # ANSI color reset |
| 605 |
\x1b\[36m # ANSI color sequence for separator |
| 606 |
= # second context header marker |
| 607 |
\x1b\[m # ANSI color reset |
| 608 |
)? |
| 609 |
) |
| 610 |
) |
| 611 |
"# |
| 612 |
} |
| 613 |
GrepLineRegex::WithFileExtensionAndLineNumber => { |
| 614 |
r#" |
| 615 |
(?: |
| 616 |
( |
| 617 |
: # 2. match marker |
| 618 |
([0-9]+): # 3. line number followed by second match marker |
| 619 |
) |
| 620 |
| |
| 621 |
( |
| 622 |
- # 4. nomatch marker |
| 623 |
([0-9]+)- # 5. line number followed by second nomatch marker |
| 624 |
) |
| 625 |
| |
| 626 |
( |
| 627 |
= # 6. context header marker |
| 628 |
([0-9]+)= # 7. line number followed by second header marker |
| 629 |
) |
| 630 |
) |
| 631 |
"# |
| 632 |
} |
| 633 |
_ => { |
| 634 |
r#" |
| 635 |
(?: |
| 636 |
( |
| 637 |
: # 2. match marker |
| 638 |
(?:([0-9]+):)? # 3. optional: line number followed by second match marker |
| 639 |
) |
| 640 |
| |
| 641 |
( |
| 642 |
- # 4. nomatch marker |
| 643 |
(?:([0-9]+)-)? # 5. optional: line number followed by second nomatch marker |
| 644 |
) |
| 645 |
| |
| 646 |
( |
| 647 |
= # 6. context header marker |
| 648 |
(?:([0-9]+)=)? # 7. optional: line number followed by second header marker |
| 649 |
) |
| 650 |
) |
| 651 |
"# |
| 652 |
} |
| 653 |
}; |
| 654 |
|
| 655 |
Regex::new(&format!( |
| 656 |
"(?x) |
| 657 |
^ |
| 658 |
{file_path} |
| 659 |
{separator} |
| 660 |
(.*) # 8. code (i.e. line contents) |
| 661 |
$ |
| 662 |
", |
| 663 |
)) |
| 664 |
.unwrap() |
| 665 |
} |
| 666 |
|
| 667 |
pub fn parse_grep_line(line: &str) -> Option<GrepLine<'_>> { |
| 668 |
if line.starts_with('{') { |
| 669 |
ripgrep_json::parse_line(line) |
| 670 |
} else { |
| 671 |
match &*process::calling_process() { |
| 672 |
process::CallingProcess::GitGrep(_) | process::CallingProcess::OtherGrep => [ |
| 673 |
&*GREP_LINE_REGEX_ASSUMING_FILE_EXTENSION_AND_LINE_NUMBER, |
| 674 |
&*GREP_LINE_REGEX_ASSUMING_FILE_EXTENSION_NO_SPACES, |
| 675 |
&*GREP_LINE_REGEX_ASSUMING_FILE_EXTENSION, |
| 676 |
&*GREP_LINE_REGEX_ASSUMING_NO_INTERNAL_SEPARATOR_CHARS, |
| 677 |
] |
| 678 |
.iter() |
| 679 |
.find_map(|regex| _parse_grep_line(regex, line)), |
| 680 |
_ => None, |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
pub fn parse_raw_grep_line(raw_line: &str) -> Option<GrepLine<'_>> { |
| 686 |
// Early exit if we don't have an escape sequence |
| 687 |
if !raw_line.starts_with('\x1b') { |
| 688 |
return None; |
| 689 |
} |
| 690 |
if !matches!( |
| 691 |
&*process::calling_process(), |
| 692 |
process::CallingProcess::GitGrep(_) | process::CallingProcess::OtherGrep |
| 693 |
) { |
| 694 |
return None; |
| 695 |
} |
| 696 |
_parse_grep_line(&GREP_LINE_REGEX_ASSUMING_COLOR, raw_line).map(|mut grep_line| { |
| 697 |
grep_line.code = ansi::strip_ansi_codes(&grep_line.code).into(); |
| 698 |
grep_line |
| 699 |
}) |
| 700 |
} |
| 701 |
|
| 702 |
pub fn _parse_grep_line<'b>(regex: &Regex, line: &'b str) -> Option<GrepLine<'b>> { |
| 703 |
let caps = regex.captures(line)?; |
| 704 |
let file = caps.get(1).unwrap().as_str().into(); |
| 705 |
let (line_type, line_number) = &[ |
| 706 |
(2, LineType::Match), |
| 707 |
(4, LineType::Context), |
| 708 |
(6, LineType::ContextHeader), |
| 709 |
] |
| 710 |
.iter() |
| 711 |
.find_map(|(i, line_type)| { |
| 712 |
if caps.get(*i).is_some() { |
| 713 |
let line_number: Option<usize> = caps.get(i + 1).and_then(|m| m.as_str().parse().ok()); |
| 714 |
Some((*line_type, line_number)) |
| 715 |
} else { |
| 716 |
None |
| 717 |
} |
| 718 |
}) |
| 719 |
.unwrap(); // The regex matches so one of the three alternatives must have matched |
| 720 |
let code = caps.get(8).unwrap().as_str().into(); |
| 721 |
|
| 722 |
Some(GrepLine { |
| 723 |
grep_type: GrepType::Classic, |
| 724 |
path: file, |
| 725 |
line_number: *line_number, |
| 726 |
line_type: *line_type, |
| 727 |
code, |
| 728 |
submatches: None, |
| 729 |
}) |
| 730 |
} |
| 731 |
|
| 732 |
#[cfg(test)] |
| 733 |
mod tests { |
| 734 |
use crate::handlers::grep::{ |
| 735 |
parse_grep_line, parse_raw_grep_line, GrepLine, GrepType, LineType, |
| 736 |
}; |
| 737 |
use crate::utils::process::tests::FakeParentArgs; |
| 738 |
|
| 739 |
#[test] |
| 740 |
fn test_parse_grep_match() { |
| 741 |
let fake_parent_grep_command = "git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 742 |
let _args = FakeParentArgs::for_scope(fake_parent_grep_command); |
| 743 |
|
| 744 |
assert_eq!( |
| 745 |
parse_grep_line("src/co-7-fig.rs:xxx"), |
| 746 |
Some(GrepLine { |
| 747 |
grep_type: GrepType::Classic, |
| 748 |
path: "src/co-7-fig.rs".into(), |
| 749 |
line_number: None, |
| 750 |
line_type: LineType::Match, |
| 751 |
code: "xxx".into(), |
| 752 |
submatches: None, |
| 753 |
}) |
| 754 |
); |
| 755 |
|
| 756 |
assert_eq!( |
| 757 |
parse_grep_line("src/config.rs:use crate::minusplus::MinusPlus;"), |
| 758 |
Some(GrepLine { |
| 759 |
grep_type: GrepType::Classic, |
| 760 |
path: "src/config.rs".into(), |
| 761 |
line_number: None, |
| 762 |
line_type: LineType::Match, |
| 763 |
code: "use crate::minusplus::MinusPlus;".into(), |
| 764 |
submatches: None, |
| 765 |
}) |
| 766 |
); |
| 767 |
|
| 768 |
assert_eq!( |
| 769 |
parse_grep_line( |
| 770 |
"src/config.rs: pub line_numbers_style_minusplus: MinusPlus<Style>," |
| 771 |
), |
| 772 |
Some(GrepLine { |
| 773 |
grep_type: GrepType::Classic, |
| 774 |
path: "src/config.rs".into(), |
| 775 |
line_number: None, |
| 776 |
line_type: LineType::Match, |
| 777 |
code: " pub line_numbers_style_minusplus: MinusPlus<Style>,".into(), |
| 778 |
submatches: None, |
| 779 |
}) |
| 780 |
); |
| 781 |
|
| 782 |
assert_eq!( |
| 783 |
parse_grep_line("src/con-fig.rs:use crate::minusplus::MinusPlus;"), |
| 784 |
Some(GrepLine { |
| 785 |
grep_type: GrepType::Classic, |
| 786 |
path: "src/con-fig.rs".into(), |
| 787 |
line_number: None, |
| 788 |
line_type: LineType::Match, |
| 789 |
code: "use crate::minusplus::MinusPlus;".into(), |
| 790 |
submatches: None, |
| 791 |
}) |
| 792 |
); |
| 793 |
|
| 794 |
assert_eq!( |
| 795 |
parse_grep_line( |
| 796 |
"src/con-fig.rs: pub line_numbers_style_minusplus: MinusPlus<Style>," |
| 797 |
), |
| 798 |
Some(GrepLine { |
| 799 |
grep_type: GrepType::Classic, |
| 800 |
path: "src/con-fig.rs".into(), |
| 801 |
line_number: None, |
| 802 |
line_type: LineType::Match, |
| 803 |
code: " pub line_numbers_style_minusplus: MinusPlus<Style>,".into(), |
| 804 |
submatches: None, |
| 805 |
}) |
| 806 |
); |
| 807 |
|
| 808 |
assert_eq!( |
| 809 |
parse_grep_line( |
| 810 |
"src/de lta.rs:pub fn delta<I>(lines: ByteLines<I>, writer: &mut dyn Write, config: &Config) -> std::io::Result<()>" |
| 811 |
), |
| 812 |
Some(GrepLine { |
| 813 |
grep_type: GrepType::Classic, |
| 814 |
path: "src/de lta.rs".into(), |
| 815 |
line_number: None, |
| 816 |
line_type: LineType::Match, |
| 817 |
code: "pub fn delta<I>(lines: ByteLines<I>, writer: &mut dyn Write, config: &Config) -> std::io::Result<()>".into(), |
| 818 |
submatches: None, |
| 819 |
}) |
| 820 |
); |
| 821 |
|
| 822 |
assert_eq!( |
| 823 |
parse_grep_line( |
| 824 |
"src/de lta.rs: pub fn new(writer: &'a mut dyn Write, config: &'a Config) -> Self {" |
| 825 |
), |
| 826 |
Some(GrepLine { |
| 827 |
grep_type: GrepType::Classic, |
| 828 |
path: "src/de lta.rs".into(), |
| 829 |
line_number: None, |
| 830 |
line_type: LineType::Match, |
| 831 |
code: " pub fn new(writer: &'a mut dyn Write, config: &'a Config) -> Self {".into(), |
| 832 |
submatches: None, |
| 833 |
}) |
| 834 |
); |
| 835 |
} |
| 836 |
|
| 837 |
#[test] |
| 838 |
fn test_parse_grep_n_match() { |
| 839 |
let fake_parent_grep_command = |
| 840 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 841 |
let _args = FakeParentArgs::for_scope(fake_parent_grep_command); |
| 842 |
|
| 843 |
assert_eq!( |
| 844 |
parse_grep_line("src/co-7-fig.rs:7:xxx"), |
| 845 |
Some(GrepLine { |
| 846 |
grep_type: GrepType::Classic, |
| 847 |
path: "src/co-7-fig.rs".into(), |
| 848 |
line_number: Some(7), |
| 849 |
line_type: LineType::Match, |
| 850 |
code: "xxx".into(), |
| 851 |
submatches: None, |
| 852 |
}) |
| 853 |
); |
| 854 |
|
| 855 |
assert_eq!( |
| 856 |
parse_grep_line("src/config.rs:21:use crate::minusplus::MinusPlus;"), |
| 857 |
Some(GrepLine { |
| 858 |
grep_type: GrepType::Classic, |
| 859 |
path: "src/config.rs".into(), |
| 860 |
line_number: Some(21), |
| 861 |
line_type: LineType::Match, |
| 862 |
code: "use crate::minusplus::MinusPlus;".into(), |
| 863 |
submatches: None, |
| 864 |
}) |
| 865 |
); |
| 866 |
|
| 867 |
assert_eq!( |
| 868 |
parse_grep_line( |
| 869 |
"src/config.rs:95: pub line_numbers_style_minusplus: MinusPlus<Style>," |
| 870 |
), |
| 871 |
Some(GrepLine { |
| 872 |
grep_type: GrepType::Classic, |
| 873 |
path: "src/config.rs".into(), |
| 874 |
line_number: Some(95), |
| 875 |
line_type: LineType::Match, |
| 876 |
code: " pub line_numbers_style_minusplus: MinusPlus<Style>,".into(), |
| 877 |
submatches: None, |
| 878 |
}) |
| 879 |
); |
| 880 |
|
| 881 |
assert_eq!( |
| 882 |
parse_grep_line("Makefile:10:test: unit-test end-to-end-test"), |
| 883 |
Some(GrepLine { |
| 884 |
grep_type: GrepType::Classic, |
| 885 |
path: "Makefile".into(), |
| 886 |
line_number: Some(10), |
| 887 |
line_type: LineType::Match, |
| 888 |
code: "test: unit-test end-to-end-test".into(), |
| 889 |
submatches: None, |
| 890 |
}) |
| 891 |
); |
| 892 |
|
| 893 |
assert_eq!( |
| 894 |
parse_grep_line( |
| 895 |
"Makefile:16: ./tests/test_raw_output_matches_git_on_full_repo_history" |
| 896 |
), |
| 897 |
Some(GrepLine { |
| 898 |
grep_type: GrepType::Classic, |
| 899 |
path: "Makefile".into(), |
| 900 |
line_number: Some(16), |
| 901 |
line_type: LineType::Match, |
| 902 |
code: " ./tests/test_raw_output_matches_git_on_full_repo_history".into(), |
| 903 |
submatches: None, |
| 904 |
}) |
| 905 |
); |
| 906 |
} |
| 907 |
|
| 908 |
#[test] |
| 909 |
#[ignore] |
| 910 |
fn test_parse_grep_n_match_file_name_with_dashes_and_no_extension() { |
| 911 |
// git grep -n |
| 912 |
// This fails: we can't parse it currently. |
| 913 |
let fake_parent_grep_command = |
| 914 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 915 |
let _args = FakeParentArgs::once(fake_parent_grep_command); |
| 916 |
|
| 917 |
assert_eq!( |
| 918 |
parse_grep_line("etc/examples/119-within-line-edits:4:repo=$(mktemp -d)"), |
| 919 |
Some(GrepLine { |
| 920 |
grep_type: GrepType::Classic, |
| 921 |
path: "etc/examples/119-within-line-edits".into(), |
| 922 |
line_number: Some(4), |
| 923 |
line_type: LineType::Match, |
| 924 |
code: "repo=$(mktemp -d)".into(), |
| 925 |
submatches: None, |
| 926 |
}) |
| 927 |
); |
| 928 |
} |
| 929 |
|
| 930 |
#[test] |
| 931 |
fn test_parse_grep_n_match_directory_name_with_dashes() { |
| 932 |
let fake_parent_grep_command = |
| 933 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 934 |
let _args = FakeParentArgs::once(fake_parent_grep_command); |
| 935 |
|
| 936 |
assert_eq!( |
| 937 |
parse_grep_line("etc/META-INF/foo.properties:4:value=hi-there"), |
| 938 |
Some(GrepLine { |
| 939 |
grep_type: GrepType::Classic, |
| 940 |
path: "etc/META-INF/foo.properties".into(), |
| 941 |
line_number: Some(4), |
| 942 |
line_type: LineType::Match, |
| 943 |
code: "value=hi-there".into(), |
| 944 |
submatches: None, |
| 945 |
}) |
| 946 |
); |
| 947 |
} |
| 948 |
|
| 949 |
#[test] |
| 950 |
fn test_parse_grep_color() { |
| 951 |
let fake_parent_grep_command = |
| 952 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 953 |
let _args = FakeParentArgs::for_scope(fake_parent_grep_command); |
| 954 |
|
| 955 |
let e = "\x1B"; |
| 956 |
assert_eq!( |
| 957 |
parse_raw_grep_line(&format!( |
| 958 |
r#"{e}[35msrc/zlib-ng/configure{e}[m{e}[36m:{e}[m -a*=* | -{e}[1;32m-arch{e}[ms=*) ARCHS=$(echo $1 | sed 's/.*=//'); shift ;;"# |
| 959 |
)), |
| 960 |
Some(GrepLine { |
| 961 |
grep_type: GrepType::Classic, |
| 962 |
path: "src/zlib-ng/configure".into(), |
| 963 |
line_number: None, |
| 964 |
line_type: LineType::Match, |
| 965 |
code: r#" -a*=* | --archs=*) ARCHS=$(echo $1 | sed 's/.*=//'); shift ;;"#.into(), |
| 966 |
submatches: None, |
| 967 |
}) |
| 968 |
); |
| 969 |
|
| 970 |
assert_eq!( |
| 971 |
parse_raw_grep_line(&format!( |
| 972 |
r#"{e}[35msrc/zlib-ng/configure{e}[m{e}[36m:{e}[m{e}[32m214{e}[m{e}[36m:{e}[m -a*=* | -{e}[1;32m-arch{e}[ms=*) ARCHS=$(echo $1 | sed 's/.*=//'); shift ;;"# |
| 973 |
)), |
| 974 |
Some(GrepLine { |
| 975 |
grep_type: GrepType::Classic, |
| 976 |
path: "src/zlib-ng/configure".into(), |
| 977 |
line_number: Some(214), |
| 978 |
line_type: LineType::Match, |
| 979 |
code: r#" -a*=* | --archs=*) ARCHS=$(echo $1 | sed 's/.*=//'); shift ;;"#.into(), |
| 980 |
submatches: None, |
| 981 |
}) |
| 982 |
); |
| 983 |
} |
| 984 |
|
| 985 |
#[test] |
| 986 |
fn test_parse_grep_no_match() { |
| 987 |
let fake_parent_grep_command = |
| 988 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 989 |
let _args = FakeParentArgs::for_scope(fake_parent_grep_command); |
| 990 |
|
| 991 |
assert_eq!( |
| 992 |
parse_grep_line("src/co-7-fig.rs-xxx"), |
| 993 |
Some(GrepLine { |
| 994 |
grep_type: GrepType::Classic, |
| 995 |
path: "src/co-7-fig.rs".into(), |
| 996 |
line_number: None, |
| 997 |
line_type: LineType::Context, |
| 998 |
code: "xxx".into(), |
| 999 |
submatches: None, |
| 1000 |
}) |
| 1001 |
); |
| 1002 |
|
| 1003 |
assert_eq!( |
| 1004 |
parse_grep_line("src/config.rs- pub available_terminal_width: usize,"), |
| 1005 |
Some(GrepLine { |
| 1006 |
grep_type: GrepType::Classic, |
| 1007 |
path: "src/config.rs".into(), |
| 1008 |
line_number: None, |
| 1009 |
line_type: LineType::Context, |
| 1010 |
code: " pub available_terminal_width: usize,".into(), |
| 1011 |
submatches: None, |
| 1012 |
}) |
| 1013 |
); |
| 1014 |
|
| 1015 |
assert_eq!( |
| 1016 |
parse_grep_line("src/con-fig.rs-use crate::minusplus::MinusPlus;"), |
| 1017 |
Some(GrepLine { |
| 1018 |
grep_type: GrepType::Classic, |
| 1019 |
path: "src/con-fig.rs".into(), |
| 1020 |
line_number: None, |
| 1021 |
line_type: LineType::Context, |
| 1022 |
code: "use crate::minusplus::MinusPlus;".into(), |
| 1023 |
submatches: None, |
| 1024 |
}) |
| 1025 |
); |
| 1026 |
|
| 1027 |
assert_eq!( |
| 1028 |
parse_grep_line("de-lta.rs- if self.source == Source::Unknown {"), |
| 1029 |
Some(GrepLine { |
| 1030 |
grep_type: GrepType::Classic, |
| 1031 |
path: "de-lta.rs".into(), |
| 1032 |
line_number: None, |
| 1033 |
line_type: LineType::Context, |
| 1034 |
code: " if self.source == Source::Unknown {".into(), |
| 1035 |
submatches: None, |
| 1036 |
}) |
| 1037 |
); |
| 1038 |
|
| 1039 |
assert_eq!( |
| 1040 |
parse_grep_line(r#"aaa/bbb.scala- s"xxx.yyy.zzz: $ccc ddd""#), |
| 1041 |
Some(GrepLine { |
| 1042 |
grep_type: GrepType::Classic, |
| 1043 |
path: "aaa/bbb.scala".into(), |
| 1044 |
line_number: None, |
| 1045 |
line_type: LineType::Context, |
| 1046 |
code: r#" s"xxx.yyy.zzz: $ccc ddd""#.into(), |
| 1047 |
submatches: None, |
| 1048 |
}) |
| 1049 |
); |
| 1050 |
|
| 1051 |
assert_eq!( |
| 1052 |
parse_grep_line(r#"aaa/bbb.scala- val atRegex = Regex.compile("(@.*)|(-shdw@.*)""#), |
| 1053 |
Some(GrepLine { |
| 1054 |
grep_type: GrepType::Classic, |
| 1055 |
path: "aaa/bbb.scala".into(), |
| 1056 |
line_number: None, |
| 1057 |
line_type: LineType::Context, |
| 1058 |
code: r#" val atRegex = Regex.compile("(@.*)|(-shdw@.*)""#.into(), |
| 1059 |
submatches: None, |
| 1060 |
}) |
| 1061 |
); |
| 1062 |
} |
| 1063 |
|
| 1064 |
#[test] |
| 1065 |
fn test_parse_grep_n_no_match() { |
| 1066 |
// git grep -n |
| 1067 |
let fake_parent_grep_command = |
| 1068 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 1069 |
|
| 1070 |
let _args = FakeParentArgs::for_scope(fake_parent_grep_command); |
| 1071 |
assert_eq!( |
| 1072 |
parse_grep_line("src/co-7-fig.rs-7-xxx"), |
| 1073 |
Some(GrepLine { |
| 1074 |
grep_type: GrepType::Classic, |
| 1075 |
path: "src/co-7-fig.rs".into(), |
| 1076 |
line_number: Some(7), |
| 1077 |
line_type: LineType::Context, |
| 1078 |
code: "xxx".into(), |
| 1079 |
submatches: None, |
| 1080 |
}) |
| 1081 |
); |
| 1082 |
|
| 1083 |
assert_eq!( |
| 1084 |
parse_grep_line("src/config.rs-58- pub available_terminal_width: usize,"), |
| 1085 |
Some(GrepLine { |
| 1086 |
grep_type: GrepType::Classic, |
| 1087 |
path: "src/config.rs".into(), |
| 1088 |
line_number: Some(58), |
| 1089 |
line_type: LineType::Context, |
| 1090 |
code: " pub available_terminal_width: usize,".into(), |
| 1091 |
submatches: None, |
| 1092 |
}) |
| 1093 |
); |
| 1094 |
|
| 1095 |
assert_eq!( |
| 1096 |
parse_grep_line(r#"foo.rs-12- .x-"#), |
| 1097 |
Some(GrepLine { |
| 1098 |
grep_type: GrepType::Classic, |
| 1099 |
path: "foo.rs".into(), |
| 1100 |
line_number: Some(12), |
| 1101 |
line_type: LineType::Context, |
| 1102 |
code: r#" .x-"#.into(), |
| 1103 |
submatches: None, |
| 1104 |
}) |
| 1105 |
); |
| 1106 |
|
| 1107 |
assert_eq!( |
| 1108 |
parse_grep_line(r#"foo.rs-12-.x-"#), |
| 1109 |
Some(GrepLine { |
| 1110 |
grep_type: GrepType::Classic, |
| 1111 |
path: "foo.rs".into(), |
| 1112 |
line_number: Some(12), |
| 1113 |
line_type: LineType::Context, |
| 1114 |
code: r#".x-"#.into(), |
| 1115 |
submatches: None, |
| 1116 |
}) |
| 1117 |
); |
| 1118 |
} |
| 1119 |
|
| 1120 |
#[test] |
| 1121 |
fn test_parse_grep_match_no_extension() { |
| 1122 |
let fake_parent_grep_command = |
| 1123 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 1124 |
let _args = FakeParentArgs::once(fake_parent_grep_command); |
| 1125 |
|
| 1126 |
assert_eq!( |
| 1127 |
parse_grep_line("Makefile:xxx"), |
| 1128 |
Some(GrepLine { |
| 1129 |
grep_type: GrepType::Classic, |
| 1130 |
path: "Makefile".into(), |
| 1131 |
line_number: None, |
| 1132 |
line_type: LineType::Match, |
| 1133 |
code: "xxx".into(), |
| 1134 |
submatches: None, |
| 1135 |
}) |
| 1136 |
); |
| 1137 |
} |
| 1138 |
|
| 1139 |
#[test] |
| 1140 |
fn test_parse_grep_n_match_no_extension() { |
| 1141 |
// git grep -n |
| 1142 |
let fake_parent_grep_command = |
| 1143 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 1144 |
let _args = FakeParentArgs::once(fake_parent_grep_command); |
| 1145 |
assert_eq!( |
| 1146 |
parse_grep_line("Makefile:7:xxx"), |
| 1147 |
Some(GrepLine { |
| 1148 |
grep_type: GrepType::Classic, |
| 1149 |
path: "Makefile".into(), |
| 1150 |
line_number: Some(7), |
| 1151 |
line_type: LineType::Match, |
| 1152 |
code: "xxx".into(), |
| 1153 |
submatches: None, |
| 1154 |
}) |
| 1155 |
); |
| 1156 |
} |
| 1157 |
|
| 1158 |
#[test] |
| 1159 |
#[allow(non_snake_case)] |
| 1160 |
fn test_parse_grep_W_context_header() { |
| 1161 |
// git grep -W |
| 1162 |
let fake_parent_grep_command = |
| 1163 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 1164 |
|
| 1165 |
let _args = FakeParentArgs::once(fake_parent_grep_command); |
| 1166 |
assert_eq!( |
| 1167 |
parse_grep_line("src/config.rs=pub struct Config {"), // match |
| 1168 |
Some(GrepLine { |
| 1169 |
grep_type: GrepType::Classic, |
| 1170 |
path: "src/config.rs".into(), |
| 1171 |
line_number: None, |
| 1172 |
line_type: LineType::ContextHeader, |
| 1173 |
code: "pub struct Config {".into(), |
| 1174 |
submatches: None, |
| 1175 |
}) |
| 1176 |
); |
| 1177 |
} |
| 1178 |
|
| 1179 |
#[test] |
| 1180 |
#[allow(non_snake_case)] |
| 1181 |
fn test_parse_grep_W_n_context_header() { |
| 1182 |
// git grep -n -W |
| 1183 |
let fake_parent_grep_command = |
| 1184 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 1185 |
let _args = FakeParentArgs::once(fake_parent_grep_command); |
| 1186 |
|
| 1187 |
assert_eq!( |
| 1188 |
parse_grep_line("src/config.rs=57=pub struct Config {"), |
| 1189 |
Some(GrepLine { |
| 1190 |
grep_type: GrepType::Classic, |
| 1191 |
path: "src/config.rs".into(), |
| 1192 |
line_number: Some(57), |
| 1193 |
line_type: LineType::ContextHeader, |
| 1194 |
code: "pub struct Config {".into(), |
| 1195 |
submatches: None, |
| 1196 |
}) |
| 1197 |
); |
| 1198 |
} |
| 1199 |
|
| 1200 |
#[test] |
| 1201 |
fn test_parse_grep_not_grep_output() { |
| 1202 |
let fake_parent_grep_command = |
| 1203 |
"/usr/local/bin/git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 1204 |
let _args = FakeParentArgs::once(fake_parent_grep_command); |
| 1205 |
|
| 1206 |
let not_grep_output = "| expose it in delta's color output styled with grep:"; |
| 1207 |
assert_eq!(parse_grep_line(not_grep_output), None); |
| 1208 |
} |
| 1209 |
|
| 1210 |
#[test] |
| 1211 |
fn test_parse_grep_parent_command_is_not_grep_1() { |
| 1212 |
let fake_parent_grep_command = |
| 1213 |
"/usr/local/bin/notgrep --doesnt-matter --nor-this nor_this -- nor_this"; |
| 1214 |
let _args = FakeParentArgs::once(fake_parent_grep_command); |
| 1215 |
|
| 1216 |
let apparently_grep_output = "src/co-7-fig.rs:xxx"; |
| 1217 |
assert_eq!(parse_grep_line(apparently_grep_output), None); |
| 1218 |
} |
| 1219 |
|
| 1220 |
#[test] |
| 1221 |
fn test_parse_grep_parent_command_is_not_grep_2() { |
| 1222 |
// No fake parent grep command |
| 1223 |
let apparently_grep_output = "src/co-7-fig.rs:xxx"; |
| 1224 |
assert_eq!(parse_grep_line(apparently_grep_output), None); |
| 1225 |
} |
| 1226 |
|
| 1227 |
#[test] |
| 1228 |
fn test_get_code_style_sections() { |
| 1229 |
use crate::ansi::strip_ansi_codes; |
| 1230 |
use crate::handlers::grep::get_code_style_sections; |
| 1231 |
use crate::paint::StyleSectionSpecifier; |
| 1232 |
use crate::style::Style; |
| 1233 |
|
| 1234 |
let fake_parent_grep_command = "git --doesnt-matter grep --nor-this nor_this -- nor_this"; |
| 1235 |
let _args = FakeParentArgs::for_scope(fake_parent_grep_command); |
| 1236 |
|
| 1237 |
let miss = Style::new(); |
| 1238 |
let hit = Style { |
| 1239 |
is_emph: true, |
| 1240 |
..miss |
| 1241 |
}; |
| 1242 |
|
| 1243 |
let escape = "\x1B"; |
| 1244 |
let working_example = format!("foo/bar/baz.yaml{escape}[36m:{escape}[m1090{escape}[36m:{escape}[m - {escape}[1;31mkind: Service{escape}[mAccount"); |
| 1245 |
let stripped = strip_ansi_codes(&working_example); |
| 1246 |
let grep = parse_grep_line(&stripped).unwrap(); |
| 1247 |
|
| 1248 |
assert_eq!( |
| 1249 |
get_code_style_sections(&working_example, hit, miss, &grep.path, grep.line_number), |
| 1250 |
Some(StyleSectionSpecifier::StyleSections(vec![ |
| 1251 |
(miss, " - "), |
| 1252 |
(hit, "kind: Service"), |
| 1253 |
(miss, "Account") |
| 1254 |
])) |
| 1255 |
); |
| 1256 |
|
| 1257 |
let broken_example = format!("foo/bar/baz.yaml{escape}[36m:{escape}[m2{escape}[36m:{escape}[m{escape}[1;31mkind: Service{escape}[m"); |
| 1258 |
let broken_stripped = strip_ansi_codes(&broken_example); |
| 1259 |
let broken_grep = parse_grep_line(&broken_stripped).unwrap(); |
| 1260 |
|
| 1261 |
assert_eq!( |
| 1262 |
get_code_style_sections( |
| 1263 |
&broken_example, |
| 1264 |
hit, |
| 1265 |
miss, |
| 1266 |
&broken_grep.path, |
| 1267 |
broken_grep.line_number |
| 1268 |
), |
| 1269 |
Some(StyleSectionSpecifier::StyleSections(vec![( |
| 1270 |
hit, |
| 1271 |
"kind: Service" |
| 1272 |
)])) |
| 1273 |
); |
| 1274 |
|
| 1275 |
let plus_example = format!("etc/examples/189-merge-conflict.2.diff{escape}[36m:{escape}[m10{escape}[36m:{escape}[m{escape}[32m + let (style, non_emph_style) = {escape}[1;31mmatch{escape}[m state {{{escape}[m"); |
| 1276 |
let plus_stripped = strip_ansi_codes(&plus_example); |
| 1277 |
let plus_grep = parse_grep_line(&plus_stripped).unwrap(); |
| 1278 |
|
| 1279 |
assert_eq!( |
| 1280 |
get_code_style_sections( |
| 1281 |
&plus_example, |
| 1282 |
hit, |
| 1283 |
miss, |
| 1284 |
&plus_grep.path, |
| 1285 |
plus_grep.line_number |
| 1286 |
), |
| 1287 |
Some(StyleSectionSpecifier::StyleSections(vec![ |
| 1288 |
(miss, " + let (style, non_emph_style) = "), |
| 1289 |
(hit, "match"), |
| 1290 |
(miss, " state {") |
| 1291 |
])) |
| 1292 |
); |
| 1293 |
} |
| 1294 |
} |