All files / src/emulator auto-complete.js

96% Statements 24/25
87.5% Branches 7/8
100% Functions 6/6
96% Lines 24/25

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 681x 1x 1x               1x 8x   8x                   1x 10x       10x   8x 13x     10x                   1x 17x     17x   17x   17x   17x   17x 5x     12x 13x 13x   13x      
import * as PathUtil from 'fs/util/path-util';
import * as GlobUtil from 'fs/util/glob-util';
import { isCommandSet, getCommandNames, getCommandOptDef } from 'emulator-state/command-mapping';
 
/**
 * Suggest command names
 * @param  {Map}    cmdMapping     command mapping
 * @param  {string} partialStr     partial user input of a command
 * @return {array}                 list of possible text suggestions
 */
export const suggestCommands = (cmdMapping, partialStr) => {
  const commandNameSeq = getCommandNames(cmdMapping);
 
  return [...GlobUtil.globSeq(commandNameSeq, `${partialStr}*`)];
};
 
/**
 * Suggest command options
 * @param  {Map}    cmdMapping     command mapping
 * @param  {string} commandName    name of the command user is running
 * @param  {string} partialStr     partial user input of a command (excluding the command name)
 * @return {array}                 list of possible text suggestions
 */
export const suggestCommandOptions = (cmdMapping, commandName, partialStr) => {
  Iif (!isCommandSet(cmdMapping, commandName)) {
    return [];
  }
 
  const optDefSeq = getCommandOptDef(cmdMapping, commandName)
    .keySeq()
    .flatMap(opts =>
      opts.split(',').map(opt => opt.trim())
    );
 
  return [...GlobUtil.globSeq(optDefSeq, `${partialStr}*`)];
};
 
/**
 * Suggest file and folder names from partially completed user input
 * @param  {Map}    fileSystem file system
 * @param  {string} cwd        current working directory
 * @param  {string} partialStr partial string to base suggestions on (excluding the command name)
 * @return {array}             list of possible text suggestions
 */
export const suggestFileSystemNames = (fileSystem, cwd, partialStr) => {
  const path = PathUtil.toAbsolutePath(partialStr, cwd);
 
  // complete name of a folder or file
  const completeNamePattern = `${path}*`;
  // complete child folder name
  const completeSubfolderPattern = path === '/' ? '/*' : `${path}*/*`;
  // only complete child folders when the path ends with / (which marks a directory path)
  const globPattern = partialStr.endsWith('/') ? completeSubfolderPattern : completeNamePattern;
 
  const childPaths = GlobUtil.globPaths(fileSystem, globPattern);
 
  if (PathUtil.isAbsPath(partialStr)) {
    return [...childPaths]; // absolute paths
  }
 
  return [...childPaths.map(path => {
    const pathPartsWithoutTail = PathUtil.toPathParts(partialStr).slice(0, -1);
    const newTail = PathUtil.getLastPathPart(path);
 
    return PathUtil.toPath(pathPartsWithoutTail.concat(newTail));
  })]; // relative paths
};