getWordAtCaret function
Implementation
WordInfo getWordAtCaret(String text, int caret, [String separator = ' ']) {
if (caret < 0 || caret > text.length) {
throw RangeError('Caret position is out of bounds.');
}
// Find the start of the word
int start = caret;
while (start > 0 && !separator.contains(text[start - 1])) {
start--;
}
// Find the end of the word
int end = caret;
while (end < text.length && !separator.contains(text[end])) {
end++;
}
// Return the start index and the word at the caret position
String word = text.substring(start, end);
return (start, word);
}