Track call arguments in a single list (#5125)

* Initial implementation of ordered call args

* Run cargo fmt

* Fix some clippy lints

* Add positional len and nth

* Cargo fmt

* Remove more old nth calls

* Good ole rustfmt

* Add named len

Co-authored-by: Hristo Filaretov <h.filaretov@protonmail.com>
This commit is contained in:
Hristo Filaretov
2022-04-09 04:55:02 +02:00
committed by GitHub
parent 3bac480ca0
commit 683b912263
25 changed files with 263 additions and 190 deletions

View File

@ -61,11 +61,15 @@ https://www.nushell.sh/book/thinking_in_nushell.html#parsing-and-evaluation-are-
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
let var_id = call.positional[0]
let var_id = call
.positional_nth(0)
.expect("checked through parser")
.as_var()
.expect("internal error: missing variable");
let keyword_expr = call.positional[1]
let keyword_expr = call
.positional_nth(1)
.expect("checked through parser")
.as_keyword()
.expect("internal error: missing keyword");
let values = eval_expression(engine_state, stack, keyword_expr)?;

View File

@ -44,7 +44,7 @@ https://www.nushell.sh/book/thinking_in_nushell.html#parsing-and-evaluation-are-
let import_pattern = if let Some(Expression {
expr: Expr::ImportPattern(pat),
..
}) = call.positional.get(0)
}) = call.positional_nth(0)
{
pat
} else {
@ -115,7 +115,11 @@ https://www.nushell.sh/book/thinking_in_nushell.html#parsing-and-evaluation-are-
};
if stack.remove_env_var(engine_state, &name).is_none() {
return Err(ShellError::NotFound(call.positional[0].span));
return Err(ShellError::NotFound(
call.positional_nth(0)
.expect("already checked for present positional")
.span,
));
}
}
} else if !import_pattern.hidden.contains(&import_pattern.head.name)

View File

@ -50,9 +50,9 @@ https://www.nushell.sh/book/thinking_in_nushell.html#parsing-and-evaluation-are-
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let cond = &call.positional[0];
let cond = call.positional_nth(0).expect("checked through parser");
let then_block: CaptureBlock = call.req(engine_state, stack, 1)?;
let else_case = call.positional.get(2);
let else_case = call.positional_nth(2);
let result = eval_expression(engine_state, stack, cond)?;
match &result {

View File

@ -42,11 +42,15 @@ https://www.nushell.sh/book/thinking_in_nushell.html#parsing-and-evaluation-are-
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let var_id = call.positional[0]
let var_id = call
.positional_nth(0)
.expect("checked through parser")
.as_var()
.expect("internal error: missing variable");
let keyword_expr = call.positional[1]
let keyword_expr = call
.positional_nth(1)
.expect("checked through parser")
.as_keyword()
.expect("internal error: missing keyword");

View File

@ -35,7 +35,7 @@ impl Command for Metadata {
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let arg = call.positional.get(0);
let arg = call.positional_nth(0);
let head = call.head;
match arg {

View File

@ -42,7 +42,7 @@ https://www.nushell.sh/book/thinking_in_nushell.html#parsing-and-evaluation-are-
let import_pattern = if let Some(Expression {
expr: Expr::ImportPattern(pat),
..
}) = call.positional.get(0)
}) = call.positional_nth(0)
{
pat
} else {

View File

@ -35,7 +35,9 @@ impl Command for LetEnv {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let env_var = call.req(engine_state, stack, 0)?;
let keyword_expr = call.positional[1]
let keyword_expr = call
.positional_nth(1)
.expect("checked through parser")
.as_keyword()
.expect("internal error: missing keyword");

View File

@ -99,7 +99,9 @@ fn with_env(
return Err(ShellError::CantConvert(
"string list or single row".into(),
x.get_type().to_string(),
call.positional[1].span,
call.positional_nth(1)
.expect("already checked through .req")
.span,
));
}
}
@ -123,7 +125,9 @@ fn with_env(
return Err(ShellError::CantConvert(
"string list or single row".into(),
x.get_type().to_string(),
call.positional[1].span,
call.positional_nth(1)
.expect("already checked through .req")
.span,
));
}
};

View File

@ -28,7 +28,7 @@ impl Command for Cd {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let raw_path = call.nth(0);
let raw_path = call.positional_nth(0);
let path_val: Option<Value> = call.opt(engine_state, stack, 0)?;
let cwd = current_dir(engine_state, stack)?;

View File

@ -57,13 +57,18 @@ impl Command for Mkdir {
}
for (i, dir) in directories.enumerate() {
let span = call.positional[i].span;
let span = call
.positional_nth(i)
.expect("already checked through directories")
.span;
let dir_res = std::fs::create_dir_all(&dir);
if let Err(reason) = dir_res {
return Err(ShellError::CreateNotPossible(
format!("failed to create directory: {}", reason),
call.positional[i].span,
call.positional_nth(i)
.expect("already checked through directories")
.span,
));
}

View File

@ -239,7 +239,9 @@ impl Command for Touch {
if let Err(err) = OpenOptions::new().write(true).create(true).open(&item) {
return Err(ShellError::CreateNotPossible(
format!("Failed to create file: {}", err),
call.positional[index].span,
call.positional_nth(index)
.expect("already checked positional")
.span,
));
};
@ -251,7 +253,9 @@ impl Command for Touch {
) {
return Err(ShellError::ChangeModifiedTimeNotPossible(
format!("Failed to change the modified time: {}", err),
call.positional[index].span,
call.positional_nth(index)
.expect("already checked positional")
.span,
));
};
}
@ -268,7 +272,9 @@ impl Command for Touch {
) {
return Err(ShellError::ChangeAccessTimeNotPossible(
format!("Failed to change the access time: {}", err),
call.positional[index].span,
call.positional_nth(index)
.expect("already checked positional")
.span,
));
};
} else {
@ -279,7 +285,9 @@ impl Command for Touch {
) {
return Err(ShellError::ChangeAccessTimeNotPossible(
format!("Failed to change the access time: {}", err),
call.positional[index].span,
call.positional_nth(index)
.expect("already checked positional")
.span,
));
};
}

View File

@ -380,7 +380,9 @@ Format: #
None => {
return Err(ShellError::UnsupportedInput(
String::from("Unknown ansi code"),
call.nth(0).expect("Unexpected missing argument").span,
call.positional_nth(0)
.expect("Unexpected missing argument")
.span,
))
}
}

View File

@ -57,15 +57,14 @@ impl Command for KeybindingsList {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let records = if call.named.is_empty() {
let records = if call.named_len() == 0 {
let all_options = vec!["modifiers", "keycodes", "edits", "modes", "events"];
all_options
.iter()
.flat_map(|argument| get_records(argument, &call.head))
.collect()
} else {
call.named
.iter()
call.named_iter()
.flat_map(|(argument, _)| get_records(argument.item.as_str(), &call.head))
.collect()
};

View File

@ -54,8 +54,7 @@ impl Command for BuildString {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let config = stack.get_config().unwrap_or_default();
let output = call
.positional
.iter()
.positional_iter()
.map(|expr| {
eval_expression(engine_state, stack, expr).map(|val| val.into_string(", ", &config))
})

View File

@ -245,7 +245,10 @@ impl Command for Char {
}
let mut multi_byte = String::new();
for (i, arg) in args.iter().enumerate() {
let span = call.nth(i).expect("Unexpected missing argument").span;
let span = call
.positional_nth(i)
.expect("Unexpected missing argument")
.span;
multi_byte.push(string_to_unicode_char(arg, &span)?)
}
Ok(Value::string(multi_byte, call_span).into_pipeline_data())
@ -262,7 +265,9 @@ impl Command for Char {
} else {
Err(ShellError::UnsupportedInput(
"error finding named character".into(),
call.nth(0).expect("Unexpected missing argument").span,
call.positional_nth(0)
.expect("Unexpected missing argument")
.span,
))
}
}