Skip to content

nu-cli: refactor completions - #5102

Merged
sophiajt merged 1 commit into
nushell:mainfrom
mxfp0:feat/completion-refactor
Apr 6, 2022
Merged

sophiajt merged 1 commit into
nushell:mainfrom
mxfp0:feat/completion-refactor

Conversation

@mxfp0

@mxfp0 mxfp0 commented Apr 5, 2022

Copy link
Copy Markdown
Contributor

Description

The previous completions file was becoming too tight coupled and big, this PR proposes a refactor to it.

Now the completions are located inside the completions/ folder, each completion is handled in different files.

For now we have 5 completions:

  • Flag Completions
  • Custom Completions
  • Command Completions
  • File Completions
  • Variable Completions

All the completions need to implement the Completer trait (which isn't the one from reedline).

The Completer trait is separated in 3 steps:

  • Fetch: responsible for fetching the Vec<Suggestions> and also the CompletionOptions which contains information helpful for filtering and sorting.
  • Filter: responsible for correctly filtering the suggestions based on the given CompletionOptions
  • Sort: responsible for correctly sorting the suggestions based on the given CompletionOptions

The trait has some default behaviors for filtering and sorting but those can be overwritten by any completer if needed, which gives us a lot of flexibility in writing new completers.

Also, the layers separation are very important for future caching, once we could cache (for some time) the fetch results and just do filter and sorting. Some Custom Completions depends on that kind of cache because they can do external calls or any other thing that takes time to return.

Tests

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --all --all-features -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo build; cargo test --all --all-features to check that all the tests pass

@mxfp0
mxfp0 force-pushed the feat/completion-refactor branch from 7a10ab1 to b83ffaa Compare April 5, 2022 23:43
@sophiajt

sophiajt commented Apr 6, 2022

Copy link
Copy Markdown
Contributor

this looks good to me. good work!

@sophiajt
sophiajt merged commit 13869e7 into nushell:main Apr 6, 2022
@mxfp0
mxfp0 deleted the feat/completion-refactor branch April 6, 2022 09:23
@mxfp0 mxfp0 mentioned this pull request Apr 7, 2022
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