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

fix: panic on completion for an alias (#12090) #12596

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Ayanrocks
Copy link

This PR Fixes: #12090

Description

This pr added a if check if offset during completion is smaller than the span value, this prevents panics during subtraction when passing value to the reedline

User-Facing Changes

  • Now suggests suggestions when using alias with a space

Tests + Formatting

After Submitting

@maxomatic458
Copy link
Contributor

you could probably save a few lines by changing it from

// we'll check if start of span is greater than offset, to prevent panic
// else we can use the start and end values from last and use that.
if last.0.start >= offset {
    self.complete_commands(
        working_set,
        Span::new(last.0.start, pos),
        offset,
        false,
        options.match_algorithm,
    )
} else {
    self.complete_commands(
        working_set,
        Span::new(last.0.start, last.0.end),
        offset,
        false,
        options.match_algorithm,
    )
}

to something like this

// we'll check if start of span is greater than offset, to prevent panic
// else we can use the start and end values from last and use that.
let end = if last.0.start >= offset {
    pos
} else {
    last.0.end
};

self.complete_commands(
    working_set,
    Span::new(last.0.start, end),
    offset,
    false,
    options.match_algorithm,
)

i dont know all the details about the bug you are fixing with this, but could this also be solved by changing the offset instead of the last span? because the span is probably not broken when its calculated, so i would not change it (might break stuff somewhere else)

but as i said i dont know the full problem here, so i might be wrong

@maxomatic458
Copy link
Contributor

you might be able to just this here

let offset = offset.min(last.0.start).min(last.0.end);
let mut results = working_set
    .find_commands_by_predicate(filter_predicate, true)
    .into_iter()
    .map(move |x| SemanticSuggestion {
        suggestion: Suggestion {
            value: String::from_utf8_lossy(&x.0).to_string(),
            description: x.1,
            style: None,
            extra: None,
            span: reedline::Span::new(span.start - offset, span.end - offset), // offset is smaller or equal to span.end and span.start
            append_whitespace: true,
        },
        kind: Some(SuggestionKind::Command(x.2)),
    })
    .collect::<Vec<_>>();

@Ayanrocks
Copy link
Author

you might be able to just this here

let offset = offset.min(last.0.start).min(last.0.end);
let mut results = working_set
    .find_commands_by_predicate(filter_predicate, true)
    .into_iter()
    .map(move |x| SemanticSuggestion {
        suggestion: Suggestion {
            value: String::from_utf8_lossy(&x.0).to_string(),
            description: x.1,
            style: None,
            extra: None,
            span: reedline::Span::new(span.start - offset, span.end - offset), // offset is smaller or equal to span.end and span.start
            append_whitespace: true,
        },
        kind: Some(SuggestionKind::Command(x.2)),
    })
    .collect::<Vec<_>>();

OK I'll check this but is there a way to log from which character to which character the span is calculated for? I can see the character number but not sure how to log the character? This is just to confirm if the span has been calculated correctly or not

@kubouch
Copy link
Contributor

kubouch commented Apr 21, 2024

Would it be possible to add a test for it? We already have some tests for completions.

@Ayanrocks
Copy link
Author

Would it be possible to add a test for it? We already have some tests for completions.

Ok But can you guide me on how to add a test cases where I've to create an alias and then call suggestions.

@kubouch
Copy link
Contributor

kubouch commented Apr 27, 2024

I haven't worked on completions myself, so I can't provide a concrete advice, but you can look at nu-cli/tests/completions.rs. You could e.g. create a new alias completer that starts with alias calendar = cal --week-start monday and then test that the panic doesn't appear when invoking the completer.

@devyn devyn added panic pr:bugfix This PR fixes some bug line editor Issues related to reedline completions Issues related to tab completion labels Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completions Issues related to tab completion line editor Issues related to reedline panic pr:bugfix This PR fixes some bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash on completion for an alias
4 participants