Struct StickyIndex
pub struct StickyIndex {
pub assoc: Assoc,
/* private fields */
}Expand description
A sticky index is based on the Yjs model and is not affected by document changes. E.g. If you place a sticky index before a certain character, it will always point to this character. If you place a sticky index at the end of a type, it will always point to the end of the type.
A numeric position is often unsuited for user selections, because it does not change when content is inserted before or after.
Insert(0, 'x')('a.bc') = 'xa.bc' Where . is the relative position.
Example:
use yrs::{Assoc, Doc, IndexedSequence, Text, Transact};
let doc = Doc::new();
let txt = doc.get_or_insert_text("text");
let mut txn = doc.transact_mut();
txt.insert(&mut txn, 0, "abc"); // => 'abc'
// create position tracker (marked as . in the comments)
let pos = txt.sticky_index(&mut txn, 2, Assoc::After).unwrap(); // => 'ab.c'
// modify text
txt.insert(&mut txn, 1, "def"); // => 'adefb.c'
txt.remove_range(&mut txn, 4, 1); // => 'adef.c'
// get current offset index within the containing collection
let a = pos.get_offset(&txn).unwrap();
assert_eq!(a.index, 4);Fields§
§assoc: AssocIf true - associate to the right block. Otherwise, associate to the left one.
Implementations§
§impl StickyIndex
impl StickyIndex
pub fn new(scope: IndexScope, assoc: Assoc) -> StickyIndex
pub fn from_id(id: ID, assoc: Assoc) -> StickyIndex
pub fn from_type<T, B>(_txn: &T, branch: &B, assoc: Assoc) -> StickyIndex
pub fn scope(&self) -> &IndexScope
pub fn is_relative(&self) -> bool
pub fn is_relative(&self) -> bool
Scope refers to a position relative to another block.
pub fn id(&self) -> Option<&ID>
pub fn id(&self) -> Option<&ID>
Returns an ID of the block position which is used as a reference to keep track the location of current StickyIndex even in face of changes performed by different peers.
Returns None if current StickyIndex has been created on an empty shared collection (in
that case there’s no block that we can refer to).
pub fn get_offset<T>(&self, txn: &T) -> Option<Offset>where
T: ReadTxn,
pub fn get_offset<T>(&self, txn: &T) -> Option<Offset>where
T: ReadTxn,
Maps current StickyIndex onto Offset which points to shared collection and a human-readable index in that collection.
That index is only valid at the current point in time - if i.e. another update from remote peer has been applied, it may have changed relative index position that StickyIndex points to, so that Offset’s index will no longer point to the same place.
§Examples
use yrs::{Assoc, Doc, IndexedSequence, Text, Transact};
let doc = Doc::new();
let text = doc.get_or_insert_text("text");
let mut txn = doc.transact_mut();
text.insert(&mut txn, 0, "hello world");
const INDEX: u32 = 4;
// set perma index at position before letter 'o' => "hell.o world"
let pos = text.sticky_index(&mut txn, INDEX, Assoc::After).unwrap();
let off = pos.get_offset(&txn).unwrap();
assert_eq!(off.index, INDEX);
// perma index will maintain it's position before letter 'o' even if another update
// shifted it's index inside of the text
text.insert(&mut txn, 1, "(see)"); // => "h(see)ell.o world" where . is perma index position
let off2 = pos.get_offset(&txn).unwrap();
assert_ne!(off2.index, off.index); // offset index changed due to new insert abovepub fn at<T>(
txn: &T,
branch: BranchPtr,
index: u32,
assoc: Assoc,
) -> Option<StickyIndex>where
T: ReadTxn,
Trait Implementations§
§impl Clone for StickyIndex
impl Clone for StickyIndex
§fn clone(&self) -> StickyIndex
fn clone(&self) -> StickyIndex
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for StickyIndex
impl Debug for StickyIndex
§impl Decode for StickyIndex
impl Decode for StickyIndex
§impl<'de> Deserialize<'de> for StickyIndex
impl<'de> Deserialize<'de> for StickyIndex
§fn deserialize<D>(
deserializer: D,
) -> Result<StickyIndex, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<StickyIndex, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl Display for StickyIndex
impl Display for StickyIndex
§impl Encode for StickyIndex
impl Encode for StickyIndex
§impl Hash for StickyIndex
impl Hash for StickyIndex
§impl Ord for StickyIndex
impl Ord for StickyIndex
§impl PartialEq for StickyIndex
impl PartialEq for StickyIndex
§impl PartialOrd for StickyIndex
impl PartialOrd for StickyIndex
§impl Serialize for StickyIndex
impl Serialize for StickyIndex
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl Eq for StickyIndex
impl StructuralPartialEq for StickyIndex
Auto Trait Implementations§
impl Freeze for StickyIndex
impl RefUnwindSafe for StickyIndex
impl Send for StickyIndex
impl Sync for StickyIndex
impl Unpin for StickyIndex
impl UnwindSafe for StickyIndex
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.