fix: overlong single-line text containing escape sequences was not being properly escaped (#1497)

This commit is contained in:
三咲雅 · Misaki Masa 2024-08-16 15:40:11 +08:00 committed by GitHub
parent 4112bf451f
commit 374c6a8891
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -60,9 +60,9 @@ impl Highlighter {
let mut reader = BufReader::new(File::open(&self.path).await?);
let syntax = Self::find_syntax(&self.path).await;
let mut plain = syntax.is_err() as u8;
let mut plain = syntax.is_err();
let mut before = Vec::with_capacity(if plain == 0 { skip } else { 0 });
let mut before = Vec::with_capacity(if plain { 0 } else { skip });
let mut after = Vec::with_capacity(limit);
let mut i = 0;
@ -73,11 +73,8 @@ impl Highlighter {
break;
}
if plain == 0 && buf.len() > 5000 {
plain = 1;
drop(mem::take(&mut before));
} else if plain == 0 && buf.contains(&0x1b) {
plain = 2;
if !plain && (buf.len() > 5000 || buf.contains(&0x1b)) {
plain = true;
drop(mem::take(&mut before));
}
@ -89,7 +86,7 @@ impl Highlighter {
if i > skip {
after.push(String::from_utf8_lossy(&buf).into_owned());
} else if plain == 0 {
} else if !plain {
before.push(String::from_utf8_lossy(&buf).into_owned());
}
buf.clear();
@ -99,15 +96,11 @@ impl Highlighter {
return Err(PeekError::Exceed(i.saturating_sub(limit)));
}
if plain == 0 {
Self::highlight_with(before, after, syntax.unwrap()).await
} else if plain == 1 {
Ok(Text::from(after.join("").replace('\t', &PREVIEW.indent())))
} else if plain == 2 {
Ok(Text::from(after.join("").replace('\x1b', "^[").replace('\t', &PREVIEW.indent())))
Ok(if plain {
Text::from(after.join("").replace('\x1b', "^[").replace('\t', &PREVIEW.indent()))
} else {
unreachable!()
}
Self::highlight_with(before, after, syntax.unwrap()).await?
})
}
async fn highlight_with(