pub trait ReplHandler {
// Required methods
fn prompt(&self, is_continuation: bool) -> &str;
fn is_complete(&self, code: &str) -> bool;
fn handle_line(&mut self, line: &str) -> Result<HandleResult, ReplError>;
// Provided methods
fn highlight<'a>(&self, _code: &'a str) -> Option<HighlightResult<'a>> { ... }
fn get_indent(&self, _code: &str) -> usize { ... }
}Expand description
Interface for language integration in the REPL.
Implement this trait to provide language-specific behavior like syntax highlighting, completion checking, and code execution.
§Usage Scenario
The ReplHandler is used by OakRepl to:
- Customize the prompt based on whether it’s a new command or a continuation.
- Provide syntax highlighting for the current input line.
- Determine if a multi-line input is complete and ready for execution.
- Execute the collected code and decide whether to continue the REPL loop.
§Example
use oak_repl::{HandleResult, ReplError, ReplHandler};
struct MyHandler;
impl ReplHandler for MyHandler {
fn prompt(&self, is_continuation: bool) -> &str {
if is_continuation { "... " } else { ">>> " }
}
fn is_complete(&self, code: &str) -> bool {
code.ends_with(';')
}
fn handle_line(&mut self, line: &str) -> Result<HandleResult, ReplError> {
println!("Executing: {}", line);
Ok(HandleResult::Continue)
}
}Required Methods§
Sourcefn prompt(&self, is_continuation: bool) -> &str
fn prompt(&self, is_continuation: bool) -> &str
Returns the prompt string to display.
is_continuation is true if the REPL is in multi-line input mode
(i.e., the previous line was not complete).
Sourcefn is_complete(&self, code: &str) -> bool
fn is_complete(&self, code: &str) -> bool
Checks if the current input buffer represents a complete statement.
If this returns false, the REPL will enter multi-line mode and
allow the user to continue typing.
Sourcefn handle_line(&mut self, line: &str) -> Result<HandleResult, ReplError>
fn handle_line(&mut self, line: &str) -> Result<HandleResult, ReplError>
Executes the given line (or multiple lines) of code.
Returns a HandleResult indicating whether to continue or exit.
Provided Methods§
Sourcefn highlight<'a>(&self, _code: &'a str) -> Option<HighlightResult<'a>>
fn highlight<'a>(&self, _code: &'a str) -> Option<HighlightResult<'a>>
Get syntax highlighting for the given code.
Returns None if no highlighting should be applied.
Sourcefn get_indent(&self, _code: &str) -> usize
fn get_indent(&self, _code: &str) -> usize
Gets the current indentation level for the next line in multi-line mode.
This is used for auto-indentation when the user presses Enter in the middle of a multi-line block.