Skip to content

Improve ZSH completions - #150

Closed
srealmoreno wants to merge 3 commits into
oven-sh:mainfrom
srealmoreno:patch-1
Closed

srealmoreno wants to merge 3 commits into
oven-sh:mainfrom
srealmoreno:patch-1

Conversation

@srealmoreno

Copy link
Copy Markdown

Refactoring the code and adding improvements

Autocomplete for --define flag

  • Reads the environment variables and autocomplete them in json format
  • Autocomplete the environment variable
    process.env.NODE_ENV

define completion

Autocomplete for --load flag

Auto complete file extensions with valid loaders

load completion

Fixed bug when autocompleting recent packages

Previously, when adding multiple packages in the same command, the autocomplete did not work well.

bun add react vite esbuild nanoid

Before:
recent packages completion before

After:
Segmentation in

  • recent
  • popular

recent packages completion after

Autocomplete for remove command

Segmentation in

  • Dependencies
  • Dev Dependencies
  • Optional Dependencies
  • Peer Dependencies

remove package completion

Segmentation for the run command

Segmentation in

  • files
  • binaries
  • scripts

run completion

Refactoring the code and adding improvements
Autocomplete for --define flag
Reads the environment variables and autocomplete them in json format
Autocomplete the environment variable process.env.NODE_ENV
define completion

Autocomplete for --load flag
Auto complete file extensions with valid loaders

load completion

Fixed bug when autocompleting recent packages
Previously, when adding multiple packages in the same command, the autocomplete did not work well.

bun add react vite esbuild nanoid
Before: recent packages completion before

After: Segmentation in

recent
popular
recent packages completion after

Autocomplete for remove command
Segmentation in

Dependencies
Dev Dependencies
Optional Dependencies
Peer Dependencies
remove package completion

Segmentation for the run command
Segmentation in

files
binaries
scripts
run completion
Comment thread completions/bun.zsh
_regex=' -(d|-define) process\.env\.NODE_ENV:(.+) '
if [[ ! ${words[@]} =~ $_regex ]]; then
_node_env_describe=(
'development -- Define mode as development'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this is great

Comment thread completions/bun.zsh Outdated
Comment thread completions/bun.zsh Outdated
Comment thread completions/bun.zsh
@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

Overall, this is great. The only part I'm not sure about is cross-platform compatibility with grep and sed

I ran into a similar issue with bun's Makefile, ended up doing this:

SED = $(shell which gsed || which sed)

homebrew aliases GNU versions of coreutils with g, so ggrep and gsed – but not everyone bothers to install the coreutils (or grep) homebrew package

Maybe we could assume the commands will work but re-run without the sed or grep or jq specific features if it returns a non-zero exit code? That way the happy path still works

@Jarred-Sumner Jarred-Sumner changed the title Refactoring the code and adding improvements Improve ZSH completions Apr 19, 2022
Changing Perl RegExp to BRE RegExp because because is not supported by MacOS and BSD
@srealmoreno

Copy link
Copy Markdown
Author

Updating RegExp to be compatible with MacOS and BSD and checking if the jq command is installed

  • RegExp

Since grep and sed in Macos don't support Perl RegExp I updated to BRE RegExp.

Before:

_envs=(
  ${(f)"$(export | sed -r "/^\w+=(true|false|null|[0-9]+)$/! s/='?(.*?)('|$)/=\"\1\"/")"}
)

_recent=($(history -n | grep -oP '(?<=^bun add ).+'))

After:

_envs=(
  ${(f)"$(export | sed "/^\w\+=\(true\|false\|null\|[0-9]\+\)$/! s/='\?\(.*\)\('\|$\)/=\"\1\"/")"}
)

_recent=($(history -n | sed -n 's/^\(bun[[:space:]]\+add[[:space:]]\+\)\(.\+\)/\2/p'))
  • jq

Since jq is not an essential tool for distributions there is a chance that it is not installed, so I added a check to determine if it is installed.

if ! command -v jq >/dev/null 2>&1; then
  _message 'jq is required to complete bun remove command'
  return
fi

@srealmoreno srealmoreno removed their assignment Apr 21, 2022
@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

Unfortunately, sed has a similar issue on macOS as grep

bun add completions don't work after the changes:
image

bun remove works though!
image

@Jarred-Sumner
Jarred-Sumner removed their request for review April 21, 2022 22:56
@srealmoreno

Copy link
Copy Markdown
Author

Would you mind providing more information about the error?
Unfortunately I don't have a MacOs to perform the necessary tests.

Please run this and show me the output:

# Test of the `add` command:
history -n | sed -n 's/^\(bun[[:space:]]\+add[[:space:]]\+\)\(.\+\)/\2/p'
# Test of the `--define` option:
export | sed "/^\w\+=\(true\|false\|null\|[0-9]\+\)$/! s/='\?\(.*\)\('\|$\)/=\"\1\"/"

@alexkuz

alexkuz commented May 15, 2022

Copy link
Copy Markdown
Contributor

@srealmoreno if this is still relevant: on Mac, it would work like this

history -n | sed -n 's/^\(bun[[:space:]]\{1,\}add[[:space:]]\{1,\}\)\(.\{1,\}\)$/\2/p'

or with extended regex:

history -n | sed -nE 's/^(bun[[:space:]]+add[[:space:]]+)(.+)$/\2/p'

Similarly for export (slightly corrected version):

export | sed -E "/^[[:alnum:]_]+=(true|false|null|[0-9])$/! s/='(.*)'$|=(.*)$/=\"\1\2\"/"

(branches don't work in BRE if I'm not mistaken)

@srealmoreno

Copy link
Copy Markdown
Author

@alexkuz Thanks, I'll add it right now.

- Fix RegExp for Macosx
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.

3 participants