mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 07:14:50 +01:00
feat: utility fn to convert from index to rowcol
This commit is contained in:
parent
488c22c0f1
commit
3bc1159ffc
1 changed files with 10 additions and 0 deletions
|
@ -3,6 +3,8 @@
|
|||
//! an editor-friendly representation of a text change in a buffer
|
||||
//! to easily interface with codemp from various editors
|
||||
|
||||
use crate::proto::RowCol;
|
||||
|
||||
/// an editor-friendly representation of a text change in a buffer
|
||||
///
|
||||
/// this represent a range in the previous state of the string and a new content which should be
|
||||
|
@ -56,4 +58,12 @@ impl TextChange {
|
|||
content: after[start..end_after].to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// convert from byte index to row and column
|
||||
/// txt must be the whole content of the buffer, in order to count lines
|
||||
pub fn index_to_rowcol(txt: &str, index: usize) -> RowCol {
|
||||
let row = txt[..index].matches('\n').count() as i32;
|
||||
let col = txt[..index].split('\n').last().unwrap_or("").len() as i32;
|
||||
RowCol { row, col }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue