diff options
author | Nadrieril | 2020-03-31 20:25:02 +0100 |
---|---|---|
committer | GitHub | 2020-03-31 20:25:02 +0100 |
commit | 5a5aa49e64197899006751db72e404f4b2292d4e (patch) | |
tree | 0b62e961c1de99ce9a78c4775ce8eab047cdf768 /dhall/src | |
parent | eecd41fe5dd155752aaaa06af02bc32f8a416ba5 (diff) | |
parent | e12fb2928307d240461e749e5d371cef17cf520c (diff) |
Merge pull request #156 from Nadrieril/fix-155
Fix #155
Diffstat (limited to 'dhall/src')
-rw-r--r-- | dhall/src/error/builder.rs | 6 | ||||
-rw-r--r-- | dhall/src/syntax/ast/span.rs | 20 |
2 files changed, 10 insertions, 16 deletions
diff --git a/dhall/src/error/builder.rs b/dhall/src/error/builder.rs index f444518..c0bacb5 100644 --- a/dhall/src/error/builder.rs +++ b/dhall/src/error/builder.rs @@ -1,6 +1,5 @@ use annotate_snippets::{ display_list::DisplayList, - formatter::DisplayListFormatter, snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation}, }; @@ -152,10 +151,9 @@ impl ErrorBuilder { title: Some(this.title.into_annotation()), slices, footer, + opt: Default::default(), }; - let dl = DisplayList::from(snippet); - let dlf = DisplayListFormatter::new(true, false); - dlf.format(&dl) + DisplayList::from(snippet).to_string() } } diff --git a/dhall/src/syntax/ast/span.rs b/dhall/src/syntax/ast/span.rs index 153a624..2e09863 100644 --- a/dhall/src/syntax/ast/span.rs +++ b/dhall/src/syntax/ast/span.rs @@ -75,19 +75,15 @@ impl Span { } /// Convert a byte idx into a string into a char idx for consumption by annotate_snippets. +/// The byte idx must be at a char boundary. fn char_idx_from_byte_idx(input: &str, idx: usize) -> usize { - let char_idx = input + use std::iter::once; + input .char_indices() + .map(|(byte_i, _)| byte_i) // We don't care about the char + .chain(once(input.len())) // In case the idx points to the end of the string .enumerate() - .find(|(_, (i, _))| *i == idx) - .map(|(i, (_, _))| i) - // We should be able to unwrap() here, but somehow it panics on an example from - // serde_dhall/lib.rs... - .unwrap_or(0); - // Unix-style newlines are counted as two chars (see - // https://github.com/rust-lang/annotate-snippets-rs/issues/24). - let nbr_newlines = input[..idx].chars().filter(|c| *c == '\n').count(); - let nbr_carriage_returns = - input[..idx].chars().filter(|c| *c == '\r').count(); - char_idx + nbr_newlines - nbr_carriage_returns + .find(|(_, byte_i)| *byte_i == idx) + .map(|(char_i, _)| char_i) + .unwrap() } |