Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use backslash at end of line for multiline text #147

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::term;

use anyhow::{Context, Result};
use reedline::Signal;
use std::borrow::Cow;
use std::rc::Rc;

pub const REPL_COMMANDS: [(&str, &str); 13] = [
Expand Down Expand Up @@ -86,7 +85,7 @@ impl Repl {
}

fn handle_line(&mut self, handler: &Rc<ReplCmdHandler>, line: &str) -> Result<bool> {
let line = clean_multiline_symbols(line);
let line = line.trim().replace("\\\n", "\n");
match parse_command(line.as_ref()) {
Some((cmd, args)) => match cmd {
".exit" => {
Expand Down Expand Up @@ -165,21 +164,12 @@ fn dump_repl_help() {
print_now!(
r###"{head}

Type `{{` to enter the multi-line editing mode, type '}}' to exit the mode.
Press Ctrl+C to abort readline, Ctrl+D to exit the REPL

"###,
);
}

fn clean_multiline_symbols(line: &str) -> Cow<str> {
let trimed_line = line.trim();
match trimed_line.chars().next() {
Some('{' | '[' | '(') => trimed_line[1..trimed_line.len() - 1].into(),
_ => Cow::Borrowed(line),
}
}

fn parse_command(line: &str) -> Option<(&str, Option<&str>)> {
let mut trimed_line = line.trim_start();
if trimed_line.starts_with('.') {
Expand Down
47 changes: 1 addition & 46 deletions src/repl/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,10 @@ pub struct ReplValidator;

impl Validator for ReplValidator {
fn validate(&self, line: &str) -> ValidationResult {
if incomplete_brackets(line) {
if line.ends_with('\\') {
ValidationResult::Incomplete
} else {
ValidationResult::Complete
}
}
}

fn incomplete_brackets(line: &str) -> bool {
let mut balance: Vec<char> = Vec::new();
let mut symbol = None;
for c in line.chars() {
match symbol {
Some(s) => match (s, c) {
('{', '}') | ('(', ')') => {
balance.pop();
}
_ if s == c => {
balance.push(c);
}
_ => {}
},
None => match c {
'{' | '(' => {
balance.push(c);
symbol = Some(c);
}
_ => {}
},
}
}

!balance.is_empty()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_incomplete_brackets() {
assert!(incomplete_brackets("{"));
assert!(incomplete_brackets("("));
assert!(!incomplete_brackets("{}"));
assert!(!incomplete_brackets("()"));
assert!(!incomplete_brackets("{ab\nc}"));
assert!(!incomplete_brackets("(ab\nc)"));
assert!(!incomplete_brackets("{[}"));
assert!(!incomplete_brackets("{{{{{}}}}}"));
assert!(incomplete_brackets("{{}"));
}
}