project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use std::{char, sync::Arc}; |
| 2 | |
| 3 | use itertools::Itertools; |
| 4 | use rustc_hash::FxHashMap; |
| 5 | |
| 6 | use crate::diff::DiffHandler; |
| 7 | |
| 8 | use super::TextHandler; |
| 9 | |
| 10 | pub(super) struct DiffHook<'a> { |
| 11 | text: &'a TextHandler, |
| 12 | new: &'a [u32], |
| 13 | last_old_index: usize, |
| 14 | current_index: usize, |
| 15 | } |
| 16 | |
| 17 | impl<'a> DiffHook<'a> { |
| 18 | pub(crate) fn new(text: &'a TextHandler, new: &'a [u32]) -> Self { |
| 19 | Self { |
| 20 | text, |
| 21 | new, |
| 22 | last_old_index: 0, |
| 23 | current_index: 0, |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl DiffHandler for DiffHook<'_> { |
| 29 | fn insert(&mut self, old_index: usize, new_index: usize, new_len: usize) { |
| 30 | if old_index > self.last_old_index { |
| 31 | self.current_index += old_index - self.last_old_index; |
| 32 | self.last_old_index = old_index; |
| 33 | } |
| 34 | |
| 35 | self.text |
| 36 | .insert_unicode( |
| 37 | self.current_index, |
| 38 | &self.new[new_index..new_index + new_len] |
| 39 | .iter() |
| 40 | .map(|x| char::from_u32(*x).unwrap()) |
| 41 | .collect::<String>(), |
| 42 | ) |
| 43 | .unwrap(); |
| 44 | self.current_index += new_len; |
| 45 | } |
| 46 | |
| 47 | fn delete(&mut self, old_index: usize, old_len: usize) { |
| 48 | self.current_index += old_index - self.last_old_index; |
| 49 | self.text |
| 50 | .delete_unicode(self.current_index, old_len) |
| 51 | .unwrap(); |
| 52 | self.last_old_index = old_index + old_len; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | pub(super) struct DiffHookForLine<'a> { |
| 57 | text: &'a TextHandler, |
| 58 | old: Vec<u32>, |
| 59 | new: Vec<u32>, |
| 60 | lines: Vec<Arc<str>>, |
| 61 | lines_lookup: FxHashMap<Arc<str>, usize>, |
| 62 | |
| 63 | last_old_index: usize, |
| 64 | current_index: usize, |
| 65 | } |
| 66 | |
| 67 | impl<'a> DiffHookForLine<'a> { |
| 68 | pub(crate) fn new(text: &'a TextHandler, new_str: &str) -> Self { |
| 69 | let mut this = Self { |
| 70 | text, |
| 71 | old: Vec::new(), |
| 72 | new: Vec::new(), |
| 73 | lines: Vec::new(), |
| 74 | lines_lookup: FxHashMap::default(), |
| 75 | last_old_index: 0, |
| 76 | current_index: 0, |
| 77 | }; |
| 78 | |
| 79 | let text_str = text.to_string(); |
| 80 | for line in text_str.split_inclusive('\n') { |
| 81 | let line: Arc<str> = Arc::from(line); |
| 82 | let id = this.register_line(line); |
| 83 | this.old.push(id as u32); |
| 84 | } |
| 85 | |
| 86 | for line in new_str.split_inclusive('\n') { |
| 87 | let line: Arc<str> = Arc::from(line); |
| 88 | let id = this.register_line(line); |
| 89 | this.new.push(id as u32); |
| 90 | } |
| 91 | |
| 92 | this |
| 93 | } |
| 94 | |
| 95 | fn register_line(&mut self, line: Arc<str>) -> usize { |
| 96 | if let Some(&index) = self.lines_lookup.get(&line) { |
| 97 | return index; |
| 98 | } |
| 99 | |
| 100 | self.lines.push(line.clone()); |
| 101 | self.lines_lookup.insert(line, self.lines.len() - 1); |
| 102 | self.lines.len() - 1 |
| 103 | } |
| 104 | |
| 105 | pub fn get_old_arr(&self) -> &[u32] { |
| 106 | &self.old |
| 107 | } |
| 108 | |
| 109 | pub fn get_new_arr(&self) -> &[u32] { |
| 110 | &self.new |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl DiffHandler for DiffHookForLine<'_> { |
| 115 | fn insert(&mut self, old_index: usize, new_index: usize, new_len: usize) { |
| 116 | if self.last_old_index < old_index { |
| 117 | assert!(self.last_old_index < old_index); |
| 118 | self.current_index += (self.last_old_index..old_index) |
| 119 | .map(|x| self.lines[self.old[x] as usize].chars().count()) |
| 120 | .sum::<usize>(); |
| 121 | self.last_old_index = old_index; |
| 122 | } |
| 123 | |
| 124 | let s = self.new[new_index..new_index + new_len] |
| 125 | .iter() |
| 126 | .map(|x| self.lines[*x as usize].clone()) |
| 127 | .join(""); |
| 128 | self.text.insert_unicode(self.current_index, &s).unwrap(); |
| 129 | self.current_index += s.chars().count(); |
| 130 | } |
| 131 | |
| 132 | fn delete(&mut self, old_index: usize, old_len: usize) { |
| 133 | if self.last_old_index != old_index { |
| 134 | assert!(self.last_old_index < old_index); |
| 135 | self.current_index += (self.last_old_index..old_index) |
| 136 | .map(|x| self.lines[self.old[x] as usize].chars().count()) |
| 137 | .sum::<usize>(); |
| 138 | } |
| 139 | |
| 140 | self.last_old_index = old_index + old_len; |
| 141 | let delete_len = (old_index..old_index + old_len) |
| 142 | .map(|x| self.lines[self.old[x] as usize].chars().count()) |
| 143 | .sum::<usize>(); |
| 144 | |
| 145 | self.text |
| 146 | .delete_unicode(self.current_index, delete_len) |
| 147 | .unwrap(); |
| 148 | } |
| 149 | } |