Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to file completer in custom/external completer #14781

Merged
merged 2 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions crates/nu-cli/src/completions/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,24 @@ impl NuCompleter {
);

match result.and_then(|data| data.into_value(span)) {
Ok(Value::List { vals, .. }) => {
let result =
map_value_completions(vals.iter(), Span::new(span.start, span.end), offset);
Some(result)
}
Ok(Value::Nothing { .. }) => None,
Ok(value) => {
if let Value::List { vals, .. } = value {
let result =
map_value_completions(vals.iter(), Span::new(span.start, span.end), offset);

return Some(result);
}
log::error!(
"External completer returned invalid value of type {}",
value.get_type().to_string()
);
Some(vec![])
}
Err(err) => {
log::error!("failed to eval completer block: {err}");
Some(vec![])
}
Err(err) => println!("failed to eval completer block: {err}"),
}

None
}

fn completion_helper(&mut self, line: &str, pos: usize) -> Vec<SemanticSuggestion> {
Expand Down Expand Up @@ -319,6 +325,7 @@ impl NuCompleter {
self.stack.clone(),
*decl_id,
initial_line,
FileCompletion::new(),
);

return self.process_completion(
Expand Down
48 changes: 35 additions & 13 deletions crates/nu-cli/src/completions/custom_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,34 @@ use std::collections::HashMap;

use super::completion_options::NuMatcher;

pub struct CustomCompletion {
pub struct CustomCompletion<T: Completer> {
stack: Stack,
decl_id: DeclId,
line: String,
fallback: T,
}

impl CustomCompletion {
pub fn new(stack: Stack, decl_id: DeclId, line: String) -> Self {
impl<T: Completer> CustomCompletion<T> {
pub fn new(stack: Stack, decl_id: DeclId, line: String, fallback: T) -> Self {
Self {
stack,
decl_id,
line,
fallback,
}
}
}

impl Completer for CustomCompletion {
impl<T: Completer> Completer for CustomCompletion<T> {
fn fetch(
&mut self,
working_set: &StateWorkingSet,
_stack: &Stack,
stack: &Stack,
prefix: &[u8],
span: Span,
offset: usize,
pos: usize,
completion_options: &CompletionOptions,
orig_options: &CompletionOptions,
) -> Vec<SemanticSuggestion> {
// Line position
let line_pos = pos - offset;
Expand Down Expand Up @@ -66,13 +68,12 @@ impl Completer for CustomCompletion {
PipelineData::empty(),
);

let mut completion_options = completion_options.clone();
let mut completion_options = orig_options.clone();
let mut should_sort = true;

// Parse result
let suggestions = result
.and_then(|data| data.into_value(span))
.map(|value| match &value {
let suggestions = match result.and_then(|data| data.into_value(span)) {
Ok(value) => match &value {
Value::Record { val, .. } => {
let completions = val
.get("completions")
Expand Down Expand Up @@ -112,9 +113,30 @@ impl Completer for CustomCompletion {
completions
}
Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset),
_ => vec![],
})
.unwrap_or_default();
Value::Nothing { .. } => {
return self.fallback.fetch(
working_set,
stack,
prefix,
span,
offset,
pos,
orig_options,
);
}
_ => {
log::error!(
"Custom completer returned invalid value of type {}",
value.get_type().to_string()
);
return vec![];
}
},
Err(e) => {
log::error!("Error getting custom completions: {e}");
return vec![];
}
};

let mut matcher = NuMatcher::new(String::from_utf8_lossy(prefix), completion_options);

Expand Down
52 changes: 52 additions & 0 deletions crates/nu-cli/tests/completions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,37 @@ fn customcompletions_no_sort() {
match_suggestions(&expected, &suggestions);
}

/// Fallback to file completions if custom completer returns null
#[test]
fn customcompletions_fallback() {
let (_, _, mut engine, mut stack) = new_engine();
let command = r#"
def comp [] { null }
def my-command [arg: string@comp] {}"#;
assert!(support::merge_input(command.as_bytes(), &mut engine, &mut stack).is_ok());

let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
let completion_str = "my-command test";
let suggestions = completer.complete(completion_str, completion_str.len());
let expected: Vec<String> = vec![folder("test_a"), file("test_a_symlink"), folder("test_b")];
match_suggestions(&expected, &suggestions);
}

/// Suppress completions for invalid values
#[test]
fn customcompletions_invalid() {
let (_, _, mut engine, mut stack) = new_engine();
let command = r#"
def comp [] { 123 }
def my-command [arg: string@comp] {}"#;
assert!(support::merge_input(command.as_bytes(), &mut engine, &mut stack).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));

let completion_str = "my-command foo";
let suggestions = completer.complete(completion_str, completion_str.len());
assert!(suggestions.is_empty());
}

#[test]
fn dotnu_completions() {
// Create a new engine
Expand Down Expand Up @@ -312,6 +343,27 @@ fn external_completer_pass_flags() {
assert_eq!("--", suggestions.get(2).unwrap().value);
}

/// Fallback to file completions when external completer returns null
#[test]
fn external_completer_fallback() {
let block = "{|spans| null}";
let input = "foo test".to_string();

let expected = vec![folder("test_a"), file("test_a_symlink"), folder("test_b")];
let suggestions = run_external_completion(block, &input);
match_suggestions(&expected, &suggestions);
}

/// Suppress completions when external completer returns invalid value
#[test]
fn external_completer_invalid() {
let block = "{|spans| 123}";
let input = "foo ".to_string();

let suggestions = run_external_completion(block, &input);
assert!(suggestions.is_empty());
}

#[test]
fn file_completions() {
// Create a new engine
Expand Down
Loading