working autocomplete now, with backwards deprecation
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -475,6 +475,7 @@ name = "canvas"
|
||||
version = "0.4.2"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"common",
|
||||
"crossterm",
|
||||
"ratatui",
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,13 +105,18 @@ pub trait AutocompleteCanvasState: CanvasState {
|
||||
///
|
||||
/// # Example Implementation:
|
||||
/// ```rust
|
||||
/// async fn trigger_autocomplete_suggestions(&mut self) {
|
||||
/// self.activate_autocomplete(); // Show loading state
|
||||
/// #[async_trait]
|
||||
/// impl AutocompleteCanvasState for MyState {
|
||||
/// type SuggestionData = MyData;
|
||||
///
|
||||
/// let query = self.get_current_input().to_string();
|
||||
/// let suggestions = my_api.search(&query).await.unwrap_or_default();
|
||||
/// async fn trigger_autocomplete_suggestions(&mut self) {
|
||||
/// self.activate_autocomplete(); // Show loading state
|
||||
///
|
||||
/// self.set_autocomplete_suggestions(suggestions);
|
||||
/// let query = self.get_current_input().to_string();
|
||||
/// let suggestions = my_api.search(&query).await.unwrap_or_default();
|
||||
///
|
||||
/// self.set_autocomplete_suggestions(suggestions);
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
async fn trigger_autocomplete_suggestions(&mut self) {
|
||||
|
||||
Reference in New Issue
Block a user