Skip to content

Commit d624a68

Browse files
committed
REPL: tab complete for Arrays
The tab complete for Arrays will include any integer indexes. This is suboptimal because obj.21 is not valid syntax and a large array will contain a very cluttered list.
1 parent 3f5d584 commit d624a68

2 files changed

Lines changed: 121 additions & 1 deletion

File tree

lib/repl.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,20 @@ REPLServer.prototype.complete = function(line, callback) {
580580
// if (e) console.log(e);
581581

582582
if (obj != null) {
583-
if (util.isObject(obj) || util.isFunction(obj)) {
583+
if (util.isArray(obj)) {
584+
// Array elements will contain many integer values
585+
// if we return them in memberGroups they will appear
586+
// as obj.0 obj.100 etc. which is not valid syntax.
587+
// Additionally for large arrays this can make for a
588+
// very cluttered list. So I filter them out.
589+
memberGroups
590+
.push(Object
591+
.getOwnPropertyNames(obj)
592+
.filter(function(element) {
593+
// parseInt('0', 10) == 0
594+
return element == 0 ? false : !parseInt(element, 10);
595+
}));
596+
} else if (util.isObject(obj) || util.isFunction(obj)) {
584597
memberGroups.push(Object.getOwnPropertyNames(obj));
585598
}
586599
// works for non-objects
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var util = require('util');
25+
26+
var repl = require('repl');
27+
28+
// A stream to push an array into a REPL
29+
function ArrayStream() {
30+
this.run = function(data) {
31+
var self = this;
32+
data.forEach(function(line) {
33+
self.emit('data', line + '\n');
34+
});
35+
}
36+
}
37+
util.inherits(ArrayStream, require('stream').Stream);
38+
ArrayStream.prototype.readable = true;
39+
ArrayStream.prototype.writable = true;
40+
ArrayStream.prototype.resume = function() {};
41+
ArrayStream.prototype.write = function() {};
42+
43+
var array_elements = [
44+
[ 'asdf.__defineGetter__', 'asdf.__defineSetter__',
45+
'asdf.__lookupGetter__', 'asdf.__lookupSetter__',
46+
'asdf.__proto__', 'asdf.constructor',
47+
'asdf.hasOwnProperty', 'asdf.isPrototypeOf',
48+
'asdf.propertyIsEnumerable',
49+
'asdf.toLocaleString', 'asdf.toString',
50+
'asdf.valueOf', '', 'asdf.concat', 'asdf.constructor',
51+
'asdf.every', 'asdf.filter',
52+
'asdf.forEach', 'asdf.indexOf',
53+
'asdf.join', 'asdf.lastIndexOf',
54+
'asdf.length', 'asdf.map',
55+
'asdf.pop', 'asdf.push', 'asdf.reduce',
56+
'asdf.reduceRight', 'asdf.reverse', 'asdf.shift',
57+
'asdf.slice', 'asdf.some', 'asdf.sort', 'asdf.splice',
58+
'asdf.toLocaleString', 'asdf.toString', 'asdf.unshift',
59+
'', 'asdf.length' ],
60+
'asdf.' ];
61+
62+
var object_elements = [
63+
[ 'asdf.__defineGetter__', 'asdf.__defineSetter__',
64+
'asdf.__lookupGetter__', 'asdf.__lookupSetter__',
65+
'asdf.__proto__', 'asdf.constructor',
66+
'asdf.hasOwnProperty', 'asdf.isPrototypeOf',
67+
'asdf.propertyIsEnumerable',
68+
'asdf.toLocaleString', 'asdf.toString',
69+
'asdf.valueOf', '', 'asdf.1'],
70+
'asdf.' ];
71+
72+
var putIn = new ArrayStream();
73+
var testMe = repl.start('', putIn);
74+
75+
// Tab Complete will not return integer indexes
76+
putIn.run(['.clear']);
77+
putIn.run([
78+
'asdf = [\'one\', 2, 3]',
79+
]);
80+
testMe.complete('asdf.', function(error, data) {
81+
assert.deepEqual(data, array_elements);
82+
});
83+
84+
// Tab Complete will return additional non-integer indexes
85+
putIn.run(['.clear']);
86+
putIn.run([
87+
'asdf = [\'one\', 2, 3]',
88+
'asdf.qwer = 1'
89+
]);
90+
91+
var array_with_extra = array_elements;
92+
array_with_extra[0].push('qwer');
93+
94+
testMe.complete('asdf.', function(error, data) {
95+
assert.deepEqual(data, array_with_extra);
96+
});
97+
98+
// Tab Complete will return integer indexes on an object
99+
putIn.run(['.clear']);
100+
putIn.run([
101+
'asdf = {}',
102+
'asdf[1] = 1'
103+
]);
104+
testMe.complete('asdf.', function(error, data) {
105+
assert.deepEqual(data, object_elements);
106+
});
107+

0 commit comments

Comments
 (0)