Skip to content

Custom completions with context are broken with if statments #12800

Open
@CarrotManMatt

Description

@CarrotManMatt

Describe the bug

When using custom completions, the docs suggest to retrieve the correct value from a record based on the context key. If however they choose to use an if statement to select the correct list of completions, it will fail.

What is most peculiar is the lack of consistency by which it will fail. Some examples below will cause an error, some will not.

How to reproduce

  1. Create a module file /tmp/example.nu
  2. Fill /tmp/example.nu with the given contents:
def "nu-complete example top-level-values" [] nothing -> list<str> {
    [
        "bools.are-funky"
        "it-aint.work-for-bools"
        "bad-is.boolean"
        "numbers-work-fine"
        "get-some.numbers-alright"
        "paths-play-funny"
        "paths.not-friendly"
    ]
}

def "nu-complete example sub-values" [context: string] nothing -> list<any> {
    let $final_context: string = ($context | str trim | split row " " | last)

    if ($final_context in [  # These are boolean types
        "bools.are-funky"
        "it-aint.work-for-bools"
        "bad-is.boolean"
    ]) {
        ["\"true\"", "\"false\""]
    } else if ($final_context in ["numbers-work-fine", "get-some.numbers-alright"]) {
        [5 6 8 10 12 14 16 20 24 32 32 36 52 56 84 100 104]
    } else if ($final_context in ["paths-play-funny"] or $final_context == "paths.not-friendly") {
        (ls | get name | where (($it | path type) == "dir") | each {|it| $it | path expand}) | append (
            if (((sys).host.name | str downcase | str ends-with "macos") or ((sys).host.name | str downcase | str ends-with "osx")) {
                $env.HOME | path join "Library/Caches/brokenexample"
            } else if ((sys).host.name | str downcase | str ends-with "windows") {
                $env.HOME | path join "AppData/Local/brokenexample/Cache"
            } else if ("XDG_CACHE_HOME" in $env) {
                $env.XDG_CACHE_HOME | path join "brokenexample"
            } else {
                $env.HOME | path join ".cache/brokenexample"
            }
        )
    }
}

# An example extern that has a broken custom completion
export extern "example" [
    top_level_value: string@"nu-complete example top-level-values"  # Pick a top_level_value.
    sub_value: any@"nu-complete example sub-values"                 # Pick a sub_value based on a top_level_value.
]
  1. Run use /tmp/example.nu * in the nushell terminal
  2. Type example , custom completions will be successful for the top_level_value
  3. Pick one of boolean suggested top_level_values
  4. Attempt tab-completion, it will fail after the first quotemark and present NO RECORDS FOUND
  5. Pick one of the number suggested top_level_values
  6. Attempt tab-completion, it will succeed at displaying all the numbers in the list
  7. Pick one of the path suggested top_level_values
  8. Attempt tab-completion, it will fail after the first matching set of base paths and present NO RECORDS FOUND (E.g. if the list of paths has multiple values that start /home/matt/ that is where it will complete to

Expected behavior

  • Boolean values are presented correctly and allowed to be autocompleted (E.g. "true", "false")
  • Path values are presented correctly and allowed to be autocompleted

Screenshots

image
image
image

Configuration

key value
version 0.93.0
major 0
minor 93
patch 0
branch
commit_hash 3b220e0
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.77.2 (25ef9e3d8 2024-04-09)
rust_channel 1.77.2-x86_64-unknown-linux-gnu
cargo_version cargo 1.77.2 (e52e36006 2024-03-26)
build_time 2024-04-30 23:06:38 +00:00
build_rust_channel release
allocator mimalloc
features dataframe, default, sqlite, static-link-openssl, system-clipboard, trash, which
installed_plugins

Additional context

I have attempted to provide enough different values such that these can be ruled out for the error. I suspect there is something going wrong with the if statements and changing the condition from an in to == sometimes fixes the issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    completionsIssues related to tab completionneeds-triageAn issue that hasn't had any proper look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions