Skip to main content

ReplHandler

Trait ReplHandler 

Source
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:

  1. Customize the prompt based on whether it’s a new command or a continuation.
  2. Provide syntax highlighting for the current input line.
  3. Determine if a multi-line input is complete and ready for execution.
  4. 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§

Source

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).

Source

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.

Source

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§

Source

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.

Source

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.

Implementors§