Remove the entire `@phpstan-ignore` comment from a line. If the comment is the only content on the line (possibly with leading whitespace), delete the entire line. Otherwise, remove just the comment portion.
(
content: &str,
line: u32,
line_text: &str,
ignore_pos: usize,
)
| 374 | /// leading whitespace), delete the entire line. Otherwise, remove |
| 375 | /// just the comment portion. |
| 376 | fn build_remove_whole_ignore( |
| 377 | content: &str, |
| 378 | line: u32, |
| 379 | line_text: &str, |
| 380 | ignore_pos: usize, |
| 381 | ) -> Option<TextEdit> { |
| 382 | // Find the start of the comment that contains @phpstan-ignore. |
| 383 | // Walk backwards from `ignore_pos` to find `//`, `/*`, or `/**`. |
| 384 | let before = &line_text[..ignore_pos]; |
| 385 | let comment_start = before |
| 386 | .rfind("//") |
| 387 | .or_else(|| before.rfind("/*")) |
| 388 | .unwrap_or(ignore_pos); |
| 389 | |
| 390 | let before_comment = line_text[..comment_start].trim_end(); |
| 391 | |
| 392 | if before_comment.is_empty() { |
| 393 | // The comment is the only thing on this line — delete the whole line. |
| 394 | let line_count = content.lines().count(); |
| 395 | let next_line = line + 1; |
| 396 | |
| 397 | if (next_line as usize) <= line_count { |
| 398 | // Delete from start of this line to start of next line. |
| 399 | Some(TextEdit { |
| 400 | range: Range { |
| 401 | start: Position { line, character: 0 }, |
| 402 | end: Position { |
| 403 | line: next_line, |
| 404 | character: 0, |
| 405 | }, |
| 406 | }, |
| 407 | new_text: String::new(), |
| 408 | }) |
| 409 | } else { |
| 410 | // Last line — delete from end of previous line to end of this line. |
| 411 | if line > 0 { |
| 412 | let prev_line_text = content.lines().nth((line - 1) as usize).unwrap_or(""); |
| 413 | Some(TextEdit { |
| 414 | range: Range { |
| 415 | start: Position { |
| 416 | line: line - 1, |
| 417 | character: prev_line_text.len() as u32, |
| 418 | }, |
| 419 | end: Position { |
| 420 | line, |
| 421 | character: line_text.len() as u32, |
| 422 | }, |
| 423 | }, |
| 424 | new_text: String::new(), |
| 425 | }) |
| 426 | } else { |
| 427 | Some(TextEdit { |
| 428 | range: Range { |
| 429 | start: Position { line, character: 0 }, |
| 430 | end: Position { |
| 431 | line, |
| 432 | character: line_text.len() as u32, |
| 433 | }, |
no test coverage detected