mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 06:55:36 +02:00
Add general refactorings (#3996)
This commit is contained in:
@ -83,7 +83,7 @@ pub fn generate_docs(scope: &Scope) -> Value {
|
||||
// Return documentation for each command
|
||||
// Sub-commands are nested under their respective parent commands
|
||||
let mut table = Vec::new();
|
||||
for name in sorted_names.iter() {
|
||||
for name in &sorted_names {
|
||||
// Must be a sub-command, skip since it's being handled underneath when we hit the parent command
|
||||
if !cmap.contains_key(name) {
|
||||
continue;
|
||||
@ -91,7 +91,7 @@ pub fn generate_docs(scope: &Scope) -> Value {
|
||||
let mut row_entries = generate_doc(name, scope);
|
||||
// Iterate over all the subcommands of the parent command
|
||||
let mut sub_table = Vec::new();
|
||||
for sub_name in cmap.get(name).unwrap_or(&Vec::new()).iter() {
|
||||
for sub_name in cmap.get(name).unwrap_or(&Vec::new()) {
|
||||
let sub_row = generate_doc(sub_name, scope);
|
||||
sub_table.push(UntaggedValue::row(sub_row).into_untagged_value());
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ pub fn run_block(
|
||||
external_redirection: ExternalRedirection,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let mut output: Result<InputStream, ShellError> = Ok(OutputStream::empty());
|
||||
for (_, definition) in block.definitions.iter() {
|
||||
for definition in block.definitions.values() {
|
||||
ctx.scope.add_definition(definition.clone());
|
||||
}
|
||||
|
||||
@ -166,12 +166,8 @@ fn run_pipeline(
|
||||
UntaggedValue::Block(captured_block) => {
|
||||
ctx.scope.enter_scope();
|
||||
ctx.scope.add_vars(&captured_block.captured.entries);
|
||||
for (param, value) in captured_block
|
||||
.block
|
||||
.params
|
||||
.positional
|
||||
.iter()
|
||||
.zip(args.iter())
|
||||
for (param, value) in
|
||||
captured_block.block.params.positional.iter().zip(&args)
|
||||
{
|
||||
ctx.scope.add_var(param.0.name(), value[0].clone());
|
||||
}
|
||||
@ -188,7 +184,7 @@ fn run_pipeline(
|
||||
}
|
||||
}
|
||||
|
||||
ClassifiedCommand::Expr(expr) => run_expression_block(&*expr, ctx)?,
|
||||
ClassifiedCommand::Expr(expr) => run_expression_block(expr, ctx)?,
|
||||
|
||||
ClassifiedCommand::Error(err) => return Err(err.clone().into()),
|
||||
|
||||
|
@ -27,7 +27,7 @@ pub(crate) fn evaluate_args(
|
||||
let mut named_args = IndexMap::new();
|
||||
|
||||
if let Some(named) = &call.named {
|
||||
for (name, value) in named.iter() {
|
||||
for (name, value) in named {
|
||||
match value {
|
||||
hir::NamedValue::PresentSwitch(tag) => {
|
||||
named_args.insert(name.clone(), UntaggedValue::boolean(true).into_value(tag));
|
||||
|
@ -127,7 +127,7 @@ pub fn evaluate_baseline_expr(
|
||||
}
|
||||
|
||||
let mut row_output = IndexMap::new();
|
||||
for cell in output_headers.iter().zip(row.iter()) {
|
||||
for cell in output_headers.iter().zip(row) {
|
||||
let val = evaluate_baseline_expr(cell.1, ctx)?;
|
||||
row_output.insert(cell.0.clone(), val);
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ impl Scope {
|
||||
let mut output: IndexMap<String, Vec<Spanned<String>>> = IndexMap::new();
|
||||
|
||||
for frame in self.frames.lock().iter().rev() {
|
||||
for v in frame.aliases.iter() {
|
||||
for v in &frame.aliases {
|
||||
if !output.contains_key(v.0) {
|
||||
output.insert(v.0.clone(), v.1.clone());
|
||||
}
|
||||
@ -55,7 +55,7 @@ impl Scope {
|
||||
let mut output: IndexMap<String, Signature> = IndexMap::new();
|
||||
|
||||
for frame in self.frames.lock().iter().rev() {
|
||||
for (name, command) in frame.commands.iter() {
|
||||
for (name, command) in &frame.commands {
|
||||
if !output.contains_key(name) {
|
||||
let mut sig = command.signature();
|
||||
// don't show --help and -h in the command arguments for $scope.commands
|
||||
@ -72,7 +72,7 @@ impl Scope {
|
||||
let mut output: IndexMap<String, Command> = IndexMap::new();
|
||||
|
||||
for frame in self.frames.lock().iter().rev() {
|
||||
for (name, command) in frame.commands.iter() {
|
||||
for (name, command) in &frame.commands {
|
||||
if !output.contains_key(name) {
|
||||
output.insert(name.clone(), command.clone());
|
||||
}
|
||||
@ -91,7 +91,7 @@ impl Scope {
|
||||
let mut output: IndexMap<String, Value> = IndexMap::new();
|
||||
|
||||
for frame in self.frames.lock().iter().rev() {
|
||||
for v in frame.vars.iter() {
|
||||
for v in &frame.vars {
|
||||
if !output.contains_key(v.0) {
|
||||
output.insert(v.0.clone(), v.1.clone());
|
||||
}
|
||||
@ -212,7 +212,7 @@ impl Scope {
|
||||
let mut output = IndexMap::new();
|
||||
|
||||
for frame in self.frames.lock().iter().rev() {
|
||||
for v in frame.env.iter() {
|
||||
for v in &frame.env {
|
||||
if !output.contains_key(v.0) {
|
||||
output.insert(v.0.clone(), v.1.clone());
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ pub fn nu(scope: &Scope, ctx: &EvaluationContext) -> Result<Value, ShellError> {
|
||||
|
||||
let mut dict = TaggedDictBuilder::new(&tag);
|
||||
|
||||
for v in env.iter() {
|
||||
for v in env {
|
||||
if v.0 != "PATH" && v.0 != "Path" {
|
||||
dict.insert_untagged(v.0, UntaggedValue::string(v.1));
|
||||
}
|
||||
@ -63,7 +63,7 @@ pub fn nu(scope: &Scope, ctx: &EvaluationContext) -> Result<Value, ShellError> {
|
||||
// For now, we work around the discrepency as best we can by merging the two into what is shown to the user as the
|
||||
// 'path' column of `$nu`
|
||||
let mut table = vec![];
|
||||
for v in env.iter() {
|
||||
for v in env {
|
||||
if v.0 == "PATH" || v.0 == "Path" {
|
||||
for path in std::env::split_paths(&v.1) {
|
||||
table.push(UntaggedValue::filepath(path).into_value(&tag));
|
||||
@ -110,11 +110,11 @@ pub fn scope(
|
||||
let mut scope_dict = TaggedDictBuilder::new(&tag);
|
||||
|
||||
let mut aliases_dict = TaggedDictBuilder::new(&tag);
|
||||
for v in aliases.iter() {
|
||||
for v in aliases {
|
||||
let values = v.1.clone();
|
||||
let mut vec = Vec::new();
|
||||
|
||||
for k in values.iter() {
|
||||
for k in &values {
|
||||
vec.push(k.to_string());
|
||||
}
|
||||
|
||||
@ -124,7 +124,7 @@ pub fn scope(
|
||||
}
|
||||
|
||||
let mut commands_dict = TaggedDictBuilder::new(&tag);
|
||||
for (name, signature) in commands.iter() {
|
||||
for (name, signature) in commands {
|
||||
commands_dict.insert_untagged(name, UntaggedValue::string(&signature.allowed().join(" ")))
|
||||
}
|
||||
|
||||
|
@ -432,7 +432,7 @@ impl VariableRegistry for EvaluationContext {
|
||||
fn variables(&self) -> Vec<String> {
|
||||
Variable::list()
|
||||
.into_iter()
|
||||
.chain(self.scope.get_variable_names().into_iter())
|
||||
.chain(self.scope.get_variable_names())
|
||||
.unique()
|
||||
.collect()
|
||||
}
|
||||
|
@ -491,7 +491,7 @@ impl Shell for FilesystemShell {
|
||||
));
|
||||
}
|
||||
|
||||
for dir in directories.iter() {
|
||||
for dir in &directories {
|
||||
let create_at = path.join(&dir.item);
|
||||
|
||||
let dir_res = std::fs::create_dir_all(&create_at);
|
||||
@ -660,7 +660,7 @@ impl Shell for FilesystemShell {
|
||||
// It is not appropriate to try and remove the
|
||||
// current directory or its parent when using
|
||||
// glob patterns.
|
||||
let name = format!("{}", f.display());
|
||||
let name = f.display().to_string();
|
||||
if name.ends_with("/.") || name.ends_with("/..") {
|
||||
continue;
|
||||
}
|
||||
@ -1056,10 +1056,8 @@ pub(crate) fn dir_entry_dict(
|
||||
{
|
||||
for column in [
|
||||
"name", "type", "target", "readonly", "size", "created", "accessed", "modified",
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
dict.insert_untagged(*column, UntaggedValue::nothing());
|
||||
] {
|
||||
dict.insert_untagged(column, UntaggedValue::nothing());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1079,18 +1077,16 @@ pub(crate) fn dir_entry_dict(
|
||||
"created",
|
||||
"accessed",
|
||||
"modified",
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
dict.insert_untagged(&(*column.to_owned()), UntaggedValue::nothing());
|
||||
] {
|
||||
dict.insert_untagged(column, UntaggedValue::nothing());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for column in ["name", "type", "target", "size", "modified"].iter() {
|
||||
if *column == "target" {
|
||||
for column in ["name", "type", "target", "size", "modified"] {
|
||||
if column == "target" {
|
||||
continue;
|
||||
}
|
||||
dict.insert_untagged(*column, UntaggedValue::nothing());
|
||||
dict.insert_untagged(column, UntaggedValue::nothing());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ pub fn build_plugin_command(
|
||||
Command::new("pwsh")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.args(&[
|
||||
.args([
|
||||
"-NoLogo",
|
||||
"-NoProfile",
|
||||
"-ExecutionPolicy",
|
||||
|
@ -133,8 +133,7 @@ fn run_filter(path: String, args: CommandArgs) -> Result<ActionStream, ShellErro
|
||||
|
||||
let bos = vec![UntaggedValue::Primitive(Primitive::BeginningOfStream).into_untagged_value()]
|
||||
.into_iter();
|
||||
let eos =
|
||||
vec![UntaggedValue::Primitive(Primitive::EndOfStream).into_untagged_value()].into_iter();
|
||||
let eos = [UntaggedValue::Primitive(Primitive::EndOfStream).into_untagged_value()];
|
||||
|
||||
let (call_info, input) = evaluate_once(args)?;
|
||||
|
||||
@ -149,7 +148,7 @@ fn run_filter(path: String, args: CommandArgs) -> Result<ActionStream, ShellErro
|
||||
Command::new("pwsh")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.args(&[
|
||||
.args([
|
||||
"-NoLogo",
|
||||
"-NoProfile",
|
||||
"-ExecutionPolicy",
|
||||
@ -172,7 +171,7 @@ fn run_filter(path: String, args: CommandArgs) -> Result<ActionStream, ShellErro
|
||||
Ok(bos
|
||||
.chain(input)
|
||||
.chain(eos)
|
||||
.map(move |item| {
|
||||
.flat_map(move |item| {
|
||||
match item {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::BeginningOfStream),
|
||||
@ -201,7 +200,7 @@ fn run_filter(path: String, args: CommandArgs) -> Result<ActionStream, ShellErro
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
return ActionStream::one(Err(ShellError::unexpected(
|
||||
format!("{}", err),
|
||||
err.to_string(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
@ -262,7 +261,7 @@ fn run_filter(path: String, args: CommandArgs) -> Result<ActionStream, ShellErro
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
return ActionStream::one(Err(ShellError::unexpected(
|
||||
format!("{}", err),
|
||||
err.to_string(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
@ -371,7 +370,6 @@ fn run_filter(path: String, args: CommandArgs) -> Result<ActionStream, ShellErro
|
||||
}
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.into_action_stream())
|
||||
}
|
||||
|
||||
@ -435,7 +433,7 @@ fn run_sink(path: String, args: CommandArgs) -> Result<ActionStream, ShellError>
|
||||
// an example of what CallInfo would look like in this temp file
|
||||
let child = if ps1_file {
|
||||
Command::new("pwsh")
|
||||
.args(&[
|
||||
.args([
|
||||
"-NoLogo",
|
||||
"-NoProfile",
|
||||
"-ExecutionPolicy",
|
||||
@ -528,7 +526,7 @@ fn evaluate_args(call: &hir::Call, ctx: &EvaluationContext) -> Result<EvaluatedA
|
||||
let mut named_args = IndexMap::new();
|
||||
|
||||
if let Some(named) = &call.named {
|
||||
for (name, value) in named.iter() {
|
||||
for (name, value) in named {
|
||||
match value {
|
||||
hir::NamedValue::PresentSwitch(tag) => {
|
||||
named_args.insert(name.clone(), UntaggedValue::boolean(true).into_value(tag));
|
||||
|
@ -68,7 +68,7 @@ impl Painter {
|
||||
// Emit, as we changed styles
|
||||
let intermediate = String::from_utf8_lossy(&self.original[idx_start..idx_end]);
|
||||
|
||||
builder.push_str(&format!("{}", current_style.paint(intermediate)));
|
||||
builder.push_str(¤t_style.paint(intermediate).to_string());
|
||||
|
||||
current_style = self.styles[idx_end];
|
||||
idx_start = idx_end;
|
||||
@ -77,7 +77,7 @@ impl Painter {
|
||||
}
|
||||
|
||||
let intermediate = String::from_utf8_lossy(&self.original[idx_start..idx_end]);
|
||||
builder.push_str(&format!("{}", current_style.paint(intermediate)));
|
||||
builder.push_str(¤t_style.paint(intermediate).to_string());
|
||||
|
||||
builder
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ impl ValueShell {
|
||||
let mut viewed = self.value.clone();
|
||||
let sep_string = std::path::MAIN_SEPARATOR.to_string();
|
||||
let sep = OsStr::new(&sep_string);
|
||||
for p in full_path.iter() {
|
||||
for p in &full_path {
|
||||
match p {
|
||||
x if x == sep => {}
|
||||
step => {
|
||||
@ -107,7 +107,7 @@ impl Shell for ValueShell {
|
||||
let mut full_path = PathBuf::from(self.path());
|
||||
|
||||
if let Some(value) = &path {
|
||||
full_path.push(value.as_ref());
|
||||
full_path.push(&value.item);
|
||||
}
|
||||
|
||||
if self.find(&full_path).is_none() {
|
||||
|
@ -323,7 +323,7 @@ fn get_result_shape_of_math_expr(
|
||||
scope: &Scope,
|
||||
) -> Result<Option<SyntaxShape>, ShellError> {
|
||||
let mut shapes: Vec<Option<SyntaxShape>> = vec![];
|
||||
for expr in &[&bin.left, &bin.right] {
|
||||
for expr in [&bin.left, &bin.right] {
|
||||
let shape = match &expr.expr {
|
||||
Expression::Binary(deep_binary) => {
|
||||
get_result_shape_of_math_expr(deep_binary, (pipeline_idx, pipeline), scope)?
|
||||
@ -1020,7 +1020,7 @@ impl VarSyntaxShapeDeductor {
|
||||
merge_join_by(a, b, |a, b| {
|
||||
(a.deduction as i32).cmp(&(b.deduction as i32))
|
||||
})
|
||||
.map(|either_or| match either_or {
|
||||
.filter_map(|either_or| match either_or {
|
||||
//Left is a, right is b
|
||||
//(a UNION none) OR a is a
|
||||
EitherOrBoth::Left(deduc) => Some(deduc.clone()),
|
||||
@ -1033,7 +1033,6 @@ impl VarSyntaxShapeDeductor {
|
||||
Some(combination)
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
//No any's intersection of both is result
|
||||
|
Reference in New Issue
Block a user