From 8d7bb9147e296aa9b8fcdaa2600866766ec2a075 Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Fri, 17 Jun 2022 20:47:43 +0800 Subject: [PATCH] Attempts to fix file completions for `open`, `rm` and `ls` (and other filesystem commands) (#5805) * fixes the issue for 'open' and other commands that explicitly use `SyntaxShape::Filepath` * fixes for `rm` and similar commands * fixes for `ls`: potentially breaking? * fmt * a curious fix to the test * a curious fix to the test, except for Windows this time * fixes Windows tests failing * resolves it by putting an explicit check for `ls` * reverts unnecessary test changes; fmt * changes the order of completion operations --- crates/nu-cli/src/completions/completer.rs | 72 ++++++++++++++-------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/crates/nu-cli/src/completions/completer.rs b/crates/nu-cli/src/completions/completer.rs index d7e69d34da..f08dccb4e9 100644 --- a/crates/nu-cli/src/completions/completer.rs +++ b/crates/nu-cli/src/completions/completer.rs @@ -97,30 +97,6 @@ impl NuCompleter { let mut prefix = working_set.get_span_contents(flat.0).to_vec(); prefix.remove(pos - (flat.0.start - alias)); - // Completions that depends on the previous expression (e.g: use, source) - if flat_idx > 0 { - if let Some(previous_expr) = flattened.get(flat_idx - 1) { - // Read the content for the previous expression - let prev_expr_str = - working_set.get_span_contents(previous_expr.0).to_vec(); - - // Completion for .nu files - if prev_expr_str == b"use" || prev_expr_str == b"source" { - let mut completer = - DotNuCompletion::new(self.engine_state.clone()); - - return self.process_completion( - &mut completer, - &working_set, - prefix, - new_span, - offset, - pos, - ); - } - } - } - // Variables completion if prefix.starts_with(b"$") || most_left_var.is_some() { let mut completer = VariableCompletion::new( @@ -153,6 +129,42 @@ impl NuCompleter { ); } + // Completions that depends on the previous expression (e.g: use, source) + if flat_idx > 0 { + if let Some(previous_expr) = flattened.get(flat_idx - 1) { + // Read the content for the previous expression + let prev_expr_str = + working_set.get_span_contents(previous_expr.0).to_vec(); + + // Completion for .nu files + if prev_expr_str == b"use" || prev_expr_str == b"source" { + let mut completer = + DotNuCompletion::new(self.engine_state.clone()); + + return self.process_completion( + &mut completer, + &working_set, + prefix, + new_span, + offset, + pos, + ); + } else if prev_expr_str == b"ls" { + let mut completer = + FileCompletion::new(self.engine_state.clone()); + + return self.process_completion( + &mut completer, + &working_set, + prefix, + new_span, + offset, + pos, + ); + } + } + } + // Match other types match &flat.1 { FlatShape::Custom(decl_id) => { @@ -185,6 +197,18 @@ impl NuCompleter { pos, ); } + FlatShape::Filepath | FlatShape::GlobPattern => { + let mut completer = FileCompletion::new(self.engine_state.clone()); + + return self.process_completion( + &mut completer, + &working_set, + prefix, + new_span, + offset, + pos, + ); + } flat_shape => { let mut completer = CommandCompletion::new( self.engine_state.clone(),