All files / repl/lib completer.js

70% Statements 140/200
58.33% Branches 7/12
100% Functions 3/3
70% Lines 140/200

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 20111x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 218x 218x 218x 218x 218x 218x 218x 13722x 13722x 13722x 13722x 218x 218x 218x 218x 218x 218x 218x 218x 218x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x                         275x 275x 275x                         275x 275x 275x                         275x 275x 275x                         275x 275x 275x                         275x 275x 57x 57x 57x 57x 218x 218x 218x 218x 218x 275x 116x 11x 11x 11x 11x 11x  
/**
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
/* eslint-disable no-underscore-dangle */
 
'use strict';
 
// MODULES //
 
var logger = require( 'debug' );
var objectKeys = require( '@stdlib/utils/keys' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var fsRegExp = require( './regexp_fs_aliases.js' );
var requireRegExp = require( './regexp_require.js' );
var workspaceRegExp = require( './regexp_workspace.js' );
var tutorialRegExp = require( './regexp_tutorial.js' );
var settingsRegExp = require( './regexp_settings.js' );
var reservedCharsRegExp = require( './regexp_reserved_syntax_characters.js' );
var completeRequire = require( './complete_require.js' );
var completeFS = require( './complete_fs.js' );
var completeWorkspace = require( './complete_workspace.js' );
var completeTutorial = require( './complete_tutorial.js' );
var completeSettings = require( './complete_settings.js' );
var completeExpression = require( './complete_expression.js' );
 
 
// VARIABLES //
 
var debug = logger( 'repl:completer:callback' );
 
 
// FUNCTIONS //
 
/**
* Normalizes a completion list.
*
* @private
* @param {Array} list - completion list
* @returns {Array} normalized completion list
*/
function normalize( list ) {
	var hash;
	var i;
 
	// Get unique values...
	hash = {};
	for ( i = 0; i < list.length; i++ ) {
		if ( !hasOwnProp( hash, list[ i ] ) ) {
			hash[ list[ i ] ] = true;
		}
	}
	list = objectKeys( hash );
 
	// TODO: sort such that printed columns are in lexicographic order, not rows, similar to bash behavior!
 
	// Sort the values in lexicographic order:
	list = list.sort();
 
	return list;
}
 
 
// MAIN //
 
/**
* Returns a callback for supporting TAB completion in a REPL environment.
*
* @private
* @param {REPL} repl - REPL instance
* @returns {Function} TAB completion callback
*/
function completer( repl ) {
	return complete;
 
	/**
	* Callback invoked upon a user entering the TAB character at the command prompt.
	*
	* @private
	* @param {string} line - current line
	* @param {Function} clbk - completion callback
	* @returns {void}
	*/
	function complete( line, clbk ) {
		var match;
		var exts;
		var res;
 
		debug( 'Line: %s', line );
 
		// Initialize an array for storing completion results:
		res = [];
 
		// Test if the line has an incomplete `require` expression:
		match = line.match( requireRegExp() );
		if ( match ) {
			debug( 'Detected incomplete `require` expression.' );

			exts = objectKeys( repl._context.require.extensions );
			debug( 'Supported `require` filename extensions: %s', exts.join( ', ' ) );

			line = completeRequire( res, match[ 1 ], repl._context.module.paths, exts ); // eslint-disable-line max-len
			res = normalize( res );

			debug( 'Completion filter: %s', line );
			debug( 'Results: %s', res.join( ', ' ) );
			return clbk( null, [ res, line ] );
		}
		// Test if the line has an incomplete file system expression:
		match = line.match( fsRegExp() );
		if ( match ) {
			debug( 'Detected incomplete file system expression.' );

			debug( 'Expression: %s', match[ 0 ] );
			debug( 'File system API: %s', match[ 1 ] );
			debug( 'Path to complete: %s', match[ 3 ] );
			line = completeFS( res, match[ 0 ], match[ 1 ], match[ 3 ] );
			res = normalize( res );

			debug( 'Completion filter: %s', line );
			debug( 'Results: %s', res.join( ', ' ) );
			return clbk( null, [ res, line ] );
		}
		// Test if the line has an incomplete workspace expression:
		match = line.match( workspaceRegExp() );
		if ( match ) {
			debug( 'Detected incomplete workspace expression.' );

			debug( 'Expression: %s', match[ 0 ] );
			debug( 'Workspace API: %s', match[ 1 ] );
			debug( 'Value to complete: %s', match[ 3 ] );
			line = completeWorkspace( res, repl, match[ 0 ], match[ 1 ], match[ 3 ] ); // eslint-disable-line max-len
			res = normalize( res );

			debug( 'Completion filter: %s', line );
			debug( 'Results: %s', res.join( ', ' ) );
			return clbk( null, [ res, line ] );
		}
		// Test if the line has an incomplete tutorial expression:
		match = line.match( tutorialRegExp() );
		if ( match ) {
			debug( 'Detected incomplete tutorial expression.' );

			debug( 'Expression: %s', match[ 0 ] );
			debug( 'Tutorial API: %s', match[ 1 ] );
			debug( 'Value to complete: %s', match[ 3 ] );
			line = completeTutorial( res, repl, match[ 0 ], match[ 1 ], match[ 3 ] ); // eslint-disable-line max-len
			res = normalize( res );

			debug( 'Completion filter: %s', line );
			debug( 'Results: %s', res.join( ', ' ) );
			return clbk( null, [ res, line ] );
		}
		// Test if the line has an incomplete settings expression:
		match = line.match( settingsRegExp() );
		if ( match ) {
			debug( 'Detected incomplete settings expression.' );

			debug( 'Expression: %s', match[ 0 ] );
			debug( 'Settings API: %s', match[ 1 ] );
			debug( 'Value to complete: %s', match[ 3 ] );
			line = completeSettings( res, repl, match[ 0 ], match[ 1 ], match[ 3 ] ); // eslint-disable-line max-len
			res = normalize( res );

			debug( 'Completion filter: %s', line );
			debug( 'Results: %s', res.join( ', ' ) );
			return clbk( null, [ res, line ] );
		}
		// Sanity check that we are attempting to complete something which is completable:
		if ( reservedCharsRegExp().test( line[ repl._rli.cursor-1 ] ) ) {
			debug( 'Detected attempt to trigger completion after a special character.' );
			debug( 'Results: %s', res.join( ', ' ) );
			return clbk( null, [ res, line ] );
		}
		debug( 'Attempting to complete an incomplete expression.' );
		line = completeExpression( res, repl._context, line );
		res = normalize( res );
		debug( 'Results: %s', res.join( ', ' ) );
		return clbk( null, [ res, line ] );
	}
}
 
 
// EXPORTS //
 
module.exports = completer;