| 1 | use std::ops::Range; |
| 2 | |
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 4 | pub(crate) enum CharType { |
| 5 | /// a-z, A-Z, 0-9, _ |
| 6 | Word, |
| 7 | /// '\t', ' ', '\u{00A0}' etc. |
| 8 | Whitespace, |
| 9 | /// \n, \r |
| 10 | Newline, |
| 11 | /// . , ; : ( ) [ ] { } ... or CJK characters: `汉`, `🎉` etc. |
| 12 | Other, |
| 13 | } |
| 14 | |
| 15 | /// Implementation based on <https://github.com/zed-industries/zed/blob/main/crates/gpui/src/text_system/line_wrapper.rs> |
| 16 | fn is_word_char(c: char) -> bool { |
| 17 | matches!(c, '_') |
| 18 | // ASCII alphanumeric characters, for English, numbers: `Hello123`, etc. |
| 19 | || c.is_ascii_alphanumeric() |
| 20 | // Latin script in Unicode for French, German, Spanish, etc. |
| 21 | || matches!(c, '\u{00C0}'..='\u{00FF}') |
| 22 | || matches!(c, '\u{0100}'..='\u{017F}') |
| 23 | || matches!(c, '\u{0180}'..='\u{024F}') |
| 24 | // Cyrillic for Russian, Ukrainian, etc. |
| 25 | || matches!(c, '\u{0400}'..='\u{04FF}') |
| 26 | // Vietnamese |
| 27 | || matches!(c, '\u{1E00}'..='\u{1EFF}') |
| 28 | || matches!(c, '\u{0300}'..='\u{036F}') |
| 29 | } |
| 30 | |
| 31 | impl From<char> for CharType { |
| 32 | fn from(c: char) -> Self { |
| 33 | match c { |
| 34 | c if is_word_char(c) => CharType::Word, |
| 35 | c if c == '\n' || c == '\r' => CharType::Newline, |
| 36 | c if c.is_whitespace() => CharType::Whitespace, |
| 37 | _ => CharType::Other, |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl CharType { |
| 43 | fn is_connectable(self, c: char) -> bool { |
| 44 | matches!( |
| 45 | (self, CharType::from(c)), |
| 46 | (CharType::Word, CharType::Word) | (CharType::Whitespace, CharType::Whitespace) |
| 47 | ) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub(crate) fn word_range_from_chars( |
| 52 | offset: usize, |
| 53 | c: char, |
| 54 | prev_chars: impl Iterator<Item = char>, |
| 55 | next_chars: impl Iterator<Item = char>, |
| 56 | ) -> Range<usize> { |
| 57 | let char_type = CharType::from(c); |
| 58 | let mut start = offset; |
| 59 | let mut end = offset + c.len_utf8(); |
| 60 | |
| 61 | for prev in prev_chars.take(128) { |
| 62 | if char_type.is_connectable(prev) { |
| 63 | start -= prev.len_utf8(); |
| 64 | } else { |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | for next in next_chars.take(128) { |
| 70 | if char_type.is_connectable(next) { |
| 71 | end += next.len_utf8(); |
| 72 | } else { |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | start..end |
| 78 | } |
| 79 | |
| 80 | pub(crate) fn word_range_at(text: &str, offset: usize) -> Option<Range<usize>> { |
| 81 | if text.is_empty() { |
| 82 | return None; |
| 83 | } |
| 84 | |
| 85 | let offset = clip_offset(text, offset); |
| 86 | let c = text[offset..].chars().next()?; |
| 87 | Some(word_range_from_chars( |
| 88 | offset, |
| 89 | c, |
| 90 | text[..offset].chars().rev(), |
| 91 | text[offset + c.len_utf8()..].chars(), |
| 92 | )) |
| 93 | } |
| 94 | |
| 95 | fn clip_offset(text: &str, offset: usize) -> usize { |
| 96 | let offset = offset.min(text.len()); |
| 97 | if offset == text.len() { |
| 98 | return text.char_indices().next_back().map_or(0, |(ix, _)| ix); |
| 99 | } |
| 100 | |
| 101 | if text.is_char_boundary(offset) { |
| 102 | offset |
| 103 | } else { |
| 104 | text.char_indices() |
| 105 | .map(|(ix, _)| ix) |
| 106 | .take_while(|ix| *ix < offset) |
| 107 | .last() |
| 108 | .unwrap_or_default() |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #[cfg(test)] |
| 113 | mod tests { |
| 114 | use super::*; |
| 115 | |
| 116 | #[test] |
| 117 | fn test_char_type_from_char() { |
| 118 | assert_eq!(CharType::from('a'), CharType::Word); |
| 119 | assert_eq!(CharType::from('Z'), CharType::Word); |
| 120 | assert_eq!(CharType::from('0'), CharType::Word); |
| 121 | assert_eq!(CharType::from('_'), CharType::Word); |
| 122 | assert_eq!(CharType::from('.'), CharType::Other); |
| 123 | assert_eq!(CharType::from(','), CharType::Other); |
| 124 | assert_eq!(CharType::from(';'), CharType::Other); |
| 125 | assert_eq!(CharType::from('!'), CharType::Other); |
| 126 | assert_eq!(CharType::from('?'), CharType::Other); |
| 127 | assert_eq!(CharType::from('['), CharType::Other); |
| 128 | assert_eq!(CharType::from('{'), CharType::Other); |
| 129 | assert_eq!(CharType::from(' '), CharType::Whitespace); |
| 130 | assert_eq!(CharType::from('\t'), CharType::Whitespace); |
| 131 | assert_eq!(CharType::from('\u{00A0}'), CharType::Whitespace); |
| 132 | assert_eq!(CharType::from('\n'), CharType::Newline); |
| 133 | assert_eq!(CharType::from('\r'), CharType::Newline); |
| 134 | assert_eq!(CharType::from('汉'), CharType::Other); |
| 135 | assert_eq!(CharType::from('é'), CharType::Word); |
| 136 | assert_eq!(CharType::from('ä'), CharType::Word); |
| 137 | assert_eq!(CharType::from('ö'), CharType::Word); |
| 138 | assert_eq!(CharType::from('ü'), CharType::Word); |
| 139 | assert_eq!(CharType::from('д'), CharType::Word); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn test_word_range_at() { |
| 144 | let text = |
| 145 | "test text\nabcde 中文🎉 test\nhello[()]\ntest_connector ____\nRope\nrök\ngrande île"; |
| 146 | let tests = [ |
| 147 | (0, Some("test")), |
| 148 | (4, Some(" ")), |
| 149 | (10, Some("abcde")), |
| 150 | (15, Some(" ")), |
| 151 | (16, Some("中")), |
| 152 | (19, Some("文")), |
| 153 | (22, Some("🎉")), |
| 154 | (27, Some("test")), |
| 155 | (37, Some("[")), |
| 156 | (38, Some("(")), |
| 157 | (39, Some(")")), |
| 158 | (40, Some("]")), |
| 159 | (42, Some("test_connector")), |
| 160 | (56, Some(" ")), |
| 161 | (57, Some("____")), |
| 162 | (62, Some("Rope")), |
| 163 | (67, Some("rök")), |
| 164 | (79, Some("île")), |
| 165 | ]; |
| 166 | |
| 167 | for (offset, expected) in tests { |
| 168 | let actual = word_range_at(text, offset).map(|range| text[range].to_string()); |
| 169 | assert_eq!(actual.as_deref(), expected, "offset {offset}"); |
| 170 | } |
| 171 | } |
| 172 | } |