Skip to content

Commit 42057df

Browse files
committed
use file-path completion as fallback
1 parent e561262 commit 42057df

File tree

1 file changed

+4
-83
lines changed

1 file changed

+4
-83
lines changed

crates/q_cli/src/cli/chat/prompt.rs

+4-83
Original file line numberDiff line numberDiff line change
@@ -62,71 +62,21 @@ pub fn generate_prompt(current_profile: Option<&str>) -> String {
6262
"> ".to_string()
6363
}
6464

65-
/// Categorized path context patterns for better organization and maintainability
66-
pub struct PathContextPatterns {
67-
/// File operation commands (cat, ls, etc.)
68-
pub file_commands: &'static [&'static str],
69-
/// Words indicating file operations (open, read, etc.)
70-
pub file_operations: &'static [&'static str],
71-
/// Path-related terms (file, path, directory, etc.)
72-
pub path_terms: &'static [&'static str],
73-
/// Special commands that work with files
74-
pub special_commands: &'static [&'static str],
75-
}
76-
77-
impl Default for PathContextPatterns {
78-
fn default() -> Self {
79-
Self {
80-
file_commands: &["cat ", "ls ", "cd ", "vim ", "nano ", "less ", "more ", "grep "],
81-
file_operations: &["open ", "read ", "write ", "edit ", "create ", "delete ", "remove "],
82-
path_terms: &["file ", "path ", "directory ", "folder ", "location "],
83-
special_commands: &["/context add", "/context rm"],
84-
}
85-
}
86-
}
87-
8865
/// A wrapper around FilenameCompleter that provides enhanced path detection
8966
/// and completion capabilities for the chat interface.
9067
pub struct PathCompleter {
9168
/// The underlying filename completer from rustyline
9269
filename_completer: FilenameCompleter,
93-
path_patterns: PathContextPatterns,
9470
}
9571

9672
impl PathCompleter {
9773
/// Creates a new PathCompleter instance
9874
pub fn new() -> Self {
9975
Self {
10076
filename_completer: FilenameCompleter::new(),
101-
path_patterns: PathContextPatterns::default(),
10277
}
10378
}
10479

105-
/// Check if the input might be referring to a file path based on context clues
106-
fn is_path_context(&self, line: &str) -> bool {
107-
// Check for file commands
108-
let has_file_command = self.path_patterns.file_commands
109-
.iter()
110-
.any(|&cmd| line.contains(cmd));
111-
112-
// Check for file operations
113-
let has_file_operation = self.path_patterns.file_operations
114-
.iter()
115-
.any(|&op| line.contains(op));
116-
117-
// Check for path terms
118-
let has_path_term = self.path_patterns.path_terms
119-
.iter()
120-
.any(|&term| line.contains(term));
121-
122-
// Check for special commands
123-
let has_special_command = self.path_patterns.special_commands
124-
.iter()
125-
.any(|&cmd| line.contains(cmd));
126-
127-
has_file_command || has_file_operation || has_path_term || has_special_command
128-
}
129-
13080
/// Attempts to complete a file path at the given position in the line
13181
pub fn complete_path(
13282
&self,
@@ -149,11 +99,6 @@ impl PathCompleter {
14999
Err(err) => Err(err),
150100
}
151101
}
152-
153-
/// Checks if the word appears to be a file path based on its syntax
154-
pub fn is_path_syntax(&self, word: &str) -> bool {
155-
word.contains('/') || word.starts_with('~') || word.starts_with('.')
156-
}
157102
}
158103

159104
pub struct ChatCompleter {
@@ -197,12 +142,10 @@ impl Completer for ChatCompleter {
197142
return Ok(self.complete_command(word, start));
198143
}
199144

200-
// Handle file path completion if the word contains path separators or context suggests file paths
201-
if self.path_completer.is_path_syntax(word) || self.path_completer.is_path_context(line) {
202-
if let Ok((pos, completions)) = self.path_completer.complete_path(line, pos, _ctx) {
203-
if !completions.is_empty() {
204-
return Ok((pos, completions));
205-
}
145+
// Handle file path completion as fallback
146+
if let Ok((pos, completions)) = self.path_completer.complete_path(line, pos, _ctx) {
147+
if !completions.is_empty() {
148+
return Ok((pos, completions));
206149
}
207150
}
208151

@@ -322,26 +265,4 @@ mod tests {
322265
// Verify no completions are returned for regular text
323266
assert!(completions.is_empty());
324267
}
325-
326-
#[test]
327-
fn test_is_path_context() {
328-
let completer = PathCompleter::new();
329-
330-
// Test positive cases
331-
assert!(completer.is_path_context("Please read file test.txt"));
332-
assert!(completer.is_path_context("Show me the path to config"));
333-
assert!(completer.is_path_context("List the directory contents"));
334-
assert!(completer.is_path_context("/context add ./myfile.txt"));
335-
assert!(completer.is_path_context("/context rm ./myfile.txt"));
336-
assert!(completer.is_path_context("Can you open this file?"));
337-
assert!(completer.is_path_context("Please read this for me"));
338-
assert!(completer.is_path_context("cat my_file.txt"));
339-
assert!(completer.is_path_context("ls -la"));
340-
assert!(completer.is_path_context("cd /usr/local"));
341-
342-
// Test negative cases
343-
assert!(!completer.is_path_context("Hello world"));
344-
assert!(!completer.is_path_context("What is the weather today?"));
345-
assert!(!completer.is_path_context("How do I upload data using s3 cli"));
346-
}
347268
}

0 commit comments

Comments
 (0)