mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-07 17:17:02 +03:00
Merge pull request #1922 from zed-industries/dont-panic-clip-instead
Dont panic in point conversion, clip instead
This commit is contained in:
commit
5a0c39cbed
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -6785,6 +6785,7 @@ name = "util"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"backtrace",
|
||||||
"futures 0.3.24",
|
"futures 0.3.24",
|
||||||
"git2",
|
"git2",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
@ -12,7 +12,7 @@ smallvec = { version = "1.6", features = ["union"] }
|
|||||||
sum_tree = { path = "../sum_tree" }
|
sum_tree = { path = "../sum_tree" }
|
||||||
arrayvec = "0.7.1"
|
arrayvec = "0.7.1"
|
||||||
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
||||||
|
util = { path = "../util" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rand = "0.8.3"
|
rand = "0.8.3"
|
||||||
|
@ -12,6 +12,7 @@ use std::{
|
|||||||
str,
|
str,
|
||||||
};
|
};
|
||||||
use sum_tree::{Bias, Dimension, SumTree};
|
use sum_tree::{Bias, Dimension, SumTree};
|
||||||
|
use util::debug_panic;
|
||||||
|
|
||||||
pub use offset_utf16::OffsetUtf16;
|
pub use offset_utf16::OffsetUtf16;
|
||||||
pub use point::Point;
|
pub use point::Point;
|
||||||
@ -679,28 +680,33 @@ impl Chunk {
|
|||||||
fn point_to_offset(&self, target: Point) -> usize {
|
fn point_to_offset(&self, target: Point) -> usize {
|
||||||
let mut offset = 0;
|
let mut offset = 0;
|
||||||
let mut point = Point::new(0, 0);
|
let mut point = Point::new(0, 0);
|
||||||
|
|
||||||
for ch in self.0.chars() {
|
for ch in self.0.chars() {
|
||||||
if point >= target {
|
if point >= target {
|
||||||
if point > target {
|
if point > target {
|
||||||
panic!("point {:?} is inside of character {:?}", target, ch);
|
debug_panic!("point {target:?} is inside of character {ch:?}");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ch == '\n' {
|
if ch == '\n' {
|
||||||
point.row += 1;
|
point.row += 1;
|
||||||
if point.row > target.row {
|
|
||||||
panic!(
|
|
||||||
"point {:?} is beyond the end of a line with length {}",
|
|
||||||
target, point.column
|
|
||||||
);
|
|
||||||
}
|
|
||||||
point.column = 0;
|
point.column = 0;
|
||||||
|
|
||||||
|
if point.row > target.row {
|
||||||
|
debug_panic!(
|
||||||
|
"point {target:?} is beyond the end of a line with length {}",
|
||||||
|
point.column
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
point.column += ch.len_utf8() as u32;
|
point.column += ch.len_utf8() as u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
offset += ch.len_utf8();
|
offset += ch.len_utf8();
|
||||||
}
|
}
|
||||||
|
|
||||||
offset
|
offset
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -737,26 +743,27 @@ impl Chunk {
|
|||||||
if ch == '\n' {
|
if ch == '\n' {
|
||||||
point.row += 1;
|
point.row += 1;
|
||||||
point.column = 0;
|
point.column = 0;
|
||||||
|
|
||||||
if point.row > target.row {
|
if point.row > target.row {
|
||||||
if clip {
|
if !clip {
|
||||||
// Return the offset of the newline
|
debug_panic!(
|
||||||
return offset;
|
"point {target:?} is beyond the end of a line with length {}",
|
||||||
|
point.column
|
||||||
|
);
|
||||||
}
|
}
|
||||||
panic!(
|
// Return the offset of the newline
|
||||||
"point {:?} is beyond the end of a line with length {}",
|
return offset;
|
||||||
target, point.column
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
point.column += ch.len_utf16() as u32;
|
point.column += ch.len_utf16() as u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
if point > target {
|
if point > target {
|
||||||
if clip {
|
if !clip {
|
||||||
// Return the offset of the codepoint which we have landed within, bias left
|
debug_panic!("point {target:?} is inside of codepoint {ch:?}");
|
||||||
return offset;
|
|
||||||
}
|
}
|
||||||
panic!("point {:?} is inside of codepoint {:?}", target, ch);
|
// Return the offset of the codepoint which we have landed within, bias left
|
||||||
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
offset += ch.len_utf8();
|
offset += ch.len_utf8();
|
||||||
|
@ -11,6 +11,7 @@ test-support = ["serde_json", "tempdir", "git2"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.38"
|
anyhow = "1.0.38"
|
||||||
|
backtrace = "0.3"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
pub mod test;
|
pub mod test;
|
||||||
|
|
||||||
|
pub use backtrace::Backtrace;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use rand::{seq::SliceRandom, Rng};
|
use rand::{seq::SliceRandom, Rng};
|
||||||
use std::{
|
use std::{
|
||||||
@ -10,6 +11,18 @@ use std::{
|
|||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! debug_panic {
|
||||||
|
( $($fmt_arg:tt)* ) => {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
panic!( $($fmt_arg)* );
|
||||||
|
} else {
|
||||||
|
let backtrace = $crate::Backtrace::new();
|
||||||
|
log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn truncate(s: &str, max_chars: usize) -> &str {
|
pub fn truncate(s: &str, max_chars: usize) -> &str {
|
||||||
match s.char_indices().nth(max_chars) {
|
match s.char_indices().nth(max_chars) {
|
||||||
None => s,
|
None => s,
|
||||||
|
Loading…
Reference in New Issue
Block a user