| 1 | // See https://github.com/BurntSushi/ripgrep |
| 2 | // This module implements handling of `rg --json` output. It is called by the |
| 3 | // handler in handlers/grep.rs. Normal rg output (i.e. without --json) is |
| 4 | // handled by the same code paths as `git grep` etc output, in handlers/grep.rs. |
| 5 | use std::borrow::Cow; |
| 6 | |
| 7 | use crate::handlers::grep; |
| 8 | use serde::Deserialize; |
| 9 | use serde_json::Value; |
| 10 | |
| 11 | pub fn parse_line(line: &str) -> Option<grep::GrepLine<'_>> { |
| 12 | let ripgrep_line: Option<RipGrepLine> = serde_json::from_str(line).ok(); |
| 13 | match ripgrep_line { |
| 14 | Some(ripgrep_line) => { |
| 15 | // A real line of rg --json output, i.e. either of type "match" or |
| 16 | // "context". |
| 17 | let mut code = ripgrep_line.data.lines.text; |
| 18 | // Keep newlines so the syntax highlighter handles C-style line comments |
| 19 | // correctly. Also remove \r, see [EndCRLF] in src/delta.rs, but this time |
| 20 | // it is syntect which adds an ANSI escape sequence in between \r\n later. |
| 21 | if code.ends_with("\r\n") { |
| 22 | code.truncate(code.len() - 2); |
| 23 | code.push('\n'); |
| 24 | } |
| 25 | Some(grep::GrepLine { |
| 26 | grep_type: crate::config::GrepType::Ripgrep, |
| 27 | line_type: ripgrep_line._type, |
| 28 | line_number: ripgrep_line.data.line_number, |
| 29 | path: Cow::from(ripgrep_line.data.path.text), |
| 30 | code: Cow::from(code), |
| 31 | submatches: Some( |
| 32 | ripgrep_line |
| 33 | .data |
| 34 | .submatches |
| 35 | .iter() |
| 36 | .map(|m| (m.start, m.end)) |
| 37 | .collect(), |
| 38 | ), |
| 39 | }) |
| 40 | } |
| 41 | None => { |
| 42 | let value: Value = serde_json::from_str(line).ok()?; |
| 43 | match &value["type"] { |
| 44 | Value::String(s) if s == "begin" || s == "end" || s == "summary" => { |
| 45 | Some(grep::GrepLine { |
| 46 | // ripgrep --json also emits these metadata lines at |
| 47 | // file boundaries. We emit nothing but signal that the |
| 48 | // line has been handled. |
| 49 | grep_type: crate::config::GrepType::Ripgrep, |
| 50 | line_type: grep::LineType::Ignore, |
| 51 | line_number: None, |
| 52 | path: "".into(), |
| 53 | code: "".into(), |
| 54 | submatches: None, |
| 55 | }) |
| 56 | } |
| 57 | _ => { |
| 58 | // Failed to interpret the line as ripgrep output; allow |
| 59 | // another delta handler to try. |
| 60 | None |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // { |
| 68 | // "type": "match", |
| 69 | // "data": { |
| 70 | // "path": { |
| 71 | // "text": "src/cli.rs" |
| 72 | // }, |
| 73 | // "lines": { |
| 74 | // "text": " fn from_clap_and_git_config(\n" |
| 75 | // }, |
| 76 | // "line_number": null, |
| 77 | // "absolute_offset": 35837, |
| 78 | // "submatches": [ |
| 79 | // { |
| 80 | // "match": { |
| 81 | // "text": "fn" |
| 82 | // }, |
| 83 | // "start": 4, |
| 84 | // "end": 6 |
| 85 | // } |
| 86 | // ] |
| 87 | // } |
| 88 | // } |
| 89 | |
| 90 | #[derive(Deserialize, PartialEq, Debug)] |
| 91 | struct RipGrepLine { |
| 92 | #[serde(rename(deserialize = "type"))] |
| 93 | _type: grep::LineType, |
| 94 | data: RipGrepLineData, |
| 95 | } |
| 96 | |
| 97 | #[derive(Deserialize, PartialEq, Debug)] |
| 98 | struct RipGrepLineData { |
| 99 | path: RipGrepLineText, |
| 100 | lines: RipGrepLineText, |
| 101 | line_number: Option<usize>, |
| 102 | absolute_offset: usize, |
| 103 | submatches: Vec<RipGrepLineSubmatch>, |
| 104 | } |
| 105 | |
| 106 | #[derive(Deserialize, PartialEq, Debug)] |
| 107 | struct RipGrepLineText { |
| 108 | text: String, |
| 109 | } |
| 110 | |
| 111 | #[derive(Deserialize, PartialEq, Debug)] |
| 112 | struct RipGrepLineSubmatch { |
| 113 | #[serde(rename(deserialize = "match"))] |
| 114 | _match: RipGrepLineText, |
| 115 | start: usize, |
| 116 | end: usize, |
| 117 | } |
| 118 | |
| 119 | #[cfg(test)] |
| 120 | mod tests { |
| 121 | use super::*; |
| 122 | |
| 123 | use crate::tests::integration_test_utils::DeltaTest; |
| 124 | use insta::assert_snapshot; |
| 125 | |
| 126 | /* FILE test.c: |
| 127 | // i ABC |
| 128 | int f() { return 4; } |
| 129 | const char* i = "ABC"; |
| 130 | double n = 1.23; |
| 131 | */ |
| 132 | #[test] |
| 133 | fn test_syntax_in_rg_output_with_context() { |
| 134 | // `rg int -C2 --json test.c` |
| 135 | let data = r#"{"type":"begin","data":{"path":{"text":"test.c"}}} |
| 136 | {"type":"context","data":{"path":{"text":"test.c"},"lines":{"text":"// i ABC\n"},"line_number":1,"absolute_offset":0,"submatches":[]}} |
| 137 | {"type":"match","data":{"path":{"text":"test.c"},"lines":{"text":"int f() { return 4; }\n"},"line_number":2,"absolute_offset":9,"submatches":[{"match":{"text":"int"},"start":0,"end":3}]}} |
| 138 | {"type":"context","data":{"path":{"text":"test.c"},"lines":{"text":"const char* i = \"ABC\";\n"},"line_number":3,"absolute_offset":31,"submatches":[]}} |
| 139 | {"type":"context","data":{"path":{"text":"test.c"},"lines":{"text":"double n = 1.23;\n"},"line_number":4,"absolute_offset":54,"submatches":[]}} |
| 140 | {"type":"end","data":{"path":{"text":"test.c"},"binary_offset":null,"stats":{"elapsed":{"secs":0,"nanos":26941,"human":"0.000027s"},"searches":1,"searches_with_match":1,"bytes_searched":71,"bytes_printed":670,"matched_lines":1,"matches":1}}} |
| 141 | {"data":{"elapsed_total":{"human":"0.000479s","nanos":478729,"secs":0},"stats":{"bytes_printed":670,"bytes_searched":71,"elapsed":{"human":"0.000027s","nanos":26941,"secs":0},"matched_lines":1,"matches":1,"searches":1,"searches_with_match":1}},"type":"summary"}"#; |
| 142 | let result = DeltaTest::with_args(&[]).explain_ansi().with_input(data); |
| 143 | // eprintln!("{}", result.raw_output); |
| 144 | assert_snapshot!(result.output, @r#" |
| 145 | (purple)test.c(normal) |
| 146 | (green)1(normal)-(242)// i ABC(normal) |
| 147 | (green)2(normal):(81 28)int(231) (149)f(231)() { (203)return(231) (141)4(231); }(normal) |
| 148 | (green)3(normal)-(203)const(231) (81)char(203)*(231) i (203)=(231) (186)"ABC"(231);(normal) |
| 149 | (green)4(normal)-(81)double(231) n (203)=(231) (141)1.23(231);(normal) |
| 150 | "#); |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn test_syntax_in_rg_output_no_context() { |
| 155 | // `rg i --json test.c` |
| 156 | let data = r#"{"type":"begin","data":{"path":{"text":"test.c"}}} |
| 157 | {"type":"match","data":{"path":{"text":"test.c"},"lines":{"text":"// i ABC\n"},"line_number":1,"absolute_offset":0,"submatches":[{"match":{"text":"i"},"start":3,"end":4}]}} |
| 158 | {"type":"match","data":{"path":{"text":"test.c"},"lines":{"text":"int f() { return 4; }\n"},"line_number":2,"absolute_offset":9,"submatches":[{"match":{"text":"i"},"start":0,"end":1}]}} |
| 159 | {"type":"match","data":{"path":{"text":"test.c"},"lines":{"text":"const char* i = \"ABC\";\n"},"line_number":3,"absolute_offset":31,"submatches":[{"match":{"text":"i"},"start":12,"end":13}]}} |
| 160 | {"type":"end","data":{"path":{"text":"test.c"},"binary_offset":null,"stats":{"elapsed":{"secs":0,"nanos":23885,"human":"0.000024s"},"searches":1,"searches_with_match":1,"bytes_searched":71,"bytes_printed":602,"matched_lines":3,"matches":3}}} |
| 161 | {"data":{"elapsed_total":{"human":"0.000433s","nanos":432974,"secs":0},"stats":{"bytes_printed":602,"bytes_searched":71,"elapsed":{"human":"0.000024s","nanos":23885,"secs":0},"matched_lines":3,"matches":3,"searches":1,"searches_with_match":1}},"type":"summary"} |
| 162 | "#; |
| 163 | let result = DeltaTest::with_args(&[]).explain_ansi().with_input(data); |
| 164 | // eprintln!("{}", result.raw_output); |
| 165 | assert_snapshot!(result.output, @r#" |
| 166 | (purple)test.c(normal) |
| 167 | (green)1(normal):(242)// (normal 28)i(242) ABC(normal) |
| 168 | (green)2(normal):(81 28)i(81)nt(231) (149)f(231)() { (203)return(231) (141)4(231); }(normal) |
| 169 | (green)3(normal):(203)const(231) (81)char(203)*(231) (normal 28)i(231) (203)=(231) (186)"ABC"(231);(normal) |
| 170 | "#); |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn test_deserialize() { |
| 175 | let line = r#"{"type":"match","data":{"path":{"text":"src/cli.rs"},"lines":{"text":" fn from_clap_and_git_config(\n"},"line_number":null,"absolute_offset":35837,"submatches":[{"match":{"text":"fn"},"start":4,"end":6}]}}"#; |
| 176 | let ripgrep_line: RipGrepLine = serde_json::from_str(line).unwrap(); |
| 177 | assert_eq!( |
| 178 | ripgrep_line, |
| 179 | RipGrepLine { |
| 180 | _type: grep::LineType::Match, |
| 181 | data: RipGrepLineData { |
| 182 | path: RipGrepLineText { |
| 183 | text: "src/cli.rs".into() |
| 184 | }, |
| 185 | lines: RipGrepLineText { |
| 186 | text: " fn from_clap_and_git_config(\n".into(), |
| 187 | }, |
| 188 | line_number: None, |
| 189 | absolute_offset: 35837, |
| 190 | submatches: vec![RipGrepLineSubmatch { |
| 191 | _match: RipGrepLineText { text: "fn".into() }, |
| 192 | start: 4, |
| 193 | end: 6 |
| 194 | }] |
| 195 | } |
| 196 | } |
| 197 | ) |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn test_deserialize_2() { |
| 202 | let line = r#"{"type":"match","data":{"path":{"text":"src/handlers/submodule.rs"},"lines":{"text":" .paint(minus_commit.chars().take(7).collect::<String>()),\n"},"line_number":41,"absolute_offset":1430,"submatches":[{"match":{"text":"("},"start":30,"end":31},{"match":{"text":"("},"start":49,"end":50},{"match":{"text":")"},"start":50,"end":51},{"match":{"text":"("},"start":56,"end":57},{"match":{"text":")"},"start":58,"end":59},{"match":{"text":"("},"start":77,"end":78},{"match":{"text":")"},"start":78,"end":79},{"match":{"text":")"},"start":79,"end":80}]}}"#; |
| 203 | let ripgrep_line: RipGrepLine = serde_json::from_str(line).unwrap(); |
| 204 | assert_eq!( |
| 205 | ripgrep_line, |
| 206 | RipGrepLine { |
| 207 | _type: grep::LineType::Match, |
| 208 | data: RipGrepLineData { |
| 209 | path: RipGrepLineText { |
| 210 | text: "src/handlers/submodule.rs".into() |
| 211 | }, |
| 212 | lines: RipGrepLineText { |
| 213 | text: " .paint(minus_commit.chars().take(7).collect::<String>()),\n".into(), |
| 214 | }, |
| 215 | line_number: Some(41), |
| 216 | absolute_offset: 1430, |
| 217 | submatches: vec![ |
| 218 | RipGrepLineSubmatch { |
| 219 | _match: RipGrepLineText { text: "(".into() }, |
| 220 | start: 30, |
| 221 | end: 31 |
| 222 | }, |
| 223 | RipGrepLineSubmatch { |
| 224 | _match: RipGrepLineText { text: "(".into() }, |
| 225 | start: 49, |
| 226 | end: 50 |
| 227 | }, |
| 228 | RipGrepLineSubmatch { |
| 229 | _match: RipGrepLineText { text: ")".into() }, |
| 230 | start: 50, |
| 231 | end: 51 |
| 232 | }, |
| 233 | RipGrepLineSubmatch { |
| 234 | _match: RipGrepLineText { text: "(".into() }, |
| 235 | start: 56, |
| 236 | end: 57 |
| 237 | }, |
| 238 | RipGrepLineSubmatch { |
| 239 | _match: RipGrepLineText { text: ")".into() }, |
| 240 | start: 58, |
| 241 | end: 59 |
| 242 | }, |
| 243 | RipGrepLineSubmatch { |
| 244 | _match: RipGrepLineText { text: "(".into() }, |
| 245 | start: 77, |
| 246 | end: 78 |
| 247 | }, |
| 248 | RipGrepLineSubmatch { |
| 249 | _match: RipGrepLineText { text: ")".into() }, |
| 250 | start: 78, |
| 251 | end: 79 |
| 252 | }, |
| 253 | RipGrepLineSubmatch { |
| 254 | _match: RipGrepLineText { text: ")".into() }, |
| 255 | start: 79, |
| 256 | end: 80 |
| 257 | }, |
| 258 | ] |
| 259 | } |
| 260 | } |
| 261 | ) |
| 262 | } |
| 263 | } |