forked from rtk-ai/rtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.rs
More file actions
494 lines (440 loc) · 14.8 KB
/
Copy pathfilter.rs
File metadata and controls
494 lines (440 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use lazy_static::lazy_static;
use regex::Regex;
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilterLevel {
None,
Minimal,
Aggressive,
}
impl FromStr for FilterLevel {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"none" => Ok(FilterLevel::None),
"minimal" => Ok(FilterLevel::Minimal),
"aggressive" => Ok(FilterLevel::Aggressive),
_ => Err(format!("Unknown filter level: {}", s)),
}
}
}
impl std::fmt::Display for FilterLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FilterLevel::None => write!(f, "none"),
FilterLevel::Minimal => write!(f, "minimal"),
FilterLevel::Aggressive => write!(f, "aggressive"),
}
}
}
pub trait FilterStrategy {
fn filter(&self, content: &str, lang: &Language) -> String;
#[allow(dead_code)]
fn name(&self) -> &'static str;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Language {
Rust,
Python,
JavaScript,
TypeScript,
Go,
C,
Cpp,
Java,
Ruby,
Shell,
/// Data formats (JSON, YAML, TOML, XML, CSV) — no comment stripping
Data,
Unknown,
}
impl Language {
pub fn from_extension(ext: &str) -> Self {
match ext.to_lowercase().as_str() {
"rs" => Language::Rust,
"py" | "pyw" => Language::Python,
"js" | "mjs" | "cjs" => Language::JavaScript,
"ts" | "tsx" => Language::TypeScript,
"go" => Language::Go,
"c" | "h" => Language::C,
"cpp" | "cc" | "cxx" | "hpp" | "hh" => Language::Cpp,
"java" => Language::Java,
"rb" => Language::Ruby,
"sh" | "bash" | "zsh" => Language::Shell,
"json" | "jsonc" | "json5" | "yaml" | "yml" | "toml" | "xml" | "csv" | "tsv"
| "graphql" | "gql" | "sql" | "md" | "markdown" | "txt" | "env" | "lock" => {
Language::Data
}
_ => Language::Unknown,
}
}
pub fn comment_patterns(&self) -> CommentPatterns {
match self {
Language::Rust => CommentPatterns {
line: Some("//"),
block_start: Some("/*"),
block_end: Some("*/"),
doc_line: Some("///"),
doc_block_start: Some("/**"),
},
Language::Python => CommentPatterns {
line: Some("#"),
block_start: Some("\"\"\""),
block_end: Some("\"\"\""),
doc_line: None,
doc_block_start: Some("\"\"\""),
},
Language::JavaScript
| Language::TypeScript
| Language::Go
| Language::C
| Language::Cpp
| Language::Java => CommentPatterns {
line: Some("//"),
block_start: Some("/*"),
block_end: Some("*/"),
doc_line: None,
doc_block_start: Some("/**"),
},
Language::Ruby => CommentPatterns {
line: Some("#"),
block_start: Some("=begin"),
block_end: Some("=end"),
doc_line: None,
doc_block_start: None,
},
Language::Shell => CommentPatterns {
line: Some("#"),
block_start: None,
block_end: None,
doc_line: None,
doc_block_start: None,
},
Language::Data => CommentPatterns {
line: None,
block_start: None,
block_end: None,
doc_line: None,
doc_block_start: None,
},
Language::Unknown => CommentPatterns {
line: Some("//"),
block_start: Some("/*"),
block_end: Some("*/"),
doc_line: None,
doc_block_start: None,
},
}
}
}
#[derive(Debug, Clone)]
pub struct CommentPatterns {
pub line: Option<&'static str>,
pub block_start: Option<&'static str>,
pub block_end: Option<&'static str>,
pub doc_line: Option<&'static str>,
pub doc_block_start: Option<&'static str>,
}
pub struct NoFilter;
impl FilterStrategy for NoFilter {
fn filter(&self, content: &str, _lang: &Language) -> String {
content.to_string()
}
fn name(&self) -> &'static str {
"none"
}
}
pub struct MinimalFilter;
lazy_static! {
static ref MULTIPLE_BLANK_LINES: Regex = Regex::new(r"\n{3,}").unwrap();
static ref TRAILING_WHITESPACE: Regex = Regex::new(r"[ \t]+$").unwrap();
}
impl FilterStrategy for MinimalFilter {
fn filter(&self, content: &str, lang: &Language) -> String {
let patterns = lang.comment_patterns();
let mut result = String::with_capacity(content.len());
let mut in_block_comment = false;
let mut in_docstring = false;
for line in content.lines() {
let trimmed = line.trim();
// Handle block comments
if let (Some(start), Some(end)) = (patterns.block_start, patterns.block_end) {
if !in_docstring
&& trimmed.contains(start)
&& !trimmed.starts_with(patterns.doc_block_start.unwrap_or("###"))
{
in_block_comment = true;
}
if in_block_comment {
if trimmed.contains(end) {
in_block_comment = false;
}
continue;
}
}
// Handle Python docstrings (keep them in minimal mode)
if *lang == Language::Python && trimmed.starts_with("\"\"\"") {
in_docstring = !in_docstring;
result.push_str(line);
result.push('\n');
continue;
}
if in_docstring {
result.push_str(line);
result.push('\n');
continue;
}
// Skip single-line comments (but keep doc comments)
if let Some(line_comment) = patterns.line {
if trimmed.starts_with(line_comment) {
// Keep doc comments
if let Some(doc) = patterns.doc_line {
if trimmed.starts_with(doc) {
result.push_str(line);
result.push('\n');
}
}
continue;
}
}
// Skip empty lines at this point, we'll normalize later
if trimmed.is_empty() {
result.push('\n');
continue;
}
result.push_str(line);
result.push('\n');
}
// Normalize multiple blank lines to max 2
let result = MULTIPLE_BLANK_LINES.replace_all(&result, "\n\n");
result.trim().to_string()
}
fn name(&self) -> &'static str {
"minimal"
}
}
pub struct AggressiveFilter;
lazy_static! {
static ref IMPORT_PATTERN: Regex =
Regex::new(r"^(use |import |from |require\(|#include)").unwrap();
static ref FUNC_SIGNATURE: Regex = Regex::new(
r"^(pub\s+)?(async\s+)?(fn|def|function|func|class|struct|enum|trait|interface|type)\s+\w+"
)
.unwrap();
}
impl FilterStrategy for AggressiveFilter {
fn filter(&self, content: &str, lang: &Language) -> String {
// Data formats (JSON, YAML, etc.) must never be code-filtered
if *lang == Language::Data {
return MinimalFilter.filter(content, lang);
}
let minimal = MinimalFilter.filter(content, lang);
let mut result = String::with_capacity(minimal.len() / 2);
let mut brace_depth = 0;
let mut in_impl_body = false;
for line in minimal.lines() {
let trimmed = line.trim();
// Always keep imports
if IMPORT_PATTERN.is_match(trimmed) {
result.push_str(line);
result.push('\n');
continue;
}
// Always keep function/struct/class signatures
if FUNC_SIGNATURE.is_match(trimmed) {
result.push_str(line);
result.push('\n');
in_impl_body = true;
brace_depth = 0;
continue;
}
// Track brace depth for implementation bodies
let open_braces = trimmed.matches('{').count();
let close_braces = trimmed.matches('}').count();
if in_impl_body {
brace_depth += open_braces as i32;
brace_depth -= close_braces as i32;
// Only keep the opening and closing braces
if brace_depth <= 1 && (trimmed == "{" || trimmed == "}" || trimmed.ends_with('{'))
{
result.push_str(line);
result.push('\n');
}
if brace_depth <= 0 {
in_impl_body = false;
if !trimmed.is_empty() && trimmed != "}" {
result.push_str(" // ... implementation\n");
}
}
continue;
}
// Keep type definitions, constants, etc.
if trimmed.starts_with("const ")
|| trimmed.starts_with("static ")
|| trimmed.starts_with("let ")
|| trimmed.starts_with("pub const ")
|| trimmed.starts_with("pub static ")
{
result.push_str(line);
result.push('\n');
}
}
result.trim().to_string()
}
fn name(&self) -> &'static str {
"aggressive"
}
}
pub fn get_filter(level: FilterLevel) -> Box<dyn FilterStrategy> {
match level {
FilterLevel::None => Box::new(NoFilter),
FilterLevel::Minimal => Box::new(MinimalFilter),
FilterLevel::Aggressive => Box::new(AggressiveFilter),
}
}
pub fn smart_truncate(content: &str, max_lines: usize, _lang: &Language) -> String {
let lines: Vec<&str> = content.lines().collect();
if lines.len() <= max_lines {
return content.to_string();
}
let mut result = Vec::with_capacity(max_lines);
let mut kept_lines = 0;
let mut skipped_section = false;
for line in &lines {
let trimmed = line.trim();
// Always keep signatures and important structural elements
let is_important = FUNC_SIGNATURE.is_match(trimmed)
|| IMPORT_PATTERN.is_match(trimmed)
|| trimmed.starts_with("pub ")
|| trimmed.starts_with("export ")
|| trimmed == "}"
|| trimmed == "{";
if is_important || kept_lines < max_lines / 2 {
if skipped_section {
result.push(format!(
" // ... {} lines omitted",
lines.len() - kept_lines
));
skipped_section = false;
}
result.push((*line).to_string());
kept_lines += 1;
} else {
skipped_section = true;
}
if kept_lines >= max_lines - 1 {
break;
}
}
if skipped_section || kept_lines < lines.len() {
result.push(format!(
"// ... {} more lines (total: {})",
lines.len() - kept_lines,
lines.len()
));
}
result.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter_level_parsing() {
assert_eq!(FilterLevel::from_str("none").unwrap(), FilterLevel::None);
assert_eq!(
FilterLevel::from_str("minimal").unwrap(),
FilterLevel::Minimal
);
assert_eq!(
FilterLevel::from_str("aggressive").unwrap(),
FilterLevel::Aggressive
);
}
#[test]
fn test_language_detection() {
assert_eq!(Language::from_extension("rs"), Language::Rust);
assert_eq!(Language::from_extension("py"), Language::Python);
assert_eq!(Language::from_extension("js"), Language::JavaScript);
}
#[test]
fn test_language_detection_data_formats() {
assert_eq!(Language::from_extension("json"), Language::Data);
assert_eq!(Language::from_extension("yaml"), Language::Data);
assert_eq!(Language::from_extension("yml"), Language::Data);
assert_eq!(Language::from_extension("toml"), Language::Data);
assert_eq!(Language::from_extension("xml"), Language::Data);
assert_eq!(Language::from_extension("csv"), Language::Data);
assert_eq!(Language::from_extension("md"), Language::Data);
assert_eq!(Language::from_extension("lock"), Language::Data);
}
#[test]
fn test_json_no_comment_stripping() {
// Reproduces #464: package.json with "packages/*" was corrupted
// because /* was treated as block comment start
let json = r#"{
"workspaces": {
"packages": [
"packages/*"
]
},
"scripts": {
"build": "bun run --workspaces build"
},
"lint-staged": {
"**/package.json": [
"sort-package-json"
]
}
}"#;
let filter = MinimalFilter;
let result = filter.filter(json, &Language::Data);
// All fields must be preserved — no comment stripping on JSON
assert!(
result.contains("packages/*"),
"packages/* should not be treated as block comment start"
);
assert!(
result.contains("scripts"),
"scripts section must not be stripped"
);
assert!(
result.contains("lint-staged"),
"lint-staged section must not be stripped"
);
assert!(
result.contains("**/package.json"),
"**/package.json should not be treated as block comment end"
);
}
#[test]
fn test_json_aggressive_filter_preserves_structure() {
let json = r#"{
"name": "my-app",
"dependencies": {
"react": "^18.0.0"
},
"scripts": {
"dev": "next dev /* not a comment */"
}
}"#;
let filter = AggressiveFilter;
let result = filter.filter(json, &Language::Data);
assert!(
result.contains("/* not a comment */"),
"Aggressive filter must not strip comment-like patterns in JSON"
);
}
#[test]
fn test_minimal_filter_removes_comments() {
let code = r#"
// This is a comment
fn main() {
println!("Hello");
}
"#;
let filter = MinimalFilter;
let result = filter.filter(code, &Language::Rust);
assert!(!result.contains("// This is a comment"));
assert!(result.contains("fn main()"));
}
}