133 lines
3.7 KiB
Rust
133 lines
3.7 KiB
Rust
//! Bounded snapshots of actual scanner input, not reconstructed call histories.
|
|
use super::*;
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum ContextKind {
|
|
Source {
|
|
source: usize,
|
|
name: String,
|
|
line: usize,
|
|
},
|
|
Macro(CommandName),
|
|
Argument,
|
|
Hook(TokenParameter),
|
|
BackedUp {
|
|
exhausted: bool,
|
|
},
|
|
Inserted,
|
|
Write,
|
|
Mark,
|
|
Output,
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct ContextFrame {
|
|
pub kind: ContextKind,
|
|
pub label: String,
|
|
pub before: String,
|
|
pub second: String,
|
|
}
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct InputContext {
|
|
pub reads: std::collections::BTreeSet<Dependency>,
|
|
pub frames: Vec<ContextFrame>,
|
|
pub text: String,
|
|
}
|
|
/// TeX's half_error_line=50 / error_line=79 pseudo printer. Only the bounded
|
|
/// suffix before the cursor and prefix after it are retained, even for large
|
|
/// macro bodies and source lines.
|
|
pub(super) struct Pseudo {
|
|
before: std::collections::VecDeque<char>,
|
|
count: usize,
|
|
after: Vec<char>,
|
|
split: bool,
|
|
}
|
|
impl Pseudo {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
before: std::collections::VecDeque::new(),
|
|
count: 0,
|
|
after: Vec::new(),
|
|
split: false,
|
|
}
|
|
}
|
|
pub fn split(&mut self) {
|
|
self.split = true;
|
|
}
|
|
pub fn push(&mut self, c: char) {
|
|
if self.split {
|
|
if self.after.len() < 80 {
|
|
self.after.push(c);
|
|
}
|
|
} else {
|
|
self.count += 1;
|
|
if self.before.len() == 79 {
|
|
self.before.pop_front();
|
|
}
|
|
self.before.push_back(c);
|
|
}
|
|
}
|
|
pub fn text(&mut self, text: &str) {
|
|
for c in text.chars() {
|
|
self.character(c);
|
|
}
|
|
}
|
|
pub fn character(&mut self, c: char) {
|
|
let encoded = match c as u32 {
|
|
0..=31 => format!("^^{}", char::from(c as u8 + 64)),
|
|
127 => "^^?".into(),
|
|
128..=255 => format!("^^{:02x}", c as u32),
|
|
_ => c.to_string(),
|
|
};
|
|
for c in encoded.chars() {
|
|
self.push(c);
|
|
}
|
|
}
|
|
pub fn finish(self, label: &str, kind: ContextKind, newline: i32) -> ContextFrame {
|
|
let l = label
|
|
.chars()
|
|
.filter(|c| *c as i32 != newline)
|
|
.map(|c| encoded_character(c).chars().count())
|
|
.sum::<usize>();
|
|
let n = (l + self.count).min(50);
|
|
let cropped = l + self.count > 50;
|
|
let keep = if cropped {
|
|
50usize.saturating_sub(l + 3)
|
|
} else {
|
|
self.count
|
|
};
|
|
let suffix: String = self
|
|
.before
|
|
.iter()
|
|
.skip(self.before.len().saturating_sub(keep))
|
|
.collect();
|
|
let before = format!("{}{suffix}", if cropped { "..." } else { "" });
|
|
let available = 79 - n;
|
|
let right_cropped = self.after.len() > available;
|
|
let keep = if right_cropped {
|
|
available.saturating_sub(3)
|
|
} else {
|
|
self.after.len()
|
|
};
|
|
let after: String = self.after.iter().take(keep).collect();
|
|
let second = format!(
|
|
"{}{after}{}",
|
|
" ".repeat(n),
|
|
if right_cropped { "..." } else { "" }
|
|
);
|
|
ContextFrame {
|
|
kind,
|
|
label: label.to_owned(),
|
|
before,
|
|
second,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) fn encoded_character(c: char) -> String {
|
|
match c as u32 {
|
|
0..=31 => format!("^^{}", char::from(c as u8 + 64)),
|
|
127 => "^^?".into(),
|
|
128..=255 => format!("^^{:02x}", c as u32),
|
|
_ => c.to_string(),
|
|
}
|
|
}
|