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 | 2x 18x 2x 2x 16x 16x 16x 16x 3x 3x 3x 3x 3x 4x 2x 1x 1x 13x 13x 1x 1x 12x 12x 12x 17x 17x 128x 17x 2x 2x 2x 2x 2x 12x 10x 10x 11x 11x 80x 11x 8x 8x 10x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 7x 6x 11x 8x 3x 3x 2x 1x 3x 3x 3x 3x 3x 3x 3x 9x 9x 18x 18x 18x 6x 3x 27x 27x 30x 30x 11x 11x 11x 27x 59x 27x 27x 27x 27x 11x 13x 3x 28x 16x 41x 13x 9x 9x | const {flatMap} = require('./flatMap') function getMatches (line, values, cmd, {only}) { // 1. If line is empty, return all args and pos arg of command if (line === '') { const matches = summarize(cmd) return match(matches, line) } const cmdStack = getCmdStack(cmd, values) const rest = getRest(values) console.log('2.') // 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) Eif (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) return match(matches, line) } console.log('3.') // 3. If line ends with "-- " display all command options if (line.endsWith('-- ')) { const matches = summarize(cmd) return match(matches, line) } let matches = [] console.log('4.') // 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) 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) console.log('5.') // 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) console.log('6.') // 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) console.log('7.') // 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) 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) { console.log('values', values) Iif (values.length === 0) return null const opt = values[values.length - 1] switch (true) { case isSubcommand(opt): console.log('isSubcommand'); return getRest(opt.values || []) case isPosArg(opt): console.log('isPosArg'); return null // opt.values[0] case isRest(opt): console.log('isRest'); return opt.values[0] default: console.log('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 summarize (cmd) { return flatMap(cmd.opts || [], opt => opt.args ? opt.args : argName(opt)) } function argName ({key, types, only, descArg}) { if (Array.isArray(only)) return only if (typeof descArg === 'string') return [descArg] Eif (Array.isArray(types) && types.length > 0) return [types.map(type => `<${type}>`).join(' ')] return [`<${key}>`] } |