Various line editor fixes and optimizations

This commit is contained in:
2026-02-25 15:43:08 -05:00
parent 8d694d8281
commit 22adbce9e4
18 changed files with 359 additions and 152 deletions

View File

@@ -1,6 +1,7 @@
use std::collections::HashSet;
use crate::expand::{perform_param_expansion, DUB_QUOTE, VAR_SUB};
use crate::expand::perform_param_expansion;
use crate::prompt::readline::markers;
use crate::state::VarFlags;
use super::*;
@@ -296,7 +297,7 @@ fn dquote_escape_dollar() {
// "\$foo" should strip backslash, produce literal $foo (no expansion)
let result = unescape_str(r#""\$foo""#);
assert!(
!result.contains(VAR_SUB),
!result.contains(markers::VAR_SUB),
"Escaped $ should not become VAR_SUB"
);
assert!(result.contains('$'), "Literal $ should be preserved");
@@ -307,7 +308,7 @@ fn dquote_escape_dollar() {
fn dquote_escape_backslash() {
// "\\" in double quotes should produce a single backslash
let result = unescape_str(r#""\\""#);
let inner: String = result.chars().filter(|&c| c != DUB_QUOTE).collect();
let inner: String = result.chars().filter(|&c| c != markers::DUB_QUOTE).collect();
assert_eq!(
inner, "\\",
"Double backslash should produce single backslash"
@@ -318,7 +319,7 @@ fn dquote_escape_backslash() {
fn dquote_escape_quote() {
// "\"" should produce a literal double quote
let result = unescape_str(r#""\"""#);
let inner: String = result.chars().filter(|&c| c != DUB_QUOTE).collect();
let inner: String = result.chars().filter(|&c| c != markers::DUB_QUOTE).collect();
assert!(
inner.contains('"'),
"Escaped quote should produce literal quote"
@@ -329,7 +330,7 @@ fn dquote_escape_quote() {
fn dquote_escape_backtick() {
// "\`" should strip backslash, produce literal backtick
let result = unescape_str(r#""\`""#);
let inner: String = result.chars().filter(|&c| c != DUB_QUOTE).collect();
let inner: String = result.chars().filter(|&c| c != markers::DUB_QUOTE).collect();
assert_eq!(
inner, "`",
"Escaped backtick should produce literal backtick"
@@ -340,7 +341,7 @@ fn dquote_escape_backtick() {
fn dquote_escape_nonspecial_preserves_backslash() {
// "\a" inside double quotes should preserve the backslash (a is not special)
let result = unescape_str(r#""\a""#);
let inner: String = result.chars().filter(|&c| c != DUB_QUOTE).collect();
let inner: String = result.chars().filter(|&c| c != markers::DUB_QUOTE).collect();
assert_eq!(
inner, "\\a",
"Backslash before non-special char should be preserved"
@@ -352,7 +353,7 @@ fn dquote_unescaped_dollar_expands() {
// "$foo" inside double quotes should produce VAR_SUB (expansion marker)
let result = unescape_str(r#""$foo""#);
assert!(
result.contains(VAR_SUB),
result.contains(markers::VAR_SUB),
"Unescaped $ should become VAR_SUB"
);
}
@@ -361,10 +362,10 @@ fn dquote_unescaped_dollar_expands() {
fn dquote_mixed_escapes() {
// "hello \$world \\end" should have literal $, single backslash
let result = unescape_str(r#""hello \$world \\end""#);
assert!(!result.contains(VAR_SUB), "Escaped $ should not expand");
assert!(!result.contains(markers::VAR_SUB), "Escaped $ should not expand");
assert!(result.contains('$'), "Literal $ should be in output");
// Should have exactly one backslash (from \\)
let inner: String = result.chars().filter(|&c| c != DUB_QUOTE).collect();
let inner: String = result.chars().filter(|&c| c != markers::DUB_QUOTE).collect();
let backslash_count = inner.chars().filter(|&c| c == '\\').count();
assert_eq!(backslash_count, 1, "\\\\ should produce one backslash");
}

View File

@@ -21,7 +21,7 @@ fn getopt_from_argv() {
panic!()
};
let (words, opts) = get_opts_from_tokens(argv, &ECHO_OPTS);
let (words, opts) = get_opts_from_tokens(argv, &ECHO_OPTS).expect("failed to get opts");
insta::assert_debug_snapshot!(words);
insta::assert_debug_snapshot!(opts)
}

View File

@@ -1,18 +1,12 @@
use std::collections::VecDeque;
use crate::{
libsh::{
expand::expand_prompt, libsh::{
error::ShErr,
term::{Style, Styled},
},
prompt::readline::{
history::History,
keys::{KeyCode, KeyEvent, ModKeys},
linebuf::LineBuf,
term::{raw_mode, KeyReader, LineWriter},
vimode::{ViInsert, ViMode, ViNormal},
ShedVi,
},
}, prompt::readline::{
Prompt, ShedVi, history::History, keys::{KeyCode, KeyEvent, ModKeys}, linebuf::LineBuf, term::{KeyReader, LineWriter, raw_mode}, vimode::{ViInsert, ViMode, ViNormal}
}
};
use pretty_assertions::assert_eq;
@@ -255,6 +249,13 @@ fn linebuf_ascii_content() {
assert_eq!(buf.slice_from(2), Some("llo"));
}
#[test]
fn expand_default_prompt() {
let prompt = expand_prompt("\\e[0m\\n\\e[1;0m\\u\\e[1;36m@\\e[1;31m\\h\\n\\e[1;36m\\W\\e[1;32m/\\n\\e[1;32m\\$\\e[0m ".into()).unwrap();
insta::assert_debug_snapshot!(prompt)
}
#[test]
fn linebuf_unicode_graphemes() {
let mut buf = LineBuf::new().with_initial("a🇺🇸b́c", 0);
@@ -598,7 +599,7 @@ fn editor_delete_line_up() {
"dk",
LOREM_IPSUM,
237),
("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\nCurabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra.".into(), 240,)
("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\nCurabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra.".into(), 129,)
)
}