Skip to content

Commit

Permalink
text view: Input rework III
Browse files Browse the repository at this point in the history
  • Loading branch information
bragefuglseth committed Jun 20, 2024
1 parent 46e9d53 commit 60b76e7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/text_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ALIASES: &'static [(&'static str, &'static str)] = &[

// The largest grapheme count of any current alias, manually kept track of for performance reasons.
// If this is too large, nothing will happen. If it's too small, larger aliases won't be recognized.
const ALIAS_MAX_SIZE: usize = 5;
const ALIAS_MAX_SIZE: usize = 2;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GraphemeState {
Expand Down Expand Up @@ -204,15 +204,14 @@ pub fn line_offset_with_replacements(
typed: &str,
unfinished_letter_length: usize,
) -> (usize, usize) {
let grapheme_idx = if let Some((_, potential_alias, _)) = end_alias(original, typed) {
typed[..typed.len() - potential_alias.len()]
.graphemes(true)
.count()
let typed_graphemes = typed.graphemes(true).count();

let grapheme_idx = if let Some((letter, potential_alias, _)) = end_alias(original, typed) {
typed_graphemes - potential_alias.graphemes(true).count() + letter.graphemes(true).count()
} else if unfinished_letter_length > 0 {
println!("a");
typed.graphemes(true).count() + unfinished_letter_length
typed_graphemes + unfinished_letter_length
} else {
typed.graphemes(true).count()
typed_graphemes
};

let line_num = original
Expand Down
5 changes: 4 additions & 1 deletion src/widgets/text_view/caret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ impl imp::KpTextView {
let original = self.original_text.borrow();
let typed = self.typed_text.borrow();

let input_context = self.input_context.borrow();
let (preedit, _, _) = input_context.as_ref().unwrap().preedit_string();

let (caret_line, caret_idx) =
line_offset_with_replacements(&original, &typed, 0);
line_offset_with_replacements(&original, &typed, preedit.graphemes(true).count());

let text_view = self.text_view.get();
let buf = text_view.buffer();
Expand Down

0 comments on commit 60b76e7

Please sign in to comment.