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

get back the ranges of the strings from the completer used for generating completions #713

Merged
merged 11 commits into from
Jan 22, 2024

Conversation

maxomatic458
Copy link
Contributor

closes #711
adds the function complete_with_base_ranges to the Completer trait, that returns the ranges, of the strings that the Suggestions were calculated from.

the ide menu can now use of this, to adjust the position of the suggestions to the typed text matches the suggestion.
grafik

@stormasm
Copy link
Contributor

#706 (comment)

This is another reason why consolidating all of our menu code in one place is going to make the testing and landing of PRs much simpler going forward...

This change will probably impact https://github.com/nushell/nushell/blob/main/crates/nu-cli/src/menus/description_menu.rs

Meaning once we land this PR nushell may no longer compile ---- having all of our menu code in one place is probably the way to go...

@maxomatic458
Copy link
Contributor Author

This change will probably impact https://github.com/nushell/nushell/blob/main/crates/nu-cli/src/menus/description_menu.rs

Meaning once we land this PR nushell may no longer compile ---- having all of our menu code in one place is probably the way to go...

I didnt really mess with the menus in this one, i added https://github.com/maxomatic458/reedline/blob/5f65d771873b3a15b862604746106bd2bfec4a84/src/completion/base.rs#L36-L50

and then used that in the ide menu

nushell should compile fine

but i agree in the future its probably better to first rework the menus before adding new ones

src/menu/mod.rs Outdated
Copy link
Contributor

@stormasm stormasm Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition for simplicity and consolidation as it cleans up the columnar and list menu 👍

@stormasm
Copy link
Contributor

This change will probably impact https://github.com/nushell/nushell/blob/main/crates/nu-cli/src/menus/description_menu.rs
Meaning once we land this PR nushell may no longer compile ---- having all of our menu code in one place is probably the way to go...

I didnt really mess with the menus in this one, i added https://github.com/maxomatic458/reedline/blob/5f65d771873b3a15b862604746106bd2bfec4a84/src/completion/base.rs#L36-L50

and then used that in the ide menu

nushell should compile fine

but i agree in the future its probably better to first rework the menus before adding new ones

OK, now I understand this better ! Thanks...

The description_menu will still compile fine --- I can get in there after this PR lands and modify the description menu in nushell similar to the way you modified the columnar and list menu removing the unused code ?

@maxomatic458
Copy link
Contributor Author

@stormasm yes, that should work just fine

@stormasm
Copy link
Contributor

@maxomatic458 Great work on this PR as it simplifies the code further as well as makes the end user of your API have more options for tweaking the menus they implement...

Can you please add in some more "scenarios" to ide_completions.rs example....

   let commands = vec![
        "test".into(),
        "hello world".into(),
        "hello world reedline".into(),
        "this is the reedline crate".into(),
    ];

just so I can test some other types of cases as well... I saw the test you added in too which is great...

Also --- maybe we can add in the ability to have some new lines just so we can see how the code behaves if you throw some new lines in there as well ??

Besides the tests you added --- its kind of cool and helpful to be able to play with your code a bit more with an expanded example that gives the end user more options to understand and test further the cool concepts you have implemented...

fn add_newline_keybinding(keybindings: &mut Keybindings) {
    // This doesn't work for macOS
    keybindings.add_binding(
        KeyModifiers::ALT,
        KeyCode::Enter,
        ReedlineEvent::Edit(vec![EditCommand::InsertNewline]),
    );
}

similar to -> https://github.com/stormasm/nutmp/blob/main/reedline/ide_completions.rs

@stormasm
Copy link
Contributor

stormasm commented Jan 22, 2024

Once your PR lands obviously we will have a lot more abilities to test it with the nushell code base...

But for the folks who just use reedline as end users and are not using nushell at all this makes the ide_completions.rs example more robust...

@stormasm
Copy link
Contributor

So here is the idea that I have --- let me know what you think...

In your expanded example or new example outline and show the scenario to the end user of two cases:

  1. what is a situation where they will need to have
           correct_cursor_pos: false
  1. what is a situation where
           correct_cursor_pos: true

This way the end user can have a better mental model of whats going on... Thanks !

@maxomatic458
Copy link
Contributor Author

can i just add those variables in the example, like so

    let min_completion_width = 0;
    let max_completion_width = 50;
    let max_completion_height = u16::MAX;
    let padding = 0;
    let border = false;
    let cursor_offset = 0;
    let description_mode = DescriptionMode::PreferRight;
    let min_description_width = 0;
    let max_description_width = 50;
    let description_offset = 1;
    let correct_cursor_pos = false;

    let commands = vec![
        "test".into(),
        "hello world".into(),
        "hello world reedline".into(),
        "this is the reedline crate".into(),
    ];
    let completer = Box::new(DefaultCompleter::new_with_wordlen(commands, 2));

    let mut ide_menu = IdeMenu::default()
        .with_name("completion_menu")
        .with_min_completion_width(min_completion_width)
        .with_max_completion_width(max_completion_width)
        .with_max_completion_height(max_completion_height)
        .with_padding(padding)
        .with_cursor_offset(cursor_offset)
        .with_description_mode(description_mode)
        .with_min_description_width(min_description_width)
        .with_max_description_width(max_description_width)
        .with_description_offset(description_offset)
        .with_correct_cursor_pos(correct_cursor_pos);
        
   ...

so someone could just change them? (ill also add some comments then)

@@ -680,12 +699,18 @@ impl Menu for IdeMenu {
// Also, by replacing the new line character with a space, the insert
// position is maintain in the line buffer.
let trimmed_buffer = editor.get_buffer().replace("\r\n", " ").replace('\n', " ");
completer.complete(
completer.complete_with_base_ranges(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expanded example with the added ability to throw some new lines in there will test this part of the code better

@stormasm
Copy link
Contributor

can i just add those variables in the example, like so

    let min_completion_width = 0;
    let max_completion_width = 50;
    let max_completion_height = u16::MAX;
    let padding = 0;
    let border = false;
    let cursor_offset = 0;
    let description_mode = DescriptionMode::PreferRight;
    let min_description_width = 0;
    let max_description_width = 50;
    let description_offset = 1;
    let correct_cursor_pos = false;

    let commands = vec![
        "test".into(),
        "hello world".into(),
        "hello world reedline".into(),
        "this is the reedline crate".into(),
    ];
    let completer = Box::new(DefaultCompleter::new_with_wordlen(commands, 2));

    let mut ide_menu = IdeMenu::default()
        .with_name("completion_menu")
        .with_min_completion_width(min_completion_width)
        .with_max_completion_width(max_completion_width)
        .with_max_completion_height(max_completion_height)
        .with_padding(padding)
        .with_cursor_offset(cursor_offset)
        .with_description_mode(description_mode)
        .with_min_description_width(min_description_width)
        .with_max_description_width(max_description_width)
        .with_description_offset(description_offset)
        .with_correct_cursor_pos(correct_cursor_pos);
        
   ...

so someone could just change them? (ill also add some comments then)

sounds perfect, thank you !

@maxomatic458
Copy link
Contributor Author

@stormasm quick question

    /// Menu builder with the default border value
    #[must_use]
    pub fn with_default_border(mut self) -> Self {
        self.default_details.border = Some(BorderSymbols::default());
        self
    }

i have this function in the ide menu builder, do you think i should change it to this?

    /// Menu builder with the default border value
    #[must_use]
    pub fn with_default_border(mut self, default_border: bool) -> Self {
        self.default_details.border = if default_border {
            Some(BorderSymbols::default())
        } else {
            None
        }
        self
    }

would be a breaking change for nushell, but i would probably do a PR there anyways to load the new config setting for the ide menu

@stormasm
Copy link
Contributor

@stormasm quick question

    /// Menu builder with the default border value
    #[must_use]
    pub fn with_default_border(mut self) -> Self {
        self.default_details.border = Some(BorderSymbols::default());
        self
    }

i have this function in the ide menu builder, do you think i should change it to this?

    /// Menu builder with the default border value
    #[must_use]
    pub fn with_default_border(mut self, default_border: bool) -> Self {
        self.default_details.border = if default_border {
            Some(BorderSymbols::default())
        } else {
            None
        }
        self
    }

would be a breaking change for nushell, but i would probably do a PR there anyways to load the new config setting for the ide menu

Lets leave it alone for now and not make the change....
We can add it in later if we want...

@maxomatic458
Copy link
Contributor Author

ok, then i think im done with the PR,
i also changed the default completions example (if we get more menus or more config settings, it might be a good idea to just have one example for them)

@stormasm
Copy link
Contributor

@maxomatic458 thanks for making the requested changes in the examples... good job 😄

@stormasm stormasm merged commit cbb56e2 into nushell:main Jan 22, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make the completer return the strings (or positions of them) that are used for completing
2 participants