Bug 1216668 - Implement console autocomplete for inline arrays and multiline strings;r=fitzgen

--HG--
extra : commitid : AM3h7WgZXX5
This commit is contained in:
Brian Grinstead 2015-10-21 07:10:38 -07:00
Родитель 8352c7d8cb
Коммит af2c9af581
3 изменённых файлов: 80 добавлений и 7 удалений

Просмотреть файл

@ -153,6 +153,14 @@ SyntaxTreesPool.prototype = {
return this._call("getNamedFunctionDefinitions", -1, aSubstring);
},
/**
* @return SyntaxTree
* The last tree in this._trees
*/
getLastSyntaxTree: function() {
return this._trees[this._trees.length - 1];
},
/**
* Gets the total number of scripts in the parent source.
* @return number

Просмотреть файл

@ -41,6 +41,46 @@ function run_test() {
results = JSPropertyProvider(dbgObject, null, "testArray[2][0].");
test_has_result(results, "propE");
do_print("Test that suggestions are given for literal arrays.");
results = JSPropertyProvider(dbgObject, null, "[1,2,3].");
test_has_result(results, "indexOf");
do_print("Test that suggestions are given for literal arrays with newlines.");
results = JSPropertyProvider(dbgObject, null, "[1,2,3,\n4\n].");
test_has_result(results, "indexOf");
do_print("Test that suggestions are given for literal strings.");
results = JSPropertyProvider(dbgObject, null, "'foo'.");
test_has_result(results, "charAt");
results = JSPropertyProvider(dbgObject, null, '"foo".');
test_has_result(results, "charAt");
results = JSPropertyProvider(dbgObject, null, "`foo`.");
test_has_result(results, "charAt");
results = JSPropertyProvider(dbgObject, null, "'[1,2,3]'.");
test_has_result(results, "charAt");
do_print("Test that suggestions are not given for syntax errors.");
results = JSPropertyProvider(dbgObject, null, "'foo\"");
do_check_null(results);
results = JSPropertyProvider(dbgObject, null, "[1,',2]");
do_check_null(results);
results = JSPropertyProvider(dbgObject, null, "'[1,2].");
do_check_null(results);
results = JSPropertyProvider(dbgObject, null, "'foo'..");
do_check_null(results);
do_print("Test that suggestions are not given without a dot.");
results = JSPropertyProvider(dbgObject, null, "'foo'");
test_has_no_results(results);
results = JSPropertyProvider(dbgObject, null, "[1,2,3]");
test_has_no_results(results);
results = JSPropertyProvider(dbgObject, null, "[1,2,3].\n'foo'");
test_has_no_results(results);
do_print("Test that suggestions are not given for numeric literals.");
results = JSPropertyProvider(dbgObject, null, "1.");
do_check_null(results);
do_print("Test that suggestions are not given for index that's out of bounds.");
results = JSPropertyProvider(dbgObject, null, "testArray[10].");
do_check_null(results);
@ -63,6 +103,15 @@ function run_test() {
do_check_null(results);
}
/**
* A helper that ensures an empty array of results were found.
* @param Object aResults
* The results returned by JSPropertyProvider.
*/
function test_has_no_results(aResults) {
do_check_neq(aResults, null);
do_check_eq(aResults.matches.length, 0);
}
/**
* A helper that ensures (required) results were found.
* @param Object aResults

Просмотреть файл

@ -12,6 +12,7 @@ const {isWindowIncluded} = require("devtools/shared/layout/utils");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
loader.lazyImporter(this, "Services", "resource://gre/modules/Services.jsm");
loader.lazyImporter(this, "Parser", "resource://gre/modules/devtools/shared/Parser.jsm");
// TODO: Bug 842672 - browser/ imports modules from toolkit/.
// Note that these are only used in WebConsoleCommands, see $0 and pprint().
@ -861,19 +862,34 @@ function JSPropertyProvider(aDbgObject, anEnvironment, aInputValue, aCursor)
}
let completionPart = inputValue.substring(beginning.startPos);
let lastDot = completionPart.lastIndexOf(".");
// Don't complete on just an empty string.
if (completionPart.trim() == "") {
return null;
}
let lastDot = completionPart.lastIndexOf(".");
if (lastDot > 0 &&
(completionPart[0] == "'" || completionPart[0] == '"') &&
completionPart[lastDot - 1] == completionPart[0]) {
// We are completing a string literal.
let matchProp = completionPart.slice(lastDot + 1);
return getMatchedProps(String.prototype, matchProp);
// Catch literals like [1,2,3] or "foo" and return the matches from
// their prototypes.
if (lastDot > 0) {
let parser = new Parser();
parser.logExceptions = false;
let syntaxTree = parser.get(completionPart.slice(0, lastDot));
let lastTree = syntaxTree.getLastSyntaxTree();
let lastBody = lastTree && lastTree.AST.body[lastTree.AST.body.length - 1];
// Finding the last expression since we've sliced up until the dot.
// If there were parse errors this won't exist.
if (lastBody) {
let expression = lastBody.expression;
let matchProp = completionPart.slice(lastDot + 1);
if (expression.type === "ArrayExpression") {
return getMatchedProps(Array.prototype, matchProp);
} else if (expression.type === "Literal" &&
(typeof expression.value === "string")) {
return getMatchedProps(String.prototype, matchProp);
}
}
}
// We are completing a variable / a property lookup.