autocomplete is now perfectc

This commit is contained in:
filipriec
2025-06-16 10:52:28 +02:00
parent 512e7fb9e7
commit cccf029464
3 changed files with 598 additions and 595 deletions

View File

@@ -3,7 +3,7 @@ use crate::config::binds::config::Config;
use crate::functions::modes::edit::{
add_logic_e, add_table_e, auth_e, form_e,
};
use crate::modes::handlers::event::EventOutcome;
use crate::modes::handlers::event::EventHandler;
use crate::services::grpc_client::GrpcClient;
use crate::state::app::state::AppState;
use crate::state::pages::admin::AdminState;
@@ -14,8 +14,9 @@ use crate::state::pages::{
};
use anyhow::Result;
use common::proto::multieko2::search::search_response::Hit;
use crossterm::event::KeyEvent;
use tracing::debug;
use crossterm::event::{KeyCode, KeyEvent};
use tokio::sync::mpsc;
use tracing::{debug, info};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EditEventOutcome {
@@ -23,6 +24,58 @@ pub enum EditEventOutcome {
ExitEditMode,
}
/// Helper function to spawn a non-blocking search task for autocomplete.
async fn trigger_form_autocomplete_search(
form_state: &mut FormState,
grpc_client: &mut GrpcClient,
sender: mpsc::UnboundedSender<Vec<Hit>>,
) {
if let Some(field_def) = form_state.fields.get(form_state.current_field) {
if field_def.is_link {
if let Some(target_table) = &field_def.link_target_table {
// 1. Update state for immediate UI feedback
form_state.autocomplete_loading = true;
form_state.autocomplete_active = true;
form_state.autocomplete_suggestions.clear();
form_state.selected_suggestion_index = None;
// 2. Clone everything needed for the background task
let query = form_state.get_current_input().to_string();
let table_to_search = target_table.clone();
let mut grpc_client_clone = grpc_client.clone();
info!(
"[Autocomplete] Spawning search in '{}' for query: '{}'",
table_to_search, query
);
// 3. Spawn the non-blocking task
tokio::spawn(async move {
match grpc_client_clone
.search_table(table_to_search, query)
.await
{
Ok(response) => {
// Send results back through the channel
let _ = sender.send(response.hits);
}
Err(e) => {
tracing::error!(
"[Autocomplete] Search failed: {:?}",
e
);
// Send an empty vec on error so the UI can stop loading
let _ = sender.send(vec![]);
}
}
});
}
}
}
}
#[allow(clippy::too_many_arguments)]
pub async fn handle_edit_event(
key: KeyEvent,
config: &Config,
@@ -30,12 +83,12 @@ pub async fn handle_edit_event(
login_state: &mut LoginState,
register_state: &mut RegisterState,
admin_state: &mut AdminState,
ideal_cursor_column: &mut usize,
current_position: &mut u64,
total_count: u64,
grpc_client: &mut GrpcClient,
event_handler: &mut EventHandler,
app_state: &AppState,
) -> Result<EditEventOutcome> {
// --- AUTOCOMPLETE-SPECIFIC KEY HANDLING ---
if app_state.ui.show_form && form_state.autocomplete_active {
if let Some(action) =
config.get_edit_action_for_key(key.code, key.modifiers)
@@ -81,11 +134,11 @@ pub async fn handle_edit_event(
{
let current_input =
form_state.get_current_input_mut();
// Use the ID from the Hit struct for the field value
*current_input = selection.id.to_string();
let new_cursor_pos = current_input.len();
form_state.set_current_cursor_pos(new_cursor_pos);
*ideal_cursor_column = new_cursor_pos;
// FIX: Access ideal_cursor_column through event_handler
event_handler.ideal_cursor_column = new_cursor_pos;
form_state.deactivate_autocomplete();
form_state.set_has_unsaved_changes(true);
return Ok(EditEventOutcome::Message(
@@ -93,504 +146,180 @@ pub async fn handle_edit_event(
));
}
}
// If no selection, fall through to default behavior
form_state.deactivate_autocomplete();
// Fall through to default 'enter' behavior
}
_ => {} // Other keys are not special, let them fall through
_ => {} // Let other keys fall through to the live search logic
}
}
}
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
&config.keybindings.global,
key.code,
key.modifiers,
) {
return Ok(EditEventOutcome::Message(
"Cannot enter command mode from edit mode here.".to_string(),
));
}
// --- LIVE AUTOCOMPLETE TRIGGER LOGIC ---
let mut trigger_search = false;
if let Some(action) = config
.get_action_for_key_in_mode(
&config.keybindings.common,
key.code,
key.modifiers,
)
.as_deref()
{
if matches!(action, "save" | "revert") {
let message_string: String = if app_state.ui.show_login {
auth_e::execute_common_action(
action,
login_state,
grpc_client,
current_position,
total_count,
)
.await?
} else if app_state.ui.show_register {
auth_e::execute_common_action(
action,
register_state,
grpc_client,
current_position,
total_count,
)
.await?
} else if app_state.ui.show_add_table {
format!(
"Action '{}' not implemented for Add Table in edit mode.",
action
)
} else if app_state.ui.show_add_logic {
format!(
"Action '{}' not implemented for Add Logic in edit mode.",
action
)
} else {
let outcome =
form_e::execute_common_action(action, form_state, grpc_client)
.await?;
match outcome {
EventOutcome::Ok(msg) | EventOutcome::DataSaved(_, msg) => msg,
_ => format!(
"Unexpected outcome from common action: {:?}",
outcome
),
}
};
return Ok(EditEventOutcome::Message(message_string));
}
}
if let Some(action_str) =
config.get_edit_action_for_key(key.code, key.modifiers).as_deref()
{
tracing::info!(
"[Handler] `handle_edit_event` received action: '{}'",
action_str
);
// --- MANUAL AUTOCOMPLETE TRIGGER ---
if action_str == "trigger_autocomplete" {
tracing::info!("[Handler] Action is 'trigger_autocomplete'. Checking conditions...");
if app_state.ui.show_form {
if let Some(field_def) = form_state.fields.get(form_state.current_field) {
if field_def.is_link {
// Use our new field to get the table to search
if let Some(target_table) = &field_def.link_target_table {
tracing::info!(
"[Handler] Field '{}' is a link to table '{}'. Triggering search.",
field_def.display_name,
target_table
);
// Set loading state and activate autocomplete UI
form_state.autocomplete_loading = true;
form_state.autocomplete_active = true;
form_state.autocomplete_suggestions.clear();
form_state.selected_suggestion_index = None;
let query = form_state.get_current_input().to_string();
let table_to_search = target_table.clone();
// Perform the gRPC call asynchronously
match grpc_client.search_table(table_to_search, query).await {
Ok(response) => {
form_state.autocomplete_suggestions = response.hits;
if !form_state.autocomplete_suggestions.is_empty() {
form_state.selected_suggestion_index = Some(0);
}
form_state.autocomplete_loading = false; // Turn off loading
return Ok(EditEventOutcome::Message(format!(
"Found {} suggestions.",
form_state.autocomplete_suggestions.len()
)));
}
Err(e) => {
// Handle errors gracefully
form_state.autocomplete_loading = false;
form_state.deactivate_autocomplete(); // Close UI on error
let error_msg = format!("Search failed: {}", e);
tracing::error!("[Handler] {}", error_msg);
return Ok(EditEventOutcome::Message(error_msg));
}
}
} else {
let msg = "Field is a link, but target table is not defined.".to_string();
tracing::error!("[Handler] {}", msg);
return Ok(EditEventOutcome::Message(msg));
}
} else {
let msg = format!("Field '{}' is not a linkable field.", field_def.display_name);
tracing::error!("[Handler] {}", msg);
return Ok(EditEventOutcome::Message(msg));
}
}
if app_state.ui.show_form {
// Manual trigger
if let Some("trigger_autocomplete") =
config.get_edit_action_for_key(key.code, key.modifiers)
{
if !form_state.autocomplete_active {
trigger_search = true;
}
// Fallback message if not in a form or something went wrong
return Ok(EditEventOutcome::Message("Autocomplete not available here.".to_string()));
}
// --- END OF NEW LOGIC ---
if action_str == "enter_decider" {
let effective_action = if app_state.ui.show_register
&& register_state.in_suggestion_mode
&& register_state.current_field() == 4
{
"select_suggestion"
} else if app_state.ui.show_add_logic
&& admin_state
.add_logic_state
.in_target_column_suggestion_mode
&& admin_state.add_logic_state.current_field() == 1
{
"select_suggestion"
} else {
"next_field"
};
let msg = if app_state.ui.show_login {
auth_e::execute_edit_action(
effective_action,
key,
login_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_table {
add_table_e::execute_edit_action(
effective_action,
key,
&mut admin_state.add_table_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_logic {
add_logic_e::execute_edit_action(
effective_action,
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_register {
auth_e::execute_edit_action(
effective_action,
key,
register_state,
ideal_cursor_column,
)
.await?
} else {
// Live search trigger while typing
else if form_state.autocomplete_active {
if let KeyCode::Char(_) | KeyCode::Backspace = key.code {
let action = if let KeyCode::Backspace = key.code {
"delete_char_backward"
} else {
"insert_char"
};
// FIX: Pass &mut event_handler.ideal_cursor_column
form_e::execute_edit_action(
effective_action,
action,
key,
form_state,
ideal_cursor_column,
&mut event_handler.ideal_cursor_column,
)
.await?
};
.await?;
trigger_search = true;
}
}
}
if trigger_search {
trigger_form_autocomplete_search(
form_state,
&mut event_handler.grpc_client,
event_handler.autocomplete_result_sender.clone(),
)
.await;
return Ok(EditEventOutcome::Message("Searching...".to_string()));
}
// --- GENERAL EDIT MODE EVENT HANDLING (IF NOT AUTOCOMPLETE) ---
if let Some(action_str) =
config.get_edit_action_for_key(key.code, key.modifiers)
{
// Handle Enter key (next field)
if action_str == "enter_decider" {
// FIX: Pass &mut event_handler.ideal_cursor_column
let msg = form_e::execute_edit_action(
"next_field",
key,
form_state,
&mut event_handler.ideal_cursor_column,
)
.await?;
return Ok(EditEventOutcome::Message(msg));
}
// Handle exiting edit mode
if action_str == "exit" {
if app_state.ui.show_register && register_state.in_suggestion_mode {
let msg = auth_e::execute_edit_action(
"exit_suggestion_mode",
key,
register_state,
ideal_cursor_column,
)
.await?;
return Ok(EditEventOutcome::Message(msg));
} else if app_state.ui.show_add_logic
&& admin_state
.add_logic_state
.in_target_column_suggestion_mode
{
admin_state
.add_logic_state
.in_target_column_suggestion_mode = false;
admin_state
.add_logic_state
.show_target_column_suggestions = false;
admin_state
.add_logic_state
.selected_target_column_suggestion_index = None;
return Ok(EditEventOutcome::Message(
"Exited column suggestions".to_string(),
));
} else {
return Ok(EditEventOutcome::ExitEditMode);
}
}
if app_state.ui.show_add_logic
&& admin_state.add_logic_state.current_field() == 1
{
if action_str == "suggestion_down" {
if !admin_state
.add_logic_state
.in_target_column_suggestion_mode
{
if let Some(profile_name) =
admin_state.add_logic_state.profile_name.clone().into()
{
if let Some(table_name) = admin_state
.add_logic_state
.selected_table_name
.clone()
{
debug!("Fetching table structure for autocomplete: Profile='{}', Table='{}'", profile_name, table_name);
match grpc_client
.get_table_structure(profile_name, table_name)
.await
{
Ok(ts_response) => {
admin_state
.add_logic_state
.table_columns_for_suggestions =
ts_response
.columns
.into_iter()
.map(|c| c.name)
.collect();
admin_state
.add_logic_state
.update_target_column_suggestions();
if !admin_state
.add_logic_state
.target_column_suggestions
.is_empty()
{
admin_state
.add_logic_state
.in_target_column_suggestion_mode =
true;
return Ok(EditEventOutcome::Message(
"Column suggestions shown"
.to_string(),
));
} else {
return Ok(EditEventOutcome::Message(
"No column suggestions for current input"
.to_string(),
));
}
}
Err(e) => {
debug!(
"Error fetching table structure: {}",
e
);
admin_state
.add_logic_state
.table_columns_for_suggestions
.clear();
admin_state
.add_logic_state
.update_target_column_suggestions();
return Ok(EditEventOutcome::Message(
format!("Error fetching columns: {}", e),
));
}
}
} else {
return Ok(EditEventOutcome::Message(
"No table selected for column suggestions"
.to_string(),
));
}
} else {
return Ok(EditEventOutcome::Message(
"Profile name missing for column suggestions"
.to_string(),
));
}
} else {
let msg = add_logic_e::execute_edit_action(
action_str,
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?;
return Ok(EditEventOutcome::Message(msg));
}
} else if admin_state
.add_logic_state
.in_target_column_suggestion_mode
&& action_str == "suggestion_up"
{
let msg = add_logic_e::execute_edit_action(
action_str,
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?;
return Ok(EditEventOutcome::Message(msg));
}
}
if app_state.ui.show_register && register_state.current_field() == 4 {
if !register_state.in_suggestion_mode
&& action_str == "suggestion_down"
{
register_state.update_role_suggestions();
if !register_state.role_suggestions.is_empty() {
register_state.in_suggestion_mode = true;
return Ok(EditEventOutcome::Message(
"Role suggestions shown".to_string(),
));
}
}
if register_state.in_suggestion_mode
&& matches!(action_str, "suggestion_down" | "suggestion_up")
{
let msg = auth_e::execute_edit_action(
action_str,
key,
register_state,
ideal_cursor_column,
)
.await?;
return Ok(EditEventOutcome::Message(msg));
}
return Ok(EditEventOutcome::ExitEditMode);
}
// Handle all other edit actions
let msg = if app_state.ui.show_login {
// FIX: Pass &mut event_handler.ideal_cursor_column
auth_e::execute_edit_action(
action_str,
key,
login_state,
ideal_cursor_column,
&mut event_handler.ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_table {
// FIX: Pass &mut event_handler.ideal_cursor_column
add_table_e::execute_edit_action(
action_str,
key,
&mut admin_state.add_table_state,
ideal_cursor_column,
&mut event_handler.ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_logic {
if !(admin_state
.add_logic_state
.in_target_column_suggestion_mode
&& matches!(action_str, "suggestion_down" | "suggestion_up"))
{
add_logic_e::execute_edit_action(
action_str,
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?
} else {
String::new()
}
// FIX: Pass &mut event_handler.ideal_cursor_column
add_logic_e::execute_edit_action(
action_str,
key,
&mut admin_state.add_logic_state,
&mut event_handler.ideal_cursor_column,
)
.await?
} else if app_state.ui.show_register {
if !(register_state.in_suggestion_mode
&& matches!(action_str, "suggestion_down" | "suggestion_up"))
{
auth_e::execute_edit_action(
action_str,
key,
register_state,
ideal_cursor_column,
)
.await?
} else {
String::new()
}
// FIX: Pass &mut event_handler.ideal_cursor_column
auth_e::execute_edit_action(
action_str,
key,
register_state,
&mut event_handler.ideal_cursor_column,
)
.await?
} else {
// FIX: Pass &mut event_handler.ideal_cursor_column
form_e::execute_edit_action(
action_str,
key,
form_state,
ideal_cursor_column,
&mut event_handler.ideal_cursor_column,
)
.await?
};
return Ok(EditEventOutcome::Message(msg));
}
let mut exited_suggestion_mode_for_typing = false;
if app_state.ui.show_register && register_state.in_suggestion_mode {
register_state.in_suggestion_mode = false;
register_state.show_role_suggestions = false;
register_state.selected_suggestion_index = None;
exited_suggestion_mode_for_typing = true;
}
if app_state.ui.show_add_logic
&& admin_state.add_logic_state.in_target_column_suggestion_mode
{
admin_state.add_logic_state.in_target_column_suggestion_mode = false;
admin_state.add_logic_state.show_target_column_suggestions = false;
admin_state
.add_logic_state
.selected_target_column_suggestion_index = None;
exited_suggestion_mode_for_typing = true;
// --- FALLBACK FOR CHARACTER INSERTION (IF NO OTHER BINDING MATCHED) ---
if let KeyCode::Char(_) = key.code {
let msg = if app_state.ui.show_login {
// FIX: Pass &mut event_handler.ideal_cursor_column
auth_e::execute_edit_action(
"insert_char",
key,
login_state,
&mut event_handler.ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_table {
// FIX: Pass &mut event_handler.ideal_cursor_column
add_table_e::execute_edit_action(
"insert_char",
key,
&mut admin_state.add_table_state,
&mut event_handler.ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_logic {
// FIX: Pass &mut event_handler.ideal_cursor_column
add_logic_e::execute_edit_action(
"insert_char",
key,
&mut admin_state.add_logic_state,
&mut event_handler.ideal_cursor_column,
)
.await?
} else if app_state.ui.show_register {
// FIX: Pass &mut event_handler.ideal_cursor_column
auth_e::execute_edit_action(
"insert_char",
key,
register_state,
&mut event_handler.ideal_cursor_column,
)
.await?
} else {
// FIX: Pass &mut event_handler.ideal_cursor_column
form_e::execute_edit_action(
"insert_char",
key,
form_state,
&mut event_handler.ideal_cursor_column,
)
.await?
};
return Ok(EditEventOutcome::Message(msg));
}
let mut char_insert_msg = if app_state.ui.show_login {
auth_e::execute_edit_action(
"insert_char",
key,
login_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_table {
add_table_e::execute_edit_action(
"insert_char",
key,
&mut admin_state.add_table_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_logic {
add_logic_e::execute_edit_action(
"insert_char",
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_register {
auth_e::execute_edit_action(
"insert_char",
key,
register_state,
ideal_cursor_column,
)
.await?
} else {
form_e::execute_edit_action(
"insert_char",
key,
form_state,
ideal_cursor_column,
)
.await?
};
if app_state.ui.show_register && register_state.current_field() == 4 {
register_state.update_role_suggestions();
}
if app_state.ui.show_add_logic
&& admin_state.add_logic_state.current_field() == 1
{
admin_state.add_logic_state.update_target_column_suggestions();
}
if exited_suggestion_mode_for_typing && char_insert_msg.is_empty() {
char_insert_msg = "Suggestions hidden".to_string();
}
Ok(EditEventOutcome::Message(char_insert_msg))
Ok(EditEventOutcome::Message(String::new())) // No action taken
}