|
|
@ -5,10 +5,13 @@ pub mod steel_implementations {
|
|
|
|
|
|
|
|
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
use steel::{
|
|
|
|
use steel::{
|
|
|
|
rvals::{Custom, SteelString},
|
|
|
|
rvals::{as_underlying_type, Custom, SteelString},
|
|
|
|
steel_vm::{builtin::BuiltInModule, register_fn::RegisterFn},
|
|
|
|
steel_vm::{builtin::BuiltInModule, register_fn::RegisterFn},
|
|
|
|
|
|
|
|
SteelVal,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use helix_stdx::rope::RopeSliceExt;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::syntax::{AutoPairConfig, SoftWrap};
|
|
|
|
use crate::syntax::{AutoPairConfig, SoftWrap};
|
|
|
|
|
|
|
|
|
|
|
|
impl steel::rvals::Custom for crate::Position {}
|
|
|
|
impl steel::rvals::Custom for crate::Position {}
|
|
|
@ -16,20 +19,38 @@ pub mod steel_implementations {
|
|
|
|
impl steel::rvals::Custom for AutoPairConfig {}
|
|
|
|
impl steel::rvals::Custom for AutoPairConfig {}
|
|
|
|
impl steel::rvals::Custom for SoftWrap {}
|
|
|
|
impl steel::rvals::Custom for SoftWrap {}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
enum SliceKind {
|
|
|
|
enum SliceKind {
|
|
|
|
Normal(usize, usize),
|
|
|
|
Normal(usize, usize),
|
|
|
|
Byte(usize, usize),
|
|
|
|
Byte(usize, usize),
|
|
|
|
Line(usize),
|
|
|
|
Line(usize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
|
|
pub struct SteelRopeSlice {
|
|
|
|
pub struct SteelRopeSlice {
|
|
|
|
text: crate::Rope,
|
|
|
|
text: crate::Rope,
|
|
|
|
ranges: SmallVec<[SliceKind; 5]>,
|
|
|
|
ranges: SmallVec<[SliceKind; 5]>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Custom for SteelRopeSlice {}
|
|
|
|
impl Custom for SteelRopeSlice {
|
|
|
|
|
|
|
|
// `equal?` on two ropes should return true if they are the same
|
|
|
|
|
|
|
|
fn equality_hint(&self, other: &dyn steel::rvals::CustomType) -> bool {
|
|
|
|
|
|
|
|
if let Some(other) = as_underlying_type::<SteelRopeSlice>(other) {
|
|
|
|
|
|
|
|
self == other
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn equality_hint_general(&self, other: &steel::SteelVal) -> bool {
|
|
|
|
|
|
|
|
match other {
|
|
|
|
|
|
|
|
SteelVal::StringV(s) => self.to_slice() == s.as_str(),
|
|
|
|
|
|
|
|
SteelVal::Custom(c) => Self::equality_hint(&self, c.borrow().as_ref()),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl SteelRopeSlice {
|
|
|
|
impl SteelRopeSlice {
|
|
|
|
pub fn from_string(string: SteelString) -> Self {
|
|
|
|
pub fn from_string(string: SteelString) -> Self {
|
|
|
@ -95,30 +116,48 @@ pub mod steel_implementations {
|
|
|
|
self.to_slice().len_lines()
|
|
|
|
self.to_slice().len_lines()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn trim_start(mut self) -> Self {
|
|
|
|
|
|
|
|
for (idx, c) in self.to_slice().chars().enumerate() {
|
|
|
|
|
|
|
|
if !c.is_whitespace() {
|
|
|
|
|
|
|
|
self.ranges.push(SliceKind::Normal(0, idx));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn trimmed_starts_with(&self, pat: SteelString) -> bool {
|
|
|
|
pub fn trimmed_starts_with(&self, pat: SteelString) -> bool {
|
|
|
|
let maybe_owned = Cow::from(self.to_slice());
|
|
|
|
let maybe_owned = Cow::from(self.to_slice());
|
|
|
|
|
|
|
|
|
|
|
|
maybe_owned.trim_start().starts_with(pat.as_str())
|
|
|
|
maybe_owned.trim_start().starts_with(pat.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn starts_with(&self, pat: SteelString) -> bool {
|
|
|
|
|
|
|
|
self.to_slice().starts_with(pat.as_str())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn ends_with(&self, pat: SteelString) -> bool {
|
|
|
|
|
|
|
|
self.to_slice().ends_with(pat.as_str())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn rope_module() -> BuiltInModule {
|
|
|
|
pub fn rope_module() -> BuiltInModule {
|
|
|
|
let mut module = BuiltInModule::new("helix/core/text");
|
|
|
|
let mut module = BuiltInModule::new("helix/core/text");
|
|
|
|
|
|
|
|
|
|
|
|
module
|
|
|
|
module
|
|
|
|
.register_fn("string->slice", SteelRopeSlice::from_string)
|
|
|
|
.register_fn("string->rope", SteelRopeSlice::from_string)
|
|
|
|
.register_fn("slice->slice", SteelRopeSlice::slice)
|
|
|
|
.register_fn("rope->slice", SteelRopeSlice::slice)
|
|
|
|
.register_fn("slice-char->byte", SteelRopeSlice::char_to_byte)
|
|
|
|
.register_fn("rope-char->byte", SteelRopeSlice::char_to_byte)
|
|
|
|
.register_fn("slice->byte-slice", SteelRopeSlice::byte_slice)
|
|
|
|
.register_fn("rope->byte-slice", SteelRopeSlice::byte_slice)
|
|
|
|
.register_fn("slice->line", SteelRopeSlice::line)
|
|
|
|
.register_fn("rope->line", SteelRopeSlice::line)
|
|
|
|
.register_fn("slice->string", SteelRopeSlice::to_string)
|
|
|
|
.register_fn("rope->string", SteelRopeSlice::to_string)
|
|
|
|
.register_fn("slice-len-chars", SteelRopeSlice::len_chars)
|
|
|
|
.register_fn("rope-len-chars", SteelRopeSlice::len_chars)
|
|
|
|
.register_fn("slice-char-ref", SteelRopeSlice::get_char)
|
|
|
|
.register_fn("rope-char-ref", SteelRopeSlice::get_char)
|
|
|
|
.register_fn("slice-len-lines", SteelRopeSlice::len_lines)
|
|
|
|
.register_fn("rope-len-lines", SteelRopeSlice::len_lines)
|
|
|
|
.register_fn(
|
|
|
|
.register_fn("rope-starts-with?", SteelRopeSlice::starts_with)
|
|
|
|
"slice-trim-and-starts-with?",
|
|
|
|
.register_fn("rope-ends-with?", SteelRopeSlice::ends_with)
|
|
|
|
SteelRopeSlice::trimmed_starts_with,
|
|
|
|
.register_fn("rope-trim-start", SteelRopeSlice::trim_start);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module
|
|
|
|
module
|
|
|
|
}
|
|
|
|
}
|
|
|
|