Skip to content

Commit

Permalink
Handle graphemes
Browse files Browse the repository at this point in the history
  • Loading branch information
terror committed Oct 1, 2024
1 parent 10aa975 commit de8d439
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 295 deletions.
586 changes: 336 additions & 250 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ keywords = ["command-line", "productivity", "utility", "markdown", "bash"]
resolver = "2"

[dependencies]
clap = { version = "4.0.9", features = ["derive"] }
console = "0.15.2"
pulldown-cmark = { version = "0.9.2", default-features = false, features = ["simd"] }
ropey = "1.5.0"
similar = "2.2.0"
snafu = { version = "0.7.1", default-features = false, features = ["std"] }
termimad = "0.20.3"
clap = { version = "4.5.19", features = ["derive"] }
console = "0.15.8"
pulldown-cmark = { version = "0.9.6", default-features = false, features = ["simd"] }
ropey = "1.6.1"
similar = "2.6.0"
snafu = { version = "0.7.5", default-features = false, features = ["std"] }
termimad = "0.20.6"
unicode-segmentation = "1.12.0"
walkdir = "2.3.2"
walkdir = "2.5.0"

[dev-dependencies]
executable-path = "1.0.0"
pretty_assertions = "1.3.0"
pretty_assertions = "1.4.1"
tempdir = "0.3.7"
unindent = "0.1.10"
unindent = "0.1.11"
3 changes: 1 addition & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Contains commonly used stuff from external crates
// std
pub(crate) use std::{
fs,
io::{self, Write},
Expand All @@ -9,12 +8,12 @@ pub(crate) use std::{
process, str,
};

// dependencies
pub(crate) use {
console::Style,
pulldown_cmark::{CodeBlockKind, Event, Parser as MarkdownParser, Tag},
ropey::Rope,
similar::{ChangeTag, TextDiff},
snafu::Snafu,
termimad::print_inline,
unicode_segmentation::UnicodeSegmentation,
};
36 changes: 31 additions & 5 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ pub struct Diff {
}

impl Diff {
/// Adjusts the diff's range by the given offset.
///
/// This method modifies the start and end points of the diff's range
/// based on the provided offset. It handles both positive and negative
/// offsets, using saturating arithmetic to prevent underflow or overflow.
pub(crate) fn offset(&mut self, offset: isize) {
if offset < 0 {
self.range.start = self.range.start.saturating_sub(offset.unsigned_abs());
self.range.end = self.range.end.saturating_sub(offset.unsigned_abs());
if offset >= 0 {
let offset = offset as usize;
self.range.start = self.range.start.saturating_add(offset);
self.range.end = self.range.end.saturating_add(offset);
} else {
self.range.start += offset as usize;
self.range.end += offset as usize;
let abs_offset = offset.unsigned_abs();
self.range.start = self.range.start.saturating_sub(abs_offset);
self.range.end = self.range.end.saturating_sub(abs_offset);
}
}

Expand Down Expand Up @@ -73,4 +80,23 @@ mod tests {
diff.offset(-10);
assert_eq!(diff.range, 0..0);
}

#[test]
fn offset_positive_large() {
let mut diff = diff();

diff.offset(isize::MAX);

assert_eq!(
diff.range,
(1 + isize::MAX as usize)..(4 + isize::MAX as usize)
);
}

#[test]
fn offset_negative_large() {
let mut diff = diff();
diff.offset(isize::MIN);
assert_eq!(diff.range, 0..0);
}
}
7 changes: 3 additions & 4 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,14 @@ impl File {
/// If [`interactive`](File::interactive) is set to `true`, the user will be
/// asked if they want to apply the change for each diff.
pub fn present(&mut self) -> Result {
let mut offset = 0;
let mut offset: isize = 0;

let diffs = self.diffs().collect::<Result<Vec<Diff>>>()?;

for mut diff in diffs {
let prev = self.content.len_bytes();

diff.range.start = (diff.range.start as i64 + offset) as usize;
diff.range.end = (diff.range.end as i64 + offset) as usize;
diff.offset(offset);

if self.interactive {
diff.print(&self.content);
Expand All @@ -112,7 +111,7 @@ impl File {
}

self.content.apply(diff.clone());
offset += self.content.len_bytes() as i64 - prev as i64;
offset += self.content.len_bytes() as isize - prev as isize;
}

Ok(())
Expand Down
20 changes: 20 additions & 0 deletions src/grapheme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::common::*;

pub(crate) fn byte_index_to_grapheme_index(
s: &str,
byte_index: usize,
) -> usize {
s.grapheme_indices(true)
.take_while(|(i, _)| *i < byte_index)
.count()
}

pub(crate) fn grapheme_index_to_byte_index(
s: &str,
grapheme_index: usize,
) -> usize {
s.grapheme_indices(true)
.nth(grapheme_index)
.map(|(i, _)| i)
.unwrap_or(s.len())
}
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod common;
mod diff;
mod error;
mod file;
mod grapheme;
mod lexer;
mod parser;
mod position;
Expand All @@ -34,8 +35,14 @@ pub use crate::{diff::Diff, error::Error, file::File};

// Public only to crate
pub(crate) use crate::{
codeblock::Codeblock, command::Command, lexer::Lexer, parser::Parser,
position::Position, prompt::prompt, rope_ext::RopeExt,
codeblock::Codeblock,
command::Command,
grapheme::{byte_index_to_grapheme_index, grapheme_index_to_byte_index},
lexer::Lexer,
parser::Parser,
position::Position,
prompt::prompt,
rope_ext::RopeExt,
};

/// Present's internal result type
Expand Down
6 changes: 4 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{common::*, rope_ext::*, Codeblock, Command, Position, Result};
use unicode_segmentation::UnicodeSegmentation;
use crate::{
byte_index_to_grapheme_index, common::*, grapheme_index_to_byte_index,
Codeblock, Command, Position, Result,
};

#[derive(Debug, Clone)]
pub(crate) struct Parser<'a> {
Expand Down
20 changes: 0 additions & 20 deletions src/rope_ext.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{common::*, Diff};
use unicode_segmentation::UnicodeSegmentation;

pub(crate) trait RopeExt {
fn apply(&mut self, diff: Diff);
Expand All @@ -23,22 +22,3 @@ impl RopeExt for Rope {
clone
}
}

pub(crate) fn byte_index_to_grapheme_index(
s: &str,
byte_index: usize,
) -> usize {
s.grapheme_indices(true)
.take_while(|(i, _)| *i < byte_index)
.count()
}

pub(crate) fn grapheme_index_to_byte_index(
s: &str,
grapheme_index: usize,
) -> usize {
s.grapheme_indices(true)
.nth(grapheme_index)
.map(|(i, _)| i)
.unwrap_or(s.len())
}

0 comments on commit de8d439

Please sign in to comment.