0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00

better autocomplete

This commit is contained in:
Mathias Stearn 2010-08-18 19:44:27 -04:00
parent 7a7cc4adfa
commit 8360e22198
2 changed files with 46 additions and 30 deletions

View File

@ -857,17 +857,24 @@ const StringData _jscode_raw_utils =
"\n"
"shellAutocomplete = function (/*prefix*/){ // outer scope function called on init. Actual function at end\n"
"\n"
"// from w3schools reference\n"
"var builtinMethods = {}; // uses constructor objects as keys\n"
"builtinMethods[Array] = \"constructor length prototype concat join pop push reverse shift slice sort splice toString unshift valueOf\".split(' ');\n"
"builtinMethods[Boolean] = \"constructor prototype toString valueOf\".split(' ');\n"
"builtinMethods[Date] = \"constructor prototype getDate getDay getFullYear getHours getMilliseconds getMinutes getMonth getSeconds getTime getTimezoneOffset getUTCDate getUTCDay getUTCFullYear getUTCHours getUTCMilliseconds getUTCMinutes getUTCMonth getUTCSeconds getYear parse setDate setFullYear setHours setMilliseconds setMinutes setMonth setSeconds setTime setUTCDate setUTCFullYear setUTCHours setUTCMilliseconds setUTCMinutes setUTCMonth setUTCSeconds setYear toDateString toGMTString toLocaleDateString toLocaleTimeString toLocaleString toString toTimeString toUTCString UTC valueOf\".split(' ');\n"
"builtinMethods[Math] = \"E LN2 LN10 LOG2E LOG10E PI SQRT1_2 SQRT2 abs acos asin atan atan2 ceil cos exp floor log max min pow random round sin sqrt tan\".split(' ');\n"
"builtinMethods[Number] = \"constructor prototype MAX_VALUE MIN_VALUE NEGATIVE_INFINITY POSITIVE_INFINITY toExponential toFixed toPrecision toString valueOf\".split(' ');\n"
"builtinMethods[RegExp] = \"constructor prototype global ignoreCase lastIndex multiline source compile exec test toString valueOf\".split(' ');\n"
"builtinMethods[String] = \"constructor prototype length charAt charCodeAt concat fromCharCode indexOf lastIndexOf match replace search slice split substr substring toLowerCase toString toUpperCase valueOf\".split(' ');\n"
"var universalMethods = \"constructor prototype toString valueOf toLocaleString hasOwnProperty propertyIsEnumerable\".split(' ');\n"
"\n"
"var extraGlobals = \"Infinity NaN undefined null true false decodeURI decodeURIComponent encodeURI encodeURIComponent escape eval isFinite isNaN parseFloat parseInt unescape Array Boolean Date Math Number RegExp String\".split(' ');\n"
"var builtinMethods = {}; // uses constructor objects as keys\n"
"builtinMethods[Array] = \"length concat join pop push reverse shift slice sort splice unshift indexOf lastIndexOf every filter forEach map some\".split(' ');\n"
"builtinMethods[Boolean] = \"\".split(' '); // nothing more than universal methods\n"
"builtinMethods[Date] = \"getDate getDay getFullYear getHours getMilliseconds getMinutes getMonth getSeconds getTime getTimezoneOffset getUTCDate getUTCDay getUTCFullYear getUTCHours getUTCMilliseconds getUTCMinutes getUTCMonth getUTCSeconds getYear parse setDate setFullYear setHours setMilliseconds setMinutes setMonth setSeconds setTime setUTCDate setUTCFullYear setUTCHours setUTCMilliseconds setUTCMinutes setUTCMonth setUTCSeconds setYear toDateString toGMTString toLocaleDateString toLocaleTimeString toTimeString toUTCString UTC\".split(' ');\n"
"builtinMethods[Math] = \"E LN2 LN10 LOG2E LOG10E PI SQRT1_2 SQRT2 abs acos asin atan atan2 ceil cos exp floor log max min pow random round sin sqrt tan\".split(' ');\n"
"builtinMethods[Number] = \"MAX_VALUE MIN_VALUE NEGATIVE_INFINITY POSITIVE_INFINITY toExponential toFixed toPrecision\".split(' ');\n"
"builtinMethods[RegExp] = \"global ignoreCase lastIndex multiline source compile exec test\".split(' ');\n"
"builtinMethods[String] = \"length charAt charCodeAt concat fromCharCode indexOf lastIndexOf match replace search slice split substr substring toLowerCase toUpperCase\".split(' ');\n"
"builtinMethods[Function] = \"call apply\".split(' ');\n"
"builtinMethods[Object] = \"bsonsize\".split(' ');\n"
"\n"
"builtinMethods[Mongo] = \"find update insert remove\".split(' ');\n"
"builtinMethods[BinData] = \"hex base64 length subtype\".split(' ');\n"
"builtinMethods[NumberLong] = \"toNumber\".split(' ');\n"
"\n"
"var extraGlobals = \"Infinity NaN undefined null true false decodeURI decodeURIComponent encodeURI encodeURIComponent escape eval isFinite isNaN parseFloat parseInt unescape Array Boolean Date Math Number RegExp String print load gc MinKey MaxKey Mongo NumberLong ObjectId DBPointer UUID BinData Map\".split(' ');\n"
"\n"
"var isPrivate = function(name){\n"
"if (shellAutocomplete.showPrivate) return false;\n"
@ -879,7 +886,7 @@ const StringData _jscode_raw_utils =
"\n"
"var customComplete = function(obj){\n"
"try {\n"
"if(obj.constructor.autocomplete){\n"
"if(obj.__proto__.constructor.autocomplete){\n"
"var ret = obj.constructor.autocomplete(obj);\n"
"if (ret.constructor != Array){\n"
"print(\"\\nautocompleters must return real Arrays\");\n"
@ -902,7 +909,7 @@ const StringData _jscode_raw_utils =
"var parts = prefix.split('.');\n"
"for (var p=0; p < parts.length - 1; p++){ // doesn't include last part\n"
"curObj = curObj[parts[p]];\n"
"if (curObj === undefined)\n"
"if (curObj == null)\n"
"return [];\n"
"}\n"
"\n"
@ -912,10 +919,11 @@ const StringData _jscode_raw_utils =
"begining += '.';\n"
"\n"
"var possibilities = new Array().concat(\n"
"universalMethods,\n"
"Object.keySet(curObj),\n"
"Object.keySet(curObj.constructor.prototype),\n"
"Object.keySet(curObj.__proto__),\n"
"builtinMethods[curObj] || [], // curObj is a builtin constructor\n"
"builtinMethods[curObj.constructor] || [], // curObj is made from a builtin constructor\n"
"builtinMethods[curObj.__proto__.constructor] || [], // curObj is made from a builtin constructor\n"
"curObj == global ? extraGlobals : [],\n"
"customComplete(curObj)\n"
");\n"
@ -923,7 +931,7 @@ const StringData _jscode_raw_utils =
"var ret = [];\n"
"for (var i=0; i < possibilities.length; i++){\n"
"var p = possibilities[i];\n"
"if (curObj[p] === undefined && curObj != global) continue; // extraGlobals aren't in the global object\n"
"if (typeof(curObj[p]) == \"undefined\" && curObj != global) continue; // extraGlobals aren't in the global object\n"
"if (p.length == 0 || p.length < lastPrefix.length) continue;\n"
"if (isPrivate(p)) continue;\n"
"if (p.match(/^[0-9]+$/)) continue; // don't array number indexes\n"

View File

@ -852,17 +852,24 @@ shellPrintHelper = function (x) {
shellAutocomplete = function (/*prefix*/){ // outer scope function called on init. Actual function at end
// from w3schools reference
var builtinMethods = {}; // uses constructor objects as keys
builtinMethods[Array] = "constructor length prototype concat join pop push reverse shift slice sort splice toString unshift valueOf".split(' ');
builtinMethods[Boolean] = "constructor prototype toString valueOf".split(' ');
builtinMethods[Date] = "constructor prototype getDate getDay getFullYear getHours getMilliseconds getMinutes getMonth getSeconds getTime getTimezoneOffset getUTCDate getUTCDay getUTCFullYear getUTCHours getUTCMilliseconds getUTCMinutes getUTCMonth getUTCSeconds getYear parse setDate setFullYear setHours setMilliseconds setMinutes setMonth setSeconds setTime setUTCDate setUTCFullYear setUTCHours setUTCMilliseconds setUTCMinutes setUTCMonth setUTCSeconds setYear toDateString toGMTString toLocaleDateString toLocaleTimeString toLocaleString toString toTimeString toUTCString UTC valueOf".split(' ');
builtinMethods[Math] = "E LN2 LN10 LOG2E LOG10E PI SQRT1_2 SQRT2 abs acos asin atan atan2 ceil cos exp floor log max min pow random round sin sqrt tan".split(' ');
builtinMethods[Number] = "constructor prototype MAX_VALUE MIN_VALUE NEGATIVE_INFINITY POSITIVE_INFINITY toExponential toFixed toPrecision toString valueOf".split(' ');
builtinMethods[RegExp] = "constructor prototype global ignoreCase lastIndex multiline source compile exec test toString valueOf".split(' ');
builtinMethods[String] = "constructor prototype length charAt charCodeAt concat fromCharCode indexOf lastIndexOf match replace search slice split substr substring toLowerCase toString toUpperCase valueOf".split(' ');
var universalMethods = "constructor prototype toString valueOf toLocaleString hasOwnProperty propertyIsEnumerable".split(' ');
var extraGlobals = "Infinity NaN undefined null true false decodeURI decodeURIComponent encodeURI encodeURIComponent escape eval isFinite isNaN parseFloat parseInt unescape Array Boolean Date Math Number RegExp String".split(' ');
var builtinMethods = {}; // uses constructor objects as keys
builtinMethods[Array] = "length concat join pop push reverse shift slice sort splice unshift indexOf lastIndexOf every filter forEach map some".split(' ');
builtinMethods[Boolean] = "".split(' '); // nothing more than universal methods
builtinMethods[Date] = "getDate getDay getFullYear getHours getMilliseconds getMinutes getMonth getSeconds getTime getTimezoneOffset getUTCDate getUTCDay getUTCFullYear getUTCHours getUTCMilliseconds getUTCMinutes getUTCMonth getUTCSeconds getYear parse setDate setFullYear setHours setMilliseconds setMinutes setMonth setSeconds setTime setUTCDate setUTCFullYear setUTCHours setUTCMilliseconds setUTCMinutes setUTCMonth setUTCSeconds setYear toDateString toGMTString toLocaleDateString toLocaleTimeString toTimeString toUTCString UTC".split(' ');
builtinMethods[Math] = "E LN2 LN10 LOG2E LOG10E PI SQRT1_2 SQRT2 abs acos asin atan atan2 ceil cos exp floor log max min pow random round sin sqrt tan".split(' ');
builtinMethods[Number] = "MAX_VALUE MIN_VALUE NEGATIVE_INFINITY POSITIVE_INFINITY toExponential toFixed toPrecision".split(' ');
builtinMethods[RegExp] = "global ignoreCase lastIndex multiline source compile exec test".split(' ');
builtinMethods[String] = "length charAt charCodeAt concat fromCharCode indexOf lastIndexOf match replace search slice split substr substring toLowerCase toUpperCase".split(' ');
builtinMethods[Function] = "call apply".split(' ');
builtinMethods[Object] = "bsonsize".split(' ');
builtinMethods[Mongo] = "find update insert remove".split(' ');
builtinMethods[BinData] = "hex base64 length subtype".split(' ');
builtinMethods[NumberLong] = "toNumber".split(' ');
var extraGlobals = "Infinity NaN undefined null true false decodeURI decodeURIComponent encodeURI encodeURIComponent escape eval isFinite isNaN parseFloat parseInt unescape Array Boolean Date Math Number RegExp String print load gc MinKey MaxKey Mongo NumberLong ObjectId DBPointer UUID BinData Map".split(' ');
var isPrivate = function(name){
if (shellAutocomplete.showPrivate) return false;
@ -874,7 +881,7 @@ shellAutocomplete = function (/*prefix*/){ // outer scope function called on ini
var customComplete = function(obj){
try {
if(obj.constructor.autocomplete){
if(obj.__proto__.constructor.autocomplete){
var ret = obj.constructor.autocomplete(obj);
if (ret.constructor != Array){
print("\nautocompleters must return real Arrays");
@ -897,7 +904,7 @@ shellAutocomplete = function (/*prefix*/){ // outer scope function called on ini
var parts = prefix.split('.');
for (var p=0; p < parts.length - 1; p++){ // doesn't include last part
curObj = curObj[parts[p]];
if (curObj === undefined)
if (curObj == null)
return [];
}
@ -907,10 +914,11 @@ shellAutocomplete = function (/*prefix*/){ // outer scope function called on ini
begining += '.';
var possibilities = new Array().concat(
universalMethods,
Object.keySet(curObj),
Object.keySet(curObj.constructor.prototype),
Object.keySet(curObj.__proto__),
builtinMethods[curObj] || [], // curObj is a builtin constructor
builtinMethods[curObj.constructor] || [], // curObj is made from a builtin constructor
builtinMethods[curObj.__proto__.constructor] || [], // curObj is made from a builtin constructor
curObj == global ? extraGlobals : [],
customComplete(curObj)
);
@ -918,7 +926,7 @@ shellAutocomplete = function (/*prefix*/){ // outer scope function called on ini
var ret = [];
for (var i=0; i < possibilities.length; i++){
var p = possibilities[i];
if (curObj[p] === undefined && curObj != global) continue; // extraGlobals aren't in the global object
if (typeof(curObj[p]) == "undefined" && curObj != global) continue; // extraGlobals aren't in the global object
if (p.length == 0 || p.length < lastPrefix.length) continue;
if (isPrivate(p)) continue;
if (p.match(/^[0-9]+$/)) continue; // don't array number indexes