Fixed crash on multibyte characters that include '\n'

Fixes #13
pull/25/head
Wilfred Hughes 2021-07-09 23:37:04 +07:00
parent bbc77b8cbc
commit 4cf2a24997
2 changed files with 14 additions and 37 deletions

@ -13,6 +13,9 @@ lines.
Fixed a crash when diff context included the first line.
Fixed a crash when plain text content contained certain non-ASCII
characters.
## 0.3
Diffs are now displayed with unchanged lines aligned to the other side.

@ -116,44 +116,18 @@ fn lang_from_value(name: &str, v: &Value) -> Language {
///
/// This is a fallback for files that we don't know how to parse.
pub fn parse_lines<'a>(arena: &'a Arena<Syntax<'a>>, s: &str) -> Vec<&'a Syntax<'a>> {
let mut line_start = 0;
let mut res: Vec<&'a Syntax<'a>> = vec![];
for (i, c) in s.chars().enumerate() {
if c == '\n' {
let line = &s[line_start..i];
let atom = Syntax::new_atom(
arena,
vec![SingleLineSpan {
line: res.len().into(),
start_col: 0,
end_col: i - line_start,
}],
line,
AtomKind::Other,
);
res.push(atom);
line_start = i + 1;
}
}
if let Some(last) = s.chars().last() {
if last != '\n' {
let line = &s[line_start..];
let atom = Syntax::new_atom(
arena,
vec![SingleLineSpan {
line: res.len().into(),
start_col: 0,
end_col: s.len() - line_start,
}],
line,
AtomKind::Other,
);
res.push(atom);
}
for (i, line) in s.lines().enumerate() {
res.push(Syntax::new_atom(
arena,
vec![SingleLineSpan {
line: i.into(),
start_col: 0,
end_col: line.len(),
}],
line,
AtomKind::Other,
));
}
res