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

minor cleanup of completion command and add tests #2313

Merged
merged 1 commit into from Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 7 additions & 25 deletions lib/completion.js
Expand Up @@ -53,7 +53,7 @@ const cmd = (args, cb) => compl(args).then(() => cb()).catch(cb)

// completion for the completion command
const completion = async (opts, cb) => {
if (opts.w > 3)
if (opts.w > 2)
return cb()

const { resolve } = require('path')
Expand All @@ -63,12 +63,12 @@ const completion = async (opts, cb) => {
])
const out = []
if (zshExists)
out.push('~/.zshrc')
out.push(['>>', '~/.zshrc'])

if (bashExists)
out.push('~/.bashrc')
out.push(['>>', '~/.bashrc'])

cb(null, opts.w === 2 ? out.map(m => ['>>', m]) : out)
cb(null, out)
}

const compl = async args => {
Expand All @@ -88,14 +88,11 @@ const compl = async args => {
return dumpScript()

// ok we're actually looking at the envs and outputting the suggestions
console.error({ COMP_CWORD, COMP_LINE, COMP_POINT })

// get the partial line and partial word,
// if the point isn't at the end.
// ie, tabbing at: npm foo b|ar
const w = +COMP_CWORD
const words = args.map(unescape)
console.error({ words, args, w })
const word = words[w]
const line = COMP_LINE
const point = +COMP_POINT
Expand All @@ -104,8 +101,6 @@ const compl = async args => {

// figure out where in that last word the point is.
const partialWordRaw = args[w]
console.error('partial word (args[%i])', w, partialWordRaw, args)

let i = partialWordRaw.length
while (partialWordRaw.substr(0, i) !== partialLine.substr(-1 * i) && i > 0)
i--
Expand All @@ -126,7 +121,6 @@ const compl = async args => {
raw: args,
}

console.error(opts)
const wrap = getWrap(opts)

if (partialWords.slice(0, -1).indexOf('--') === -1) {
Expand All @@ -138,7 +132,6 @@ const compl = async args => {
!isFlag(words[w - 1])) {
// awaiting a value for a non-bool config.
// don't even try to do this for now
console.error('configValueCompl')
return wrap(configValueCompl(opts))
}
}
Expand All @@ -151,9 +144,7 @@ const compl = async args => {
const parsed = opts.conf =
nopt(types, shorthands, partialWords.slice(0, -1), 0)
// check if there's a command already.
console.error('PARSED', parsed)
const cmd = parsed.argv.remain[1]
console.error('CMD', cmd)
if (!cmd)
return wrap(cmdCompl(opts))

Expand Down Expand Up @@ -225,20 +216,15 @@ const escape = w => !/\s+/.test(w) ? w
// Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
// to: 'a', 'b c', or 'd' 'e'
const getWrap = opts => compls => {
console.error('WRAP', opts, compls)

if (!Array.isArray(compls))
compls = compls ? [compls] : []

compls = compls.map(c =>
Array.isArray(c) ? c.map(escape).join(' ') : escape(c))

if (opts.partialWord) {
console.error('HAS PARTIAL WORD', opts.partialWord, compls)
if (opts.partialWord)
compls = compls.filter(c => c.startsWith(opts.partialWord))
}

console.error(compls, opts.partialWord)
if (compls.length > 0)
output(compls.join('\n'))
}
Expand All @@ -251,7 +237,6 @@ const configCompl = opts => {
const dashes = split[1]
const no = split[2]
const flags = configNames.filter(isFlag)
console.error(flags)
return allConfs.map(c => dashes + c)
.concat(flags.map(f => dashes + (no || 'no-') + f))
}
Expand All @@ -276,17 +261,14 @@ const isFlag = word => {
// complete against the npm commands
// if they all resolve to the same thing, just return the thing it already is
const cmdCompl = opts => {
console.error('CMD COMPL', opts.partialWord)
const matches = fullList.filter(c => c.startsWith(opts.partialWord))
console.error('MATCHES', matches)
if (!matches.length)
return matches

const derefs = new Set([...matches.map(c => deref(c))])
if (derefs.size === 1) {
console.error('ONLY ONE MATCH', derefs)
if (derefs.size === 1)
return [...derefs]
}

return fullList
}

Expand Down