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

Apply "Parsing With Derivatives" incremental parsing to Bash tab completion #119

Merged
merged 1 commit into from
Mar 25, 2022

Conversation

harveywi
Copy link
Contributor

Overview

This PR adapts the Parsing With Derivatives (PwD) functional pearl to the problem of CLI tab completion. Currently, this is only supported for Bash tab completion, but it should be possible to use this routine for tab completion with other shells.

PwD for Incremental Parsing

For any zio.cli.Command instance, there is a corresponding regular language that contains all of the instance's valid sequences of command line arguments. We first build a data structure that represents this regular language, then use the PwD algorithm to incrementally update this data structure as a kind of incremental parsing.

As we process each user-provided command line argument from left to right, the PwD algorithm gives us a "residual language" that represents all of the valid arguments that can be provided to the right of the current argument. So all we have to do is calculate the residual language for everything to the left of the CLI cursor position, then return a list of all of the residual language's first tokens that share a prefix with the token at the cursor position.

Notes on Latency

Since the Bash complete machinery will call your binary to calculate tab completions, and satisfactory experience with tab completion requires low latency, we highly recommend building your binary using graalvm native-image --no-fallback.

@jdegoes
Copy link
Member

jdegoes commented Mar 18, 2022

@harveywi Can you bring this up to date so we can merge?

@harveywi
Copy link
Contributor Author

@harveywi Can you bring this up to date so we can merge?

Yes - I'll set some time aside this weekend, or early next week if needed. I think there's just a merge conflict that (hopefully) won't be too challenging to resolve.

@harveywi
Copy link
Contributor Author

@jdegoes I am almost done refreshing this PR - there was considerable code churn and I went on a side quest to upgrade zio-nio to use ZIO 2.0.0-RC3. The good news is that all the tests pass and the tab completion works as expected 🎉.

To speed things along, I may see if I can just mimic the one or two methods from zio-nio and pop them directly in this PR - I think they're only used in the unit tests anyways. I should be able to wrap this up soon.

@jdegoes
Copy link
Member

jdegoes commented Mar 24, 2022

Thanks for the update!

@harveywi harveywi force-pushed the wjh-tab-completion-incremental-parsing branch 2 times, most recently from fdf488f to adbbd4b Compare March 25, 2022 02:12
@harveywi harveywi force-pushed the wjh-tab-completion-incremental-parsing branch from adbbd4b to c82b510 Compare March 25, 2022 02:23
@harveywi
Copy link
Contributor Author

Ok - I think that should do it. 🤷

@jdegoes
Copy link
Member

jdegoes commented Mar 25, 2022

Looks great! Can you resolve conflicts?

@harveywi
Copy link
Contributor Author

Looks great! Can you resolve conflicts?

Did I miss some conflicts? I had rebased this PR off of the latest (as of now) master branch, and GitHub seems to think the PR has no merge conflicts ("This branch has no conflicts with the base branch").

@jdegoes jdegoes merged commit d45450a into zio:master Mar 25, 2022
@jdegoes
Copy link
Member

jdegoes commented Mar 25, 2022

Sorry must have been a stale browser window. Looks great, and all merged in! 💪

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.

2 participants