Skip to content

Commit

Permalink
minor cleanup of completion command and add tests
Browse files Browse the repository at this point in the history
PR-URL: #2313
Credit: @nlf
Close: #2313
Reviewed-by: @isaacs
  • Loading branch information
nlf authored and isaacs committed Dec 11, 2020
1 parent 244c206 commit c243e3b
Show file tree
Hide file tree
Showing 2 changed files with 594 additions and 25 deletions.
32 changes: 7 additions & 25 deletions lib/completion.js
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit c243e3b

Please sign in to comment.