Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix span relocation #14

Merged
merged 2 commits into from
Jan 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ccs"
version = "0.9.0"
version = "0.9.1"
edition = "2021"
license = "0BSD"
repository = "https://github.com/museun/ccs"
@@ -14,4 +14,5 @@ indoc = "2.0.5"
owo-colors = { version = "3.5.0", features = [ "supports-colors" ] }
serde = { version = "1.0.213", features = [ "derive" ] }
serde_json = "1.0.132"
str_indices = "0.4.4"
toml = "0.7.8"
50 changes: 48 additions & 2 deletions src/parse/span.rs
Original file line number Diff line number Diff line change
@@ -22,9 +22,13 @@ impl Span {
if matches!(render_options.render, RenderStyle::Full) {
use owo_colors::OwoColorize as _;
self.relocate().try_for_each(|(start, end, text)| {
let start = floor_char_boundary(text, start);
let end = ceil_char_boundary(text, end);

let head = &text[..start];
let mid = &text[start..end];
let tail = &text[end..];

writeln!(
out,
" {head}{mid}{tail}",
@@ -68,14 +72,56 @@ impl Span {
left_pad = span.text.len() - s.len();
}

let start = span.highlight_start.saturating_sub(left_pad + 1);
let end = span.highlight_end.saturating_sub(left_pad + 1);

let start = str_indices::chars::from_byte_idx(&span.text, start);
let end = str_indices::chars::from_byte_idx(&span.text, end);

// error messages are 1 indexed
break Some((
span.highlight_start.saturating_sub(left_pad + 1),
span.highlight_end.saturating_sub(left_pad + 1),
start,
end,
// TODO use unicode-segmentation here
// what does this mean? how would segmentation be applicable here?
&span.text[left_pad..],
));
}
})
}
}

// NOTE this is taken from <https://github.com/rust-lang/rust/issues/93743>
// TODO its currently unstable but its fine for what we need
fn floor_char_boundary(str: &str, index: usize) -> usize {
if index >= str.len() {
str.len()
} else {
let lower_bound = index.saturating_sub(3);
let new_index = str.as_bytes()[lower_bound..=index]
.iter()
.rposition(|&b| is_utf8_char_boundary(b));

lower_bound + new_index.unwrap()
}
}

// NOTE this is taken from <https://github.com/rust-lang/rust/issues/93743>
// TODO its currently unstable but its fine for what we need
fn ceil_char_boundary(str: &str, index: usize) -> usize {
if index > str.len() {
str.len()
} else {
let upper_bound = Ord::min(index + 4, str.len());
str.as_bytes()[index..upper_bound]
.iter()
.position(|&b| is_utf8_char_boundary(b))
.map_or(upper_bound, |pos| pos + index)
}
}

// NOTE impl detail of `u8::is_utf8_char_boundary` used by `floor_char_boundary` and `ceil_char_boundary`
const fn is_utf8_char_boundary(byte: u8) -> bool {
// This is bit magic equivalent to: b < 128 || b >= 192
(byte as i8) >= -0x40
}
4 changes: 2 additions & 2 deletions src/parse/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[derive(Debug, serde::Deserialize)]
pub struct Text {
pub highlight_start: usize,
pub highlight_end: usize,
pub highlight_start: usize, // are these byte or grapheme indices?
pub highlight_end: usize, // are these byte or grapheme indices?
pub text: String,
}