From fd7875e572177377dc34028290d9279bbfbf4f69 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Wed, 14 Apr 2021 18:57:47 -0500 Subject: [PATCH] sort scope.aliases, commands, variables (#3319) --- crates/nu-engine/src/evaluate/scope.rs | 43 ++++++++++++++------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/crates/nu-engine/src/evaluate/scope.rs b/crates/nu-engine/src/evaluate/scope.rs index 7355185a2..028de8d14 100644 --- a/crates/nu-engine/src/evaluate/scope.rs +++ b/crates/nu-engine/src/evaluate/scope.rs @@ -34,7 +34,7 @@ impl Scope { } pub fn get_aliases(&self) -> IndexMap>> { - let mut output = IndexMap::new(); + let mut output: IndexMap>> = IndexMap::new(); for frame in self.frames.lock().iter().rev() { for v in frame.aliases.iter() { @@ -43,11 +43,12 @@ impl Scope { } } } - output + + output.sorted_by(|k1, _v1, k2, _v2| k1.cmp(k2)).collect() } pub fn get_commands(&self) -> IndexMap { - let mut output = IndexMap::new(); + let mut output: IndexMap = IndexMap::new(); for frame in self.frames.lock().iter().rev() { for (name, command) in frame.commands.iter() { @@ -60,7 +61,22 @@ impl Scope { } } - output + output.sorted_by(|k1, _v1, k2, _v2| k1.cmp(k2)).collect() + } + + pub fn get_vars(&self) -> IndexMap { + //FIXME: should this be an iterator? + let mut output: IndexMap = IndexMap::new(); + + for frame in self.frames.lock().iter().rev() { + for v in frame.vars.iter() { + if !output.contains_key(v.0) { + output.insert(v.0.clone(), v.1.clone()); + } + } + } + + output.sorted_by(|k1, _v1, k2, _v2| k1.cmp(k2)).collect() } pub fn get_aliases_with_name(&self, name: &str) -> Option>>> { @@ -109,8 +125,9 @@ impl Scope { names.append(&mut frame_command_names); } - names.dedup(); + // Sort needs to happen first because dedup works on consecutive dupes only names.sort(); + names.dedup(); names } @@ -125,6 +142,7 @@ impl Scope { names.append(&mut frame_command_names); } + // Sort needs to happen first because dedup works on consecutive dupes only names.sort(); names.dedup(); @@ -166,21 +184,6 @@ impl Scope { } } - pub fn get_vars(&self) -> IndexMap { - //FIXME: should this be an iterator? - let mut output = IndexMap::new(); - - for frame in self.frames.lock().iter().rev() { - for v in frame.vars.iter() { - if !output.contains_key(v.0) { - output.insert(v.0.clone(), v.1.clone()); - } - } - } - - output - } - pub fn get_env_vars(&self) -> IndexMap { //FIXME: should this be an iterator? let mut output = IndexMap::new();