use crate::tui::{pickers::text_picker_with_suggestion::Suggestion, widgets::input::Input};
use super::extraction::is_separator;
#[derive(Debug, Clone)]
pub struct SqlSuggestion {
text: String,
}
impl SqlSuggestion {
pub fn new(text: String) -> Self {
Self { text }
}
}
impl Suggestion for SqlSuggestion {
fn title(&self) -> &str {
&self.text
}
fn apply_to(&self, input: &mut Input) {
let cursor = input.cursor();
let value = input.value().to_owned();
let before_cursor = &value[..cursor];
let at_cursor = value[cursor..].chars().next();
let token_start = before_cursor
.char_indices()
.rev()
.find(|(_, character)| is_separator(*character))
.map(|(index, character)| index + character.len_utf8())
.unwrap_or(0);
let token_character_length = before_cursor[token_start..].chars().count();
for _ in 0..token_character_length {
input.delete_prev();
}
for character in self.text.chars() {
input.insert(character);
}
if !at_cursor.is_some_and(|character| character.is_whitespace()) {
input.insert(' ');
}
}
}