repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
nushell/modules/mise-completions.nu
Raw# Query Mise's own CLI metadata instead of maintaining a duplicate extern suite.
# Protocol: github.com/jdx/usage, argv/src/complete.rs (usage-rs 7).
# Decode literal Nu strings only; never evaluate variables or expressions.
def literal-word [word: string] {
if ($word | str starts-with '`') {
return ($word | str trim --char '`')
}
if ($word | str starts-with "'") {
return ($word | str trim --char "'")
}
if ($word | str starts-with '"') {
# A still-open quote is normal while completing an argument.
return (try { $word | from nuon } catch { $word | str substring 1.. })
}
$word
}
def insertion [value: string] {
if $value =~ '[\s\[\]{}()<>$&;|#"\x27`\\]' {
$value | to nuon
} else {
$value
}
}
# Match Nu's default Windows PATH export: expand ~ and quote semicolons.
def windows-path-string [paths: list<string>] {
$paths | each {|entry|
let entry = if ($entry | str starts-with '~') { $entry | path expand --no-symlink } else { $entry }
if ($entry | str contains '"') { error make {msg: 'PATH entries cannot contain double quotes.'} }
if ($entry | str contains ';') { $'"($entry)"' } else { $entry }
} | str join (char esep)
}
export def main [place: record] {
let executable = (which --all mise | where type == external | get -o 0.path)
if $executable == null { return null }
let words = ($place.command | skip 1 | each { literal-word $in } | prepend 'mise')
let prefix = ($place.command | last)
mut environment = {
MISE_PREFER_OFFLINE: '1'
MISE_FETCH_REMOTE_VERSIONS_TIMEOUT: '3s'
MISE_HTTP_TIMEOUT: '3s'
MISE_HTTP_RETRIES: '0'
}
# Mise's dynamic completers use POSIX scripts, even on Windows.
# Supply Git's sh/cut/sed only to this subprocess, never to the parent PATH.
if $nu.os-info.family == 'windows' {
let git = (which --all git | where type == external | get -o 0.path)
if $git != null {
let bin = ($git | path dirname | path dirname | path join usr bin)
if ($bin | path join sh.exe | path exists) {
let names = ($env | columns | where { str lowercase | $in == 'path' } | prepend Path | uniq)
let converters = ($names | each {|name|
$env.ENV_CONVERSIONS? | default {} | get -o $name | get -o to_string
} | where { $in != null })
let parent_path = if ($converters | is-empty) {
windows-path-string $env.PATH
} else {
$env.PATH | do ($converters | first) $env.PATH
}
let child_path = $"(windows-path-string [$bin])(char esep)($parent_path)"
# Nu 0.116 can export inherited PATH and scoped Path together.
# Convert every spelling to the same child value, without hiding
# parent scopes or changing the caller's PATH/conversions.
$environment.ENV_CONVERSIONS = ($names | reduce --fold ($env.ENV_CONVERSIONS? | default {}) {|name, conversions|
let conversion = ($conversions | get -o $name | default {} | merge {to_string: { $child_path }})
$conversions | upsert $name $conversion
})
}
}
}
# PowerShell selects the rich TSV wire format; no PowerShell process runs.
let result = (with-env $environment {
^$executable __complete_word__ --shell powershell --words ...$words | complete
})
if $result.exit_code != 0 or $result.stderr =~ '(?m)^mise ERROR' {
error make {msg: 'Mise completion failed; inspect `mise __complete_word__` for diagnostics.'}
}
if $result.stderr =~ '(?i)(not recognized as|command not found)' {
error make {msg: 'Mise dynamic completion needs POSIX sh/cut/sed; Git for Windows provides them.'}
}
mut suggestions = []
let marker = (char --unicode '0001')
for line in ($result.stdout | lines | where { $in != '' }) {
if ($line | str starts-with $marker) {
let fields = ($line | str substring 1.. | split row (char tab))
let paths = match $fields.0 {
dirs => { $prefix | commandline complete --type directory --detailed }
commands => { $prefix | commandline complete --type command --detailed }
_ => { $prefix | commandline complete --type path --detailed }
}
let paths = if $fields.0 == 'extensions' {
$paths | where {|item|
(literal-word $item.value | path type) == dir or (
$fields | skip 1 | any {|extension|
(literal-word $item.value | str ends-with $".($extension | str trim --left --char '.')")
}
)
}
} else if $fields.0 == 'executables' {
$paths | where {|item|
(literal-word $item.value | path type) == dir or (
which --all (literal-word $item.value) | any {|entry| $entry.type == external }
)
}
} else { $paths }
# Let the outer completion engine supply the actual line span.
$suggestions = ($suggestions | append ($paths | reject span))
} else {
let fields = ($line | split row (char tab))
$suggestions = ($suggestions | append {
value: (insertion $fields.0)
description: ($fields | get -o 1 | default '')
display_override: ($fields | get -o 2 | default $fields.0)
append_whitespace: ($fields.0 !~ '[@/\\:=]$')
})
}
}
{completions: ($suggestions | uniq-by value), options: {filter: false}}
}