use crate::compat::call_sites::{AttrCtxScanner, strip_line_comment};
use crate::compat::patch::{Hunk, HunkLine};
use crate::model::names::RelativePath;
use crate::model::symbol::RefContext;
#[derive(Debug, Clone, Copy)]
pub struct AddedLinesSubject<'a> {
pub source_path: &'a RelativePath,
pub added_text: &'a str,
pub line_map: &'a [u32],
}
pub fn added_lines_with_offsets(hunks: &[Hunk]) -> (String, Vec<u32>) {
let mut blob = String::new();
let mut line_map = Vec::new();
for hunk in hunks {
let mut new_line = hunk.new_start as u32;
for line in &hunk.lines {
match line {
HunkLine::Added(s) => {
blob.push_str(s);
blob.push('\n');
line_map.push(new_line);
new_line += 1;
}
HunkLine::Context(_) => new_line += 1,
HunkLine::Removed(_) => {}
}
}
}
(blob, line_map)
}
pub fn added_lines_with_context(hunks: &[Hunk]) -> (String, Vec<u32>, Vec<RefContext>) {
let mut blob = String::new();
let mut line_map = Vec::new();
let mut ctx = Vec::new();
let mut scanner = AttrCtxScanner::new();
for hunk in hunks {
let mut new_line = hunk.new_start as u32;
for line in &hunk.lines {
match line {
HunkLine::Added(s) => {
ctx.push(scanner.classify(strip_line_comment(s)));
blob.push_str(s);
blob.push('\n');
line_map.push(new_line);
new_line += 1;
}
HunkLine::Context(s) => {
scanner.classify(strip_line_comment(s));
new_line += 1;
}
HunkLine::Removed(_) => {}
}
}
}
(blob, line_map, ctx)
}
pub fn file_line(line_map: &[u32], blob_line: u32) -> u32 {
line_map
.get(blob_line.saturating_sub(1) as usize)
.copied()
.unwrap_or(blob_line)
}