Skip to content

Commit c243e3b

Browse files
nlfisaacs
authored andcommitted
minor cleanup of completion command and add tests
PR-URL: #2313 Credit: @nlf Close: #2313 Reviewed-by: @isaacs
1 parent 244c206 commit c243e3b

2 files changed

Lines changed: 594 additions & 25 deletions

File tree

lib/completion.js

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const cmd = (args, cb) => compl(args).then(() => cb()).catch(cb)
5353

5454
// completion for the completion command
5555
const completion = async (opts, cb) => {
56-
if (opts.w > 3)
56+
if (opts.w > 2)
5757
return cb()
5858

5959
const { resolve } = require('path')
@@ -63,12 +63,12 @@ const completion = async (opts, cb) => {
6363
])
6464
const out = []
6565
if (zshExists)
66-
out.push('~/.zshrc')
66+
out.push(['>>', '~/.zshrc'])
6767

6868
if (bashExists)
69-
out.push('~/.bashrc')
69+
out.push(['>>', '~/.bashrc'])
7070

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

7474
const compl = async args => {
@@ -88,14 +88,11 @@ const compl = async args => {
8888
return dumpScript()
8989

9090
// ok we're actually looking at the envs and outputting the suggestions
91-
console.error({ COMP_CWORD, COMP_LINE, COMP_POINT })
92-
9391
// get the partial line and partial word,
9492
// if the point isn't at the end.
9593
// ie, tabbing at: npm foo b|ar
9694
const w = +COMP_CWORD
9795
const words = args.map(unescape)
98-
console.error({ words, args, w })
9996
const word = words[w]
10097
const line = COMP_LINE
10198
const point = +COMP_POINT
@@ -104,8 +101,6 @@ const compl = async args => {
104101

105102
// figure out where in that last word the point is.
106103
const partialWordRaw = args[w]
107-
console.error('partial word (args[%i])', w, partialWordRaw, args)
108-
109104
let i = partialWordRaw.length
110105
while (partialWordRaw.substr(0, i) !== partialLine.substr(-1 * i) && i > 0)
111106
i--
@@ -126,7 +121,6 @@ const compl = async args => {
126121
raw: args,
127122
}
128123

129-
console.error(opts)
130124
const wrap = getWrap(opts)
131125

132126
if (partialWords.slice(0, -1).indexOf('--') === -1) {
@@ -138,7 +132,6 @@ const compl = async args => {
138132
!isFlag(words[w - 1])) {
139133
// awaiting a value for a non-bool config.
140134
// don't even try to do this for now
141-
console.error('configValueCompl')
142135
return wrap(configValueCompl(opts))
143136
}
144137
}
@@ -151,9 +144,7 @@ const compl = async args => {
151144
const parsed = opts.conf =
152145
nopt(types, shorthands, partialWords.slice(0, -1), 0)
153146
// check if there's a command already.
154-
console.error('PARSED', parsed)
155147
const cmd = parsed.argv.remain[1]
156-
console.error('CMD', cmd)
157148
if (!cmd)
158149
return wrap(cmdCompl(opts))
159150

@@ -225,20 +216,15 @@ const escape = w => !/\s+/.test(w) ? w
225216
// Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
226217
// to: 'a', 'b c', or 'd' 'e'
227218
const getWrap = opts => compls => {
228-
console.error('WRAP', opts, compls)
229-
230219
if (!Array.isArray(compls))
231220
compls = compls ? [compls] : []
232221

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

236-
if (opts.partialWord) {
237-
console.error('HAS PARTIAL WORD', opts.partialWord, compls)
225+
if (opts.partialWord)
238226
compls = compls.filter(c => c.startsWith(opts.partialWord))
239-
}
240227

241-
console.error(compls, opts.partialWord)
242228
if (compls.length > 0)
243229
output(compls.join('\n'))
244230
}
@@ -251,7 +237,6 @@ const configCompl = opts => {
251237
const dashes = split[1]
252238
const no = split[2]
253239
const flags = configNames.filter(isFlag)
254-
console.error(flags)
255240
return allConfs.map(c => dashes + c)
256241
.concat(flags.map(f => dashes + (no || 'no-') + f))
257242
}
@@ -276,17 +261,14 @@ const isFlag = word => {
276261
// complete against the npm commands
277262
// if they all resolve to the same thing, just return the thing it already is
278263
const cmdCompl = opts => {
279-
console.error('CMD COMPL', opts.partialWord)
280264
const matches = fullList.filter(c => c.startsWith(opts.partialWord))
281-
console.error('MATCHES', matches)
282265
if (!matches.length)
283266
return matches
284267

285268
const derefs = new Set([...matches.map(c => deref(c))])
286-
if (derefs.size === 1) {
287-
console.error('ONLY ONE MATCH', derefs)
269+
if (derefs.size === 1)
288270
return [...derefs]
289-
}
271+
290272
return fullList
291273
}
292274

0 commit comments

Comments
 (0)