Build a `TextEdit` that removes a `@phpstan-ignore` comment or a specific identifier from it. `message_line` is the line referenced in the PHPStan message (the line the ignore is supposed to suppress errors on). `diag_line` is the line PHPStan reports the unmatched-ignore error on. We search both, plus `message_line - 1` (the previous-line style), to cover all comment placement conventions. Re
(
content: &str,
message_line: u32,
diag_line: u32,
remove_id: Option<&str>,
)
| 320 | /// |
| 321 | /// Returns `None` if no `@phpstan-ignore` comment is found. |
| 322 | fn build_remove_ignore_edit( |
| 323 | content: &str, |
| 324 | message_line: u32, |
| 325 | diag_line: u32, |
| 326 | remove_id: Option<&str>, |
| 327 | ) -> Option<TextEdit> { |
| 328 | let lines: Vec<&str> = content.lines().collect(); |
| 329 | |
| 330 | // Search the message line, the line above it, and the diagnostic |
| 331 | // line (which may differ from message_line). Deduplicate so we |
| 332 | // don't check the same line twice. |
| 333 | let mut search_lines = vec![message_line]; |
| 334 | if message_line > 0 { |
| 335 | search_lines.push(message_line - 1); |
| 336 | } |
| 337 | if diag_line != message_line && (message_line == 0 || diag_line != message_line - 1) { |
| 338 | search_lines.push(diag_line); |
| 339 | } |
| 340 | |
| 341 | for &check_line in &search_lines { |
| 342 | let line_text = match lines.get(check_line as usize) { |
| 343 | Some(l) => *l, |
| 344 | None => continue, |
| 345 | }; |
| 346 | |
| 347 | if let Some(ignore_pos) = line_text.find("@phpstan-ignore") { |
| 348 | let after_tag = &line_text[ignore_pos + "@phpstan-ignore".len()..]; |
| 349 | |
| 350 | // Don't touch `@phpstan-ignore-line` / `@phpstan-ignore-next-line`. |
| 351 | if after_tag.starts_with("-line") || after_tag.starts_with("-next-line") { |
| 352 | // But if we specifically want to remove the whole thing, |
| 353 | // we can handle it below. |
| 354 | return build_remove_whole_ignore(content, check_line, line_text, ignore_pos); |
| 355 | } |
| 356 | |
| 357 | // If we have a specific identifier to remove, try to remove |
| 358 | // just that one from the comma-separated list. |
| 359 | if let Some(id) = remove_id { |
| 360 | return build_remove_single_id(content, check_line, line_text, ignore_pos, id); |
| 361 | } |
| 362 | |
| 363 | // No specific identifier — remove the whole comment. |
| 364 | return build_remove_whole_ignore(content, check_line, line_text, ignore_pos); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | None |
| 369 | } |
| 370 | |
| 371 | /// Remove the entire `@phpstan-ignore` comment from a line. |
| 372 | /// |