From 81bbef60729e817f4f18057d7b5c7a95b9ab36cb Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 4 Sep 2023 03:10:33 +0200 Subject: [PATCH] feat: helpers in textchange to convert to rowcol --- src/buffer/mod.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index c3a7e21..dc799d4 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -19,6 +19,8 @@ pub mod factory; pub use factory::OperationFactory; pub use controller::BufferController as Controller; +use crate::proto::RowCol; + /// an editor-friendly representation of a text change in a buffer /// @@ -34,4 +36,26 @@ pub struct TextChange { /// reference to current content of buffer pub after: Arc, } + +impl TextChange { + /// convert from byte index to row and column. + /// if `end` is true, span end will be used, otherwise span start + /// if `after` is true, buffer after change will be used, otherwise buffer before change + fn index_to_rowcol(&self, end: bool, after: bool) -> RowCol { + let txt = if after { &self.after } else { &self.before }; + let index = if end { self.span.end } else { self.span.start }; + let row = txt[..index].matches('\n').count() as i32; + let col = txt[..index].split('\n').last().unwrap_or("").len() as i32; + RowCol { row, col } + } + + /// retrn row and column of text change start + pub fn start(&self) -> RowCol { + self.index_to_rowcol(false, false) + } + + /// return row and column of text change end + pub fn end(&self) -> RowCol { + self.index_to_rowcol(true, false) + } }