All files / src/repl/completer getMatches.js

90% Statements 108/120
75% Branches 60/80
94.74% Functions 18/19
93.55% Lines 87/93

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 2102x       16x 2x 2x     14x 14x     14x 1x 1x 1x       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         7x 6x       9x 8x           1x   1x 1x                         1x   1x 1x 1x   1x         1x   1x 3x   3x 6x 6x   6x     2x     1x       23x   23x 26x   26x 9x 9x 9x       23x       51x       23x   23x   23x 9x 13x   1x         26x       1x       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)
 
    // 2. If last value is command and not rest, show args of last command
    if (rest === null) {
      const cmd2 = cmdStack[cmdStack.length - 1]
      const matches = summarize(cmd2)
      return match(matches, line)
    }
 
    // 3. If line ends with "-- " display all command options
    if (line.endsWith('-- ')) {
      const matches = summarize(cmd)
      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)
          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.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)
      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)
 
    Eif (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) {
    Iif (values.length === 0) return null
  
    const opt = values[values.length - 1]
  
    switch (true) {
      case isSubcommand(opt): return getRest(opt.values || [])
      case isPosArg(opt):     return opt.values[0]
      case isRest(opt):       return opt.values[0]
      default:                return null
    }
  }
 
  function isPosArg ({args}) {
    return !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 (typeof descArg === 'string') return [descArg]
    if (Array.isArray(only)) return only
    Eif (Array.isArray(types) && types.length > 0) return [types.map(type => `<${type}>`).join(' ')]
    return [`<${key}>`]
  }