Skip to content

Commit 2d16273

Browse files
authored
added hint for multiline triple backtick code block detector (#3327)
* got rid of unnecessary triple backtick code block detector * added back the triple backtick validation * multiline hint added * formatted after multiline hint added * blocked undesired autocompletion for multiline hint
1 parent a0eb7b2 commit 2d16273

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,16 @@ impl ChatHinter {
353353
return None;
354354
}
355355

356+
// Check for unclosed triple backticks
357+
if line.contains("```") {
358+
let triple_backtick_count = line.matches("```").count();
359+
if triple_backtick_count % 2 == 1 {
360+
// We have an odd number of ```, meaning we're in multiline mode
361+
// Show status hint (right arrow key is overridden to not complete this)
362+
return Some("in multiline mode, waiting for closing backticks ```".to_string());
363+
}
364+
}
365+
356366
// If line starts with a slash, try to find a command hint
357367
if line.starts_with('/') {
358368
return self
@@ -550,6 +560,34 @@ impl rustyline::ConditionalEventHandler for PasteImageHandler {
550560
}
551561
}
552562

563+
/// Handler for right arrow key that prevents completing the multiline status hint
564+
struct RightArrowHandler;
565+
566+
impl rustyline::ConditionalEventHandler for RightArrowHandler {
567+
fn handle(
568+
&self,
569+
_evt: &rustyline::Event,
570+
_n: rustyline::RepeatCount,
571+
_positive: bool,
572+
ctx: &rustyline::EventContext<'_>,
573+
) -> Option<Cmd> {
574+
let line = ctx.line();
575+
576+
// Check if we're in multiline mode with unclosed backticks
577+
if line.contains("```") {
578+
let triple_backtick_count = line.matches("```").count();
579+
if triple_backtick_count % 2 == 1 {
580+
// We're in multiline mode - don't complete the hint
581+
// Just move the cursor forward instead
582+
return Some(Cmd::Move(rustyline::Movement::ForwardChar(1)));
583+
}
584+
}
585+
586+
// Normal case - complete the hint
587+
Some(Cmd::CompleteHint)
588+
}
589+
}
590+
553591
pub fn rl(
554592
os: &Os,
555593
sender: PromptQuerySender,
@@ -643,6 +681,12 @@ pub fn rl(
643681
EventHandler::Conditional(Box::new(PasteImageHandler::new(paste_state))),
644682
);
645683

684+
// Override right arrow key to prevent completing multiline status hints
685+
rl.bind_sequence(
686+
KeyEvent(KeyCode::Right, Modifiers::empty()),
687+
EventHandler::Conditional(Box::new(RightArrowHandler)),
688+
);
689+
646690
Ok(rl)
647691
}
648692

0 commit comments

Comments
 (0)