Find the word boundaries (start, end) at the given character position. Used for double-click word selection.
(text: &str, pos: usize)
| 1786 | /// Find the word boundaries (start, end) at the given character position. |
| 1787 | /// Used for double-click word selection. |
| 1788 | pub fn find_word_at(text: &str, pos: usize) -> (usize, usize) { |
| 1789 | let chars: Vec<char> = text.chars().collect(); |
| 1790 | let len = chars.len(); |
| 1791 | if len == 0 || pos >= len { |
| 1792 | return (pos, pos); |
| 1793 | } |
| 1794 | let is_word_char = |c: char| !c.is_whitespace(); |
| 1795 | if !is_word_char(chars[pos]) { |
| 1796 | // On whitespace — select the whitespace run |
| 1797 | let mut start = pos; |
| 1798 | while start > 0 && !is_word_char(chars[start - 1]) { |
| 1799 | start -= 1; |
| 1800 | } |
| 1801 | let mut end = pos; |
| 1802 | while end < len && !is_word_char(chars[end]) { |
| 1803 | end += 1; |
| 1804 | } |
| 1805 | return (start, end); |
| 1806 | } |
| 1807 | // On a word char — find word boundaries |
| 1808 | let mut start = pos; |
| 1809 | while start > 0 && is_word_char(chars[start - 1]) { |
| 1810 | start -= 1; |
| 1811 | } |
| 1812 | let mut end = pos; |
| 1813 | while end < len && is_word_char(chars[end]) { |
| 1814 | end += 1; |
| 1815 | } |
| 1816 | (start, end) |
| 1817 | } |
| 1818 | |
| 1819 | /// Build the display text for rendering. |
| 1820 | /// Returns the string that should be measured/drawn. |
no outgoing calls
no test coverage detected