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

Completion on xrepo install packages #3214

Merged
merged 18 commits into from
Dec 28, 2022
Merged

Completion on xrepo install packages #3214

merged 18 commits into from
Dec 28, 2022

Conversation

glcraft
Copy link
Contributor

@glcraft glcraft commented Dec 27, 2022

Add completion on packages for xrepo install command.

Add a cache file to quick search over packages in repositories

@@ -79,7 +79,8 @@ function menu_options()
" - xrepo install -p iphoneos -a arm64 \"zlib >=1.2.0\"",
" - xrepo install -p android [--ndk=/xxx] -m debug \"pcre2 10.x\"",
" - xrepo install -p mingw [--mingw=/xxx] -k shared zlib",
" - xrepo install conan::zlib/1.2.11 vcpkg::zlib"}
" - xrepo install conan::zlib/1.2.11 vcpkg::zlib",
values = function (complete, opt) return import("private.xrepo.quick_search.completion")(complete, opt) end}
Copy link
Member

Choose a reason for hiding this comment

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

it will break xrepo install -h

error: attempt to index a boolean value (local 'complete')

you need handle help menu and disable complete.

if complete then
    -- do complete
end

@@ -79,7 +79,8 @@ function menu_options()
" - xrepo install -p iphoneos -a arm64 \"zlib >=1.2.0\"",
" - xrepo install -p android [--ndk=/xxx] -m debug \"pcre2 10.x\"",
" - xrepo install -p mingw [--mingw=/xxx] -k shared zlib",
" - xrepo install conan::zlib/1.2.11 vcpkg::zlib"}
" - xrepo install conan::zlib/1.2.11 vcpkg::zlib",
values = function (complete, opt) return import("private.xrepo.quick_search.completion")(complete, opt) end}
Copy link
Member

Choose a reason for hiding this comment

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

it will break xrepo install -h

error: attempt to index a boolean value (local 'complete')

you need handle help menu and disable complete.

if complete then
    -- do complete
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intercepted the help with "helpmenu" option in the completion funciton (in b1d694c)

Do you think it is enough ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, help menu should be able to handle the new kind of data.

In the values function for xrepo install, I return an array of objects with value, description and versions fields. I use value and description to build completion in json format (I put versions for an eventual later update of xrepo install completion).

For xrepo install help, it's useless to put values in help menu, but in other command completions like xrepo remove or xmake build for example, it could be interesting to change values function to also return name and description. Therefore, help menu have to handle those.

Copy link
Member

Choose a reason for hiding this comment

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

I intercepted the help with "helpmenu" option in the completion funciton (in b1d694c)

Do you think it is enough ?

sure

@waruqi
Copy link
Member

waruqi commented Dec 28, 2022

How should I test it, I run xrepo install z and press the tab button and nothing happens.

@glcraft
Copy link
Contributor Author

glcraft commented Dec 28, 2022

Here is some script to add in your shell profile file :
For Powershell

# xrepo completion
$xrepo_script = {
    param($wordToComplete, $commandAst, $cursorPosition)
        xmake lua 'private.xrepo.complete' $cursorPosition json $commandAst.ToString() | ConvertFrom-Json | Sort-Object -Property value | ForEach-Object {
            $hasdesc = [bool] $_.psobject.Properties['description']
            $desc = $hasdesc  ? " - $($_.description)" : ""
            [System.Management.Automation.CompletionResult]::new($_.value, "$($_.value)$desc", 'ParameterValue', $_.value)
        }
}
Register-ArgumentCompleter -Native -CommandName xrepo -ScriptBlock $xrepo_script

# xmake completion
$xmake_script = {
    param($wordToComplete, $commandAst, $cursorPosition)
        xmake lua 'private.utils.complete' $cursorPosition json $commandAst.ToString() | ConvertFrom-Json | Sort-Object -Property value | ForEach-Object {
            $hasdesc = [bool] $_.psobject.Properties['description']
            $desc = $hasdesc  ? " - $($_.description)" : ""
            [System.Management.Automation.CompletionResult]::new($_.value, "$($_.value)$desc", 'ParameterValue', $_.value)
        }
}
Register-ArgumentCompleter -Native -CommandName xmake -ScriptBlock $xmake_script

For Nu shell

let external_completer = {|spans| 
  {
    xmake: { XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua 'private.utils.complete' 0 'nospace-json' $spans | from json | sort-by value }
    xrepo: { xmake lua 'private.xrepo.complete' 0 'json' $spans | from json | sort-by value }
  } | get $spans.0 | each {|it| do $it}
}

let-env config = {
# ...
  completions: {
    case_sensitive: false # set to true to enable case-sensitive completions
    quick: true  # set this to false to prevent auto-selecting completions when only one remains
    partial: true  # set this to false to prevent partial filling of the prompt
    algorithm: "fuzzy"  # prefix or fuzzy
    external: {
      enable: true # set to false to prevent nushell looking into $env.PATH to find more suggestions, `false` recommended for WSL users as this look up my be very slow
      max_results: 100 # setting it lower can improve completion performance at the cost of omitting some options
      completer: $external_completer # check 'carapace_completer' above as an example
    }
  }
# ...
}

On Zsh (adapted from ~/.xmake/profile)

  # zsh parameter completion for xmake
  _xmake_zsh_complete() 
  {
    local completions=("$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.utils.complete 0 nospace "$words")")
    reply=( "${(ps:\n:)completions}" )
  }
  compctl -f -S "" -K _xmake_zsh_complete xmake
  # zsh parameter completion for xrepo
  _xrepo_zsh_complete() 
  {
    local completions=("$(xmake lua private.xrepo.complete 0 nospace "$words")")
    reply=( "${(ps:\n:)completions}" )
  }
  compctl -f -S "" -K _xrepo_zsh_complete xrepo

@waruqi
Copy link
Member

waruqi commented Dec 28, 2022

I use ./scripts/get.sh to install complete scripts, but I cannot get any response

my shell is bash.

cd xmake
./configure
make
./scripts/get.sh __local__ __install_only__
xrepo install z -> tab ..

@glcraft
Copy link
Contributor Author

glcraft commented Dec 28, 2022

./scripts/get.sh installs ./scripts/register_completion.sh into $HOME/.xmake/profile. Since I did not touch files in scripts, we have to register completion by hand for now.

I surely can implement xrepo completion in ./scripts/register_completion.sh and ./scripts/register_completion.ps1. For nu shell, I don't know how to install completion properly by script yet.

@waruqi
Copy link
Member

waruqi commented Dec 28, 2022

well, I need add this script to register-completions.sh


  # bash parameter completion for xrepo
  _xrepo_bash_complete()
  {
    local word=${COMP_WORDS[COMP_CWORD]}
    local completions
    completions="$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.xrepo.complete "${COMP_POINT}" "nospace-nokey" "${COMP_LINE}")"
    if [ $? -ne 0 ]; then
      completions=""
    fi
    COMPREPLY=( $(compgen -W "$completions") )
  }
  complete -o default -o nospace -F _xrepo_bash_complete xrepo

@waruqi waruqi merged commit c6af394 into xmake-io:dev Dec 28, 2022
@glcraft
Copy link
Contributor Author

glcraft commented Dec 28, 2022

Yes, as the xrepo completion work like xmake completion, it should work just fine.

Tell me, what are XMAKE_SKIP_HISTORY=1 and XMAKE_ROOT=y supposed to do with the completion lua script ?

@waruqi
Copy link
Member

waruqi commented Dec 28, 2022

XMAKE_ROOT Allow users to use xmake under root mode.

XMAKE_SKIP_HISTORY=1 skip xmake command record.

@waruqi
Copy link
Member

waruqi commented Dec 28, 2022

I added complete in register-completions.sh, but I cannot add it in ps1, because I have no environment to test it. You can add and test it.

@glcraft
Copy link
Contributor Author

glcraft commented Dec 28, 2022

Ok i'll do it,
I may also try to install xmake/xrepo compleytion for nu shell the scripts

@glcraft glcraft deleted the completer branch December 28, 2022 12:13
@xq114
Copy link
Contributor

xq114 commented Dec 29, 2022

Try xmake update --integrate, this command will install the completion script

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.

None yet

3 participants