11cb0ef41Sopenharmony_ci# npm completions for Fish shell
21cb0ef41Sopenharmony_ci# This script is a work in progress and does not fall under the normal semver contract as the rest of npm.
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci# __fish_npm_needs_command taken from:
51cb0ef41Sopenharmony_ci# https://stackoverflow.com/questions/16657803/creating-autocomplete-script-with-sub-commands
61cb0ef41Sopenharmony_cifunction __fish_npm_needs_command
71cb0ef41Sopenharmony_ci    set -l cmd (commandline -opc)
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci    if test (count $cmd) -eq 1
101cb0ef41Sopenharmony_ci        return 0
111cb0ef41Sopenharmony_ci    end
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci    return 1
141cb0ef41Sopenharmony_ciend
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci# Taken from https://github.com/fish-shell/fish-shell/blob/HEAD/share/completions/npm.fish
171cb0ef41Sopenharmony_cifunction __fish_complete_npm -d "Complete the commandline using npm's 'completion' tool"
181cb0ef41Sopenharmony_ci    # tell npm we are fish shell
191cb0ef41Sopenharmony_ci    set -lx COMP_FISH true
201cb0ef41Sopenharmony_ci    if command -sq npm
211cb0ef41Sopenharmony_ci        # npm completion is bash-centric, so we need to translate fish's "commandline" stuff to bash's $COMP_* stuff
221cb0ef41Sopenharmony_ci        # COMP_LINE is an array with the words in the commandline
231cb0ef41Sopenharmony_ci        set -lx COMP_LINE (commandline -opc)
241cb0ef41Sopenharmony_ci        # COMP_CWORD is the index of the current word in COMP_LINE
251cb0ef41Sopenharmony_ci        # bash starts arrays with 0, so subtract 1
261cb0ef41Sopenharmony_ci        set -lx COMP_CWORD (math (count $COMP_LINE) - 1)
271cb0ef41Sopenharmony_ci        # COMP_POINT is the index of point/cursor when the commandline is viewed as a string
281cb0ef41Sopenharmony_ci        set -lx COMP_POINT (commandline -C)
291cb0ef41Sopenharmony_ci        # If the cursor is after the last word, the empty token will disappear in the expansion
301cb0ef41Sopenharmony_ci        # Readd it
311cb0ef41Sopenharmony_ci        if test (commandline -ct) = ""
321cb0ef41Sopenharmony_ci            set COMP_CWORD (math $COMP_CWORD + 1)
331cb0ef41Sopenharmony_ci            set COMP_LINE $COMP_LINE ""
341cb0ef41Sopenharmony_ci        end
351cb0ef41Sopenharmony_ci        command npm completion -- $COMP_LINE 2>/dev/null
361cb0ef41Sopenharmony_ci    end
371cb0ef41Sopenharmony_ciend
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci# flush out what ships with fish
401cb0ef41Sopenharmony_cicomplete -e npm
41