Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | 2x 30x 30x 3x 3x 27x 27x 27x 14x 14x 9x 9x 9x 4x 2x 12x 12x 13x 1x 1x 12x 12x 17x 17x 128x 17x 2x 2x 2x 2x 2x 12x 10x 11x 11x 80x 11x 8x 8x 10x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 19x 18x 11x 8x 3x 3x 2x 1x 3x 3x 3x 3x 3x 3x 3x 9x 9x 18x 18x 18x 6x 3x 46x 46x 53x 53x 19x 19x 19x 46x 184x 46x 45x 45x 19x 10x 13x 3x 167x 16x 27x 17x 35x 31x 13x 18x 4x 17x 52x 77x 17x 45x 17x 1x 1x 49x 74x 53x 19x 34x 21x | const {flatMap} = require('./flatMap') function getMatches (line, opts, cmd, {only}) { const values = justValues(opts) // 1. If line is empty if (line === '') { // 1.a. If the command only consists of pos args and the first pos arg has an only value // ... // 2.b. Else return all args and (the first) pos arg of command const matches = summarize(cmd, values) return match(matches, line) } const cmdStack = getCmdStack(cmd, values) const rest = getRest(values) // 2. If last value is not rest if (rest === null) { const cmd2 = cmdStack[cmdStack.length - 1] // 2.a. If only is true and last option of last command is invalid (its only does not contain its values) if (only && Array.isArray(cmd2.values) && cmd2.values.length > 0) { const lastOpt = cmd2.values[cmd2.values.length - 1] const rest = lastOpt.values[lastOpt.values.length - 1] if (lastOpt.only && !lastOpt.only.includes(rest)) { const matches = lastOpt.only.filter(val => val.startsWith(rest)) return complete(matches, line, rest) } } // 2.b. Else show args of last command const matches = summarize(cmd2, values) return match(matches, line) } // 3. If line ends with "-- " display all command options if (line.endsWith('-- ')) { const matches = summarize(cmd, values) return match(matches, line) } let matches = [] // 4. Check if rest is args in any of the stack commands (from right to left) // -> if args of subcommand, show all its opts // -> if args is option, show its only field or empty if missing for (let i = cmdStack.length - 1; i >= 0; i--) { const cmd2 = cmdStack[i] const optsWithRestArg = flatMap( cmd2.opts, opt => (opt.args || []).includes(rest) ? [opt] : [] ) if (optsWithRestArg.length > 0) { matches = flatMap(optsWithRestArg, opt => { Iif (isSubcommand(opt)) return summarize(opt, values) else { Iif (opt.descArg) return [opt.descArg] Eif (only === true && opt.only) return opt.only if (opt.types) return [`${(opt.types || []).map(type => '<' + type + '>').join(' ')}`] return [] } }) break } } if (matches.length > 0) return match(matches, line) // 5. Check if rest is start of args in any of the stack commands (from right to left) // -> if yes, matches are all right-most args that start with rest for (let i = cmdStack.length - 1; i >= 0; i--) { const cmd2 = cmdStack[i] const argsStartingWithRest = flatMap( cmd2.opts, opt => flatMap(opt.args || [], arg => arg.startsWith(rest) ? [arg] : []) ) if (argsStartingWithRest.length > 0) { matches = argsStartingWithRest break } } if (matches.length > 0) return complete(matches, line, rest) // 6. Check if the first positional argument has only values and the rest fits some values from only Eif (only === true) { const cmd = cmdStack[cmdStack.length - 1] const firstPosArg = cmd.opts.find(isPosArg) Eif (typeof firstPosArg !== 'undefined' && Array.isArray(firstPosArg.only)) { matches = firstPosArg.only.filter(value => value !== rest && value.startsWith(rest)) } } if (matches.length > 0) return complete(matches, line, rest) // 7. Otherwise // -> Matches are first non-required pos arg of rightmost command without values, or any subcommand of any of the stack commands for (let i = cmdStack.length - 1; i >= 0; i--) { const cmd2 = cmdStack[i] const args = summarize(cmd2, values) matches = [...matches, ...args] } return match(matches, line) } module.exports = { getMatches } function match (matches, line) { if (matches.length === 1) return [[line + ' ' + matches[0]], line] return [matches, line] } function complete (matches, line, rest) { if (matches.length === 1) { return [ [line.slice(0, line.length - rest.length) + matches[0]], line ] } const substr = shortestCommonSubstring(matches) if (rest !== substr) { return [ [line.slice(0, line.length - rest.length) + substr], line ] } return [ matches, line.slice(0, line.length - rest.length) + substr ] } function shortestCommonSubstring (matches) { Iif (matches.length === 0) return '' let shortest = matches[0] for (let i = 1; i < matches.length; i++) { const match = matches[i] Iif (match.length < shortest.length) { shortest = match } } let substr = '' shortestLoop: for (let i = 0; i < shortest.length; i++) { const ch = shortest[i] for (let j = 0; j < matches.length; j++) { const match = matches[j] const ch2 = match[i] if (ch !== ch2) break shortestLoop } substr += ch } return substr } function getCmdStack (cmd, values) { let stack = [cmd] for (let i = values.length - 1; i >= 0; i--) { const opt = values[i] if (isSubcommand(opt)) { const stack2 = getCmdStack(opt, opt.values) stack = [...stack, ...stack2] break } } return stack } function isSubcommand ({opts} = {opts: undefined}) { return Array.isArray(opts) } function getRest (values) { if (values.length === 0) return null const opt = values[values.length - 1] switch (true) { case isSubcommand(opt): return getRest(opt.values || []) case isPosArg(opt): return null case isRest(opt): return opt.values[0] default: return null } } function isPosArg ({key, args}) { return typeof key === 'string' && !Array.isArray(args) } function isRest (opt) { return Object.keys(opt).length === 1 && Array.isArray(opt.values) && opt.values.length === 1 } function isVariadic ({types}) { return typeof types === 'undefined' } function summarize (cmd, values) { const innerValues = values => { if (values.length > 0) { if (isSubcommand(values[0])) { return innerValues(values[0].values) } else { return values } } else { return values } } const firstEmptyPosArg = (cmd.opts || []).find( opt => isPosArg(opt) && (isVariadic(opt) || !innerValues(values).find(({key}) => opt.key === key)) ) const options = (cmd.opts || []).filter(opt => !isPosArg(opt)) return flatMap( [...options, ...(firstEmptyPosArg ? [firstEmptyPosArg] : [])], opt => opt.args ? opt.args : argName(opt) ) } function argName ({key, types, only, descArg}) { if (Array.isArray(only)) return only Iif (typeof descArg === 'string') return [descArg] Eif (Array.isArray(types) && types.length > 0) return [types.map(type => `<${type}>`).join(' ')] return [`<${key}>`] } function justValues (opts) { return flatMap(opts, opt => { if (Array.isArray(opt.values)) { if (isSubcommand(opt)) { return [{...opt, values: justValues(opt.values)}] } else { return [opt] } } else { return [] } }) } |