#![allow(dead_code)]
pub mod fuzzy_file;
pub mod path;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompletionKind {
SlashCommand,
SlashArgument {
command: String,
},
FilePath,
FuzzyFile {
query: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompletionItem {
pub text: String,
pub label: String,
pub description: Option<String>,
pub kind: CompletionKind,
}
impl std::fmt::Display for CompletionItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.description {
Some(desc) => write!(f, "{} — {}", self.label, desc),
None => write!(f, "{}", self.label),
}
}
}
pub struct CompletionManager {
cwd: std::path::PathBuf,
}
impl CompletionManager {
pub fn new(cwd: std::path::PathBuf) -> Self {
CompletionManager { cwd }
}
pub fn get_completions(&self, input: &str) -> Vec<CompletionItem> {
if input.starts_with("./") || input.starts_with("../") || input.starts_with("~") {
return path::complete_path(input, &self.cwd);
}
if input.contains('/') && !input.starts_with('/') {
return path::complete_path(input, &self.cwd);
}
Vec::new()
}
pub async fn fuzzy_search(&self, query: &str) -> Vec<CompletionItem> {
fuzzy_file::fuzzy_file_search(query, &self.cwd).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_completion_item_display() {
let item = CompletionItem {
text: "src/main.rs".to_string(),
label: "src/main.rs".to_string(),
description: Some("Rust source file".to_string()),
kind: CompletionKind::FilePath,
};
assert_eq!(format!("{}", item), "src/main.rs — Rust source file");
}
#[test]
fn test_completion_item_no_desc() {
let item = CompletionItem {
text: "/model".to_string(),
label: "/model".to_string(),
description: None,
kind: CompletionKind::SlashCommand,
};
assert_eq!(format!("{}", item), "/model");
}
#[test]
fn test_manager_path_completion() {
let mgr = CompletionManager::new(std::env::current_dir().unwrap());
let results = mgr.get_completions("./");
let _ = results;
}
#[test]
fn test_manager_no_completion_for_plain_text() {
let mgr = CompletionManager::new(std::env::current_dir().unwrap());
let results = mgr.get_completions("hello world");
assert!(results.is_empty());
}
}