working autocomplete now, with backwards deprecation

This commit is contained in:
Priec
2025-07-31 22:44:21 +02:00
parent c594c35b37
commit 8f99aa79ec
5 changed files with 59 additions and 45 deletions

1
Cargo.lock generated
View File

@@ -475,6 +475,7 @@ name = "canvas"
version = "0.4.2"
dependencies = [
"anyhow",
"async-trait",
"common",
"crossterm",
"ratatui",

View File

@@ -22,6 +22,7 @@ thiserror = { workspace = true }
tracing = "0.1.41"
tracing-subscriber = "0.3.19"
async-trait = { workspace = true, optional = true }
[dev-dependencies]
tokio-test = "0.4.4"
@@ -29,7 +30,7 @@ tokio-test = "0.4.4"
[features]
default = []
gui = ["ratatui"]
autocomplete = ["tokio"]
autocomplete = ["tokio", "async-trait"]
[[example]]
name = "autocomplete"

View File

@@ -33,6 +33,9 @@ use canvas::{
CanvasAction,
};
// Add the async_trait import
use async_trait::async_trait;
// Simple theme implementation
#[derive(Clone)]
struct DemoTheme;
@@ -146,6 +149,8 @@ impl CanvasState for AutocompleteFormState {
}
}
// Add the #[async_trait] attribute to the implementation
#[async_trait]
impl AutocompleteCanvasState for AutocompleteFormState {
type SuggestionData = EmailSuggestion;

View File

@@ -13,7 +13,7 @@ use anyhow::Result;
/// ```rust
/// execute_with_autocomplete(action, &mut state).await?;
/// ```
pub async fn execute_with_autocomplete<S: CanvasState + AutocompleteCanvasState>(
pub async fn execute_with_autocomplete<S: CanvasState + AutocompleteCanvasState + Send>(
action: CanvasAction,
state: &mut S,
) -> Result<ActionResult> {
@@ -112,7 +112,7 @@ pub async fn execute_with_autocomplete<S: CanvasState + AutocompleteCanvasState>
/// None
/// }
/// ```
pub fn handle_autocomplete_feature_action<S: CanvasState + AutocompleteCanvasState>(
pub fn handle_autocomplete_feature_action<S: CanvasState + AutocompleteCanvasState + Send>(
action: &CanvasAction,
state: &S,
) -> Option<String> {
@@ -160,7 +160,7 @@ pub fn handle_autocomplete_feature_action<S: CanvasState + AutocompleteCanvasSta
/// Legacy compatibility function - kept for backward compatibility
/// This is the old function signature, now it just wraps the new system
#[deprecated(note = "Use execute_with_autocomplete instead")]
pub async fn execute_canvas_action_with_autocomplete<S: CanvasState + AutocompleteCanvasState>(
pub async fn execute_canvas_action_with_autocomplete<S: CanvasState + AutocompleteCanvasState + Send>(
action: CanvasAction,
state: &mut S,
_ideal_cursor_column: &mut usize, // Ignored - new system manages this internally

View File

@@ -1,6 +1,7 @@
// src/autocomplete/state.rs
use crate::canvas::state::CanvasState;
use async_trait::async_trait;
/// OPTIONAL extension trait for states that want rich autocomplete functionality.
/// Only implement this if you need the new autocomplete features.
@@ -12,6 +13,7 @@ use crate::canvas::state::CanvasState;
/// 4. You implement async fetching logic in that method
/// 5. You call set_autocomplete_suggestions() with results
/// 6. Library manages UI state and navigation
#[async_trait]
pub trait AutocompleteCanvasState: CanvasState {
/// Associated type for suggestion data (e.g., Hit, String, CustomType)
type SuggestionData: Clone + Send + 'static;
@@ -103,6 +105,10 @@ pub trait AutocompleteCanvasState: CanvasState {
///
/// # Example Implementation:
/// ```rust
/// #[async_trait]
/// impl AutocompleteCanvasState for MyState {
/// type SuggestionData = MyData;
///
/// async fn trigger_autocomplete_suggestions(&mut self) {
/// self.activate_autocomplete(); // Show loading state
///
@@ -111,6 +117,7 @@ pub trait AutocompleteCanvasState: CanvasState {
///
/// self.set_autocomplete_suggestions(suggestions);
/// }
/// }
/// ```
async fn trigger_autocomplete_suggestions(&mut self) {
// Activate autocomplete UI