mirror of
https://github.com/RustyRaptor/brackets.git
synced 2026-12-04 00:15:22 -07:00
Add HintUtil2.formatParameterHint and fix bugs
This commit is contained in:
1 parent
6ffa193689
commit
088c6c9c42
8 files changed
+283
-362
No files matched your search
@@ -27,7 +27,7 @@
|
||||
define(function (require, exports, module) {
|
||||
"use strict";
|
||||
|
||||
var acorn = require("thirdparty/acorn/acorn");
|
||||
var Acorn = require("thirdparty/acorn/acorn");
|
||||
|
||||
var LANGUAGE_ID = "javascript",
|
||||
HTML_LANGUAGE_ID = "html",
|
||||
@@ -64,7 +64,7 @@ define(function (require, exports, module) {
|
||||
i;
|
||||
|
||||
for (i = 0; i < key.length; i++) {
|
||||
result = acorn.isIdentifierChar(key.charCodeAt(i));
|
||||
result = Acorn.isIdentifierChar(key.charCodeAt(i));
|
||||
if (!result) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
|
||||
/*global define */
|
||||
|
||||
/**
|
||||
* HintUtils2 was created as a place to put utilities that do not require third party dependencies so
|
||||
* they can be used by tern-worker.js and other JS files.
|
||||
* This is done because of the require config in tern-worker.js needed to load tern libraries. Libraries
|
||||
* that include, say "acorn", will fail to load.
|
||||
*/
|
||||
define(function (require, exports, module) {
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Format the given parameter array. Handles separators between
|
||||
* parameters, syntax for optional parameters, and the order of the
|
||||
* parameter type and parameter name.
|
||||
*
|
||||
* @param {!Array.<name: {string}, type: {string},
|
||||
* isOptional: {boolean}>} params - array of parameter descriptors
|
||||
* @param {function(string)} appendSeparators - callback function to append separators.
|
||||
* The separator is passed to the callback.
|
||||
* @param {function(string, number)} appendParameter - callback function to append parameter.
|
||||
* The formatted parameter type and name is passed to the callback along with the
|
||||
* current index of the parameter.
|
||||
* @param {boolean=} typesOnly - only show parameter types. The
|
||||
* default behavior is to include both parameter names and types.
|
||||
*/
|
||||
function formatParameterHint(params, appendSeparators, appendParameter, typesOnly) {
|
||||
var result = "",
|
||||
pendingOptional = false;
|
||||
|
||||
params.forEach(function (value, i) {
|
||||
var param = value.type,
|
||||
separators = "";
|
||||
|
||||
if (value.isOptional) {
|
||||
// if an optional param is following by an optional parameter, then
|
||||
// terminate the bracket. Otherwise enclose a required parameter
|
||||
// in the same bracket.
|
||||
if (pendingOptional) {
|
||||
separators += "]";
|
||||
pendingOptional = false;
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
result += " ";
|
||||
}
|
||||
separators += "[";
|
||||
pendingOptional = true;
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
separators += ", ";
|
||||
}
|
||||
|
||||
appendSeparators(separators);
|
||||
|
||||
if (!typesOnly) {
|
||||
param += " " + value.name;
|
||||
}
|
||||
|
||||
appendParameter(param, i);
|
||||
|
||||
});
|
||||
|
||||
if (pendingOptional) {
|
||||
appendSeparators("]");
|
||||
}
|
||||
}
|
||||
|
||||
exports.formatParameterHint = formatParameterHint;
|
||||
});
|
||||
@@ -1,241 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
|
||||
/*global define, brackets */
|
||||
|
||||
define(function (require, exports, module) {
|
||||
"use strict";
|
||||
|
||||
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
|
||||
|
||||
var Infer,
|
||||
cx,
|
||||
thirdPartyPath = ExtensionUtils.getModulePath(module, "thirdparty");
|
||||
|
||||
brackets.libRequire.config({baseUrl: thirdPartyPath})(["tern/lib/infer"], function (infer) {
|
||||
Infer = infer;
|
||||
|
||||
cx = new infer.Context([], null);
|
||||
|
||||
/**
|
||||
* Given a Tern formatted function type string, convert it to an array of Objects, where each object describes
|
||||
* a parameter.
|
||||
*
|
||||
* @param {String} newFnType - Tern formatted function type string.
|
||||
* @returns {Array<{name: string, type: string, isOptional: boolean}>} where each entry in the array is a parameter.
|
||||
*/
|
||||
function getParameters(newFnType) {
|
||||
|
||||
// work around define functions before use warning.
|
||||
var inferTypeToString, processInferFnTypeParameters;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {!Array.<name: {string}, type: {string},
|
||||
* isOptional: {boolean}>} params - array of parameter descriptors
|
||||
* @param {boolean=} typesOnly - only show parameter types. The
|
||||
* default behavior is to include both parameter names and types.
|
||||
* @returns {string} parameters formatted as a string
|
||||
*/
|
||||
function formatParams(params, typesOnly) {
|
||||
var result = "";
|
||||
|
||||
params.forEach(function (value, i) {
|
||||
|
||||
if (value.isOptional) {
|
||||
if (i > 0) {
|
||||
result += " ";
|
||||
}
|
||||
result += "[";
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
result += ", ";
|
||||
}
|
||||
|
||||
var type = value.type;
|
||||
|
||||
result += type;
|
||||
|
||||
if (!typesOnly) {
|
||||
result += " " + value.name;
|
||||
}
|
||||
|
||||
if (value.isOptional) {
|
||||
result += "]";
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an infer array type to a string.
|
||||
*
|
||||
* Formatted using google closure style. For example:
|
||||
*
|
||||
* "Array.<string, number>"
|
||||
*
|
||||
* @param {Infer.Arr} inferArrType
|
||||
*
|
||||
* @return {String} - array formatted in google closure style.
|
||||
*
|
||||
*/
|
||||
function inferArrTypeToString(inferArrType) {
|
||||
var result = "Array.<";
|
||||
|
||||
inferArrType.props["<i>"].types.forEach(function (value, i) {
|
||||
if (i > 0) {
|
||||
result += ", ";
|
||||
}
|
||||
result += inferTypeToString(value);
|
||||
});
|
||||
|
||||
// workaround case where types is zero length
|
||||
if (inferArrType.props["<i>"].types.length === 0) {
|
||||
result += "Object";
|
||||
}
|
||||
result += ">";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an infer type to a string.
|
||||
*
|
||||
* @param {*} inferType - one of the Infer's types; infer.Prim, infer.Arr, infer.ANull. infer.Fn functions are
|
||||
* not handled here.
|
||||
*
|
||||
* @return {String}
|
||||
*
|
||||
*/
|
||||
inferTypeToString = function (inferType) {
|
||||
var result;
|
||||
|
||||
if (inferType instanceof infer.Prim) {
|
||||
result = inferType.toString();
|
||||
if (result === "string") {
|
||||
result = "String";
|
||||
} else if (result === "number") {
|
||||
result = "Number";
|
||||
} else if (result === "boolean") {
|
||||
result = "Boolean";
|
||||
}
|
||||
} else if (inferType instanceof infer.Arr) {
|
||||
result = inferArrTypeToString(inferType);
|
||||
} else {
|
||||
result = "Object";
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert an infer function type to a Google closure type string.
|
||||
*
|
||||
* @param {Infer.Fn} inferType - type to convert.
|
||||
* @return {string} - function type as a string.
|
||||
*/
|
||||
function inferFnTypeToString(inferType) {
|
||||
var result = "function(",
|
||||
params = processInferFnTypeParameters(inferType);
|
||||
|
||||
result += formatParams(params, true);
|
||||
result += "): ";
|
||||
result += inferTypeToString(inferType.retval);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an infer function type to string.
|
||||
*
|
||||
* @param {*} inferType - one of the Infer's types; infer.Fn, infer.Prim, infer.Arr, infer.ANull
|
||||
* @returns {Array<{name: string, type: string, isOptional: boolean}>} where each entry in the array is a parameter.
|
||||
*/
|
||||
processInferFnTypeParameters = function (inferType) {
|
||||
var params = [],
|
||||
i;
|
||||
|
||||
for (i = 0; i < inferType.args.length; i++) {
|
||||
var param = {},
|
||||
name = inferType.argNames[i],
|
||||
type = inferType.args[i];
|
||||
|
||||
if (name === undefined) {
|
||||
name = 'arg' + (i + 1);
|
||||
}
|
||||
|
||||
if (name[name.length - 1] === "?") {
|
||||
name = name.substring(0, name.length - 1);
|
||||
param.isOptional = true;
|
||||
}
|
||||
|
||||
param.name = name;
|
||||
|
||||
if (type instanceof Infer.Fn) {
|
||||
param.type = inferFnTypeToString(type);
|
||||
} else {
|
||||
param.type = inferTypeToString(type);
|
||||
}
|
||||
|
||||
params.push(param);
|
||||
}
|
||||
|
||||
return params;
|
||||
};
|
||||
|
||||
if (newFnType && newFnType.indexOf("fn(") === 0) {
|
||||
var params = Infer.withContext(cx, function () {
|
||||
|
||||
try {
|
||||
var typeParser = new Infer.def.TypeParser(newFnType, null, null, true),
|
||||
inferType = typeParser.parseType("", true);
|
||||
return processInferFnTypeParameters(inferType);
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
return [{name: e.message, type: ""}];
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
console.log("newFnType = " + newFnType);
|
||||
console.log("convert to params:");
|
||||
|
||||
params.forEach(function (value) {
|
||||
console.log(value);
|
||||
});
|
||||
|
||||
return params;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
exports.getParameters = getParameters;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ define(function (require, exports, module) {
|
||||
Menus = brackets.getModule("command/Menus"),
|
||||
Strings = brackets.getModule("strings"),
|
||||
StringUtils = brackets.getModule("utils/StringUtils"),
|
||||
HintsUtils2 = require("HintUtils2"),
|
||||
ScopeManager = require("ScopeManager"),
|
||||
Session = require("Session");
|
||||
|
||||
@@ -49,7 +50,7 @@ define(function (require, exports, module) {
|
||||
$hintContent, // function hint content holder
|
||||
|
||||
/** @type {{inFunctionCall: boolean, functionCallPos: {line: number, ch: number},
|
||||
* fnType: {string}}
|
||||
* fnType: {Array.<Object>}}
|
||||
*/
|
||||
hintState = {},
|
||||
hintStack = [], // stack for previous function hint to restore
|
||||
@@ -57,12 +58,8 @@ define(function (require, exports, module) {
|
||||
session; // current editor session, updated by main
|
||||
|
||||
// Constants
|
||||
var
|
||||
POSITION_OFFSET = 38, // Distance between the bottom of the line and the bottom of the preview container
|
||||
POINTER_LEFT_OFFSET = 17, // Half of the pointer width, used to find the center of the pointer
|
||||
POINTER_TOP_OFFSET = 4, // Size of margin + border of hint.
|
||||
POSITION_BELOW_OFFSET = 16, // Amount to adjust to top position when the preview bubble is below the text
|
||||
POPOVER_HORZ_MARGIN = 5; // Horizontal margin
|
||||
var POINTER_TOP_OFFSET = 4, // Size of margin + border of hint.
|
||||
POSITION_BELOW_OFFSET = 4; // Amount to adjust to top position when the preview bubble is below the text
|
||||
|
||||
// keep jslint from complaining about handleCursorActivity being used before
|
||||
// it was defined.
|
||||
@@ -138,44 +135,29 @@ define(function (require, exports, module) {
|
||||
* of the function call.
|
||||
*/
|
||||
function formatHint(functionInfo) {
|
||||
var hints = session.getParameterHint(functionInfo.functionCallPos);
|
||||
var hints = session.getParameterHint(functionInfo.functionCallPos),
|
||||
pendingOptional = false;
|
||||
|
||||
$hintContent.empty();
|
||||
$hintContent.addClass("brackets-js-hints");
|
||||
hints.parameters.forEach(function (value, i) {
|
||||
var param = value.type + " " + value.name,
|
||||
separator = "";
|
||||
|
||||
if (value.isOptional) {
|
||||
if (i > 0) {
|
||||
separator += " ";
|
||||
}
|
||||
function appendSeparators(separators) {
|
||||
$hintContent.append(separators);
|
||||
}
|
||||
|
||||
separator += "[";
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
separator += ", ";
|
||||
}
|
||||
|
||||
if (separator) {
|
||||
$hintContent.append(separator);
|
||||
}
|
||||
|
||||
if (hints.currentIndex === i) {
|
||||
function appendParameter(param, index) {
|
||||
if (hints.currentIndex === index) {
|
||||
$hintContent.append($("<span>")
|
||||
.append(StringUtils.htmlEscape(param))
|
||||
.addClass("current-parameter"));
|
||||
} else {
|
||||
$hintContent.append(StringUtils.htmlEscape(param));
|
||||
}
|
||||
}
|
||||
|
||||
if (value.isOptional) {
|
||||
$hintContent.append("]");
|
||||
}
|
||||
});
|
||||
|
||||
if (hints.parameters.length === 0) {
|
||||
if (hints.parameters.length > 0) {
|
||||
HintsUtils2.formatParameterHint(hints.parameters, appendSeparators, appendParameter);
|
||||
} else {
|
||||
$hintContent.append(StringUtils.htmlEscape(Strings.NO_ARGUMENTS));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,6 @@ define(function (require, exports, module) {
|
||||
TokenUtils = brackets.getModule("utils/TokenUtils"),
|
||||
HintUtils = require("HintUtils"),
|
||||
ScopeManager = require("ScopeManager"),
|
||||
InferUtils = require("InferUtils"),
|
||||
Acorn = require("thirdparty/acorn/acorn"),
|
||||
Acorn_Loose = require("thirdparty/acorn/acorn_loose");
|
||||
|
||||
@@ -627,10 +626,11 @@ define(function (require, exports, module) {
|
||||
/**
|
||||
* Set a new function type hint.
|
||||
*
|
||||
* @param {string} newFnType - the new tern function hint.
|
||||
* @param {Array<{name: string, type: string, isOptional: boolean}>} newFnType -
|
||||
* Array of function hints.
|
||||
*/
|
||||
Session.prototype.setFnType = function (newFnType) {
|
||||
this.fnType = newFnType; //InferUtils.getParameters(newFnType);
|
||||
this.fnType = newFnType;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -640,7 +640,7 @@ define(function (require, exports, module) {
|
||||
*/
|
||||
Session.prototype.setFunctionCallPos = function (functionCallPos) {
|
||||
this.functionCallPos = functionCallPos;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the function type hint. This will format the hint, showing the
|
||||
|
||||
@@ -29,13 +29,13 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var MessageIds;
|
||||
var MessageIds, HintUtils2;
|
||||
var Tern, Infer;
|
||||
require(["./MessageIds"], function (messageIds) {
|
||||
require(["./MessageIds", "./HintUtils2"], function (messageIds, hintUtils2) {
|
||||
MessageIds = messageIds;
|
||||
HintUtils2 = hintUtils2;
|
||||
var ternRequire = require.config({baseUrl: "./thirdparty"});
|
||||
ternRequire(["tern/lib/tern", "tern/lib/infer", "tern/plugin/requirejs", "tern/plugin/doc_comment"],
|
||||
function (tern, infer, requirejs, docComment) {
|
||||
ternRequire(["tern/lib/tern", "tern/lib/infer", "tern/plugin/requirejs", "tern/plugin/doc_comment"], function (tern, infer, requirejs, docComment) {
|
||||
Tern = tern;
|
||||
Infer = infer;
|
||||
|
||||
@@ -305,47 +305,7 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
function getParameters(newFnType) {
|
||||
|
||||
// work around define functions before use warning.
|
||||
var inferTypeToString, processInferFnTypeParameters;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {!Array.<name: {string}, type: {string},
|
||||
* isOptional: {boolean}>} params - array of parameter descriptors
|
||||
* @param {boolean=} typesOnly - only show parameter types. The
|
||||
* default behavior is to include both parameter names and types.
|
||||
* @returns {string} parameters formatted as a string
|
||||
*/
|
||||
function formatParams(params, typesOnly) {
|
||||
var result = "";
|
||||
|
||||
params.forEach(function (value, i) {
|
||||
|
||||
if (value.isOptional) {
|
||||
if (i > 0) {
|
||||
result += " ";
|
||||
}
|
||||
result += "[";
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
result += ", ";
|
||||
}
|
||||
|
||||
var type = value.type;
|
||||
|
||||
result += type;
|
||||
|
||||
if (!typesOnly) {
|
||||
result += " " + value.name;
|
||||
}
|
||||
|
||||
if (value.isOptional) {
|
||||
result += "]";
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
var inferTypeToString, processInferFnTypeParameters, inferFnTypeToString;
|
||||
|
||||
/**
|
||||
* Convert an infer array type to a string.
|
||||
@@ -418,15 +378,22 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
* @param {Infer.Fn} inferType - type to convert.
|
||||
* @return {string} - function type as a string.
|
||||
*/
|
||||
function inferFnTypeToString(inferType) {
|
||||
inferFnTypeToString = function (inferType) {
|
||||
var result = "function(",
|
||||
params = processInferFnTypeParameters(inferType);
|
||||
|
||||
result += formatParams(params, true);
|
||||
result += "):";
|
||||
result += inferTypeToString(inferType.retval);
|
||||
function appendValue(value) {
|
||||
result += value;
|
||||
}
|
||||
|
||||
HintUtils2.formatParameterHint(params, appendValue, appendValue, true);
|
||||
if (inferType.retval) {
|
||||
result += "):";
|
||||
result += inferTypeToString(inferType.retval);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert an infer function type to string.
|
||||
@@ -444,7 +411,7 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
type = inferType.args[i];
|
||||
|
||||
if (name === undefined) {
|
||||
name = 'arg' + (i + 1);
|
||||
name = 'param' + (i + 1);
|
||||
}
|
||||
|
||||
if (name[name.length - 1] === "?") {
|
||||
@@ -469,18 +436,11 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
return processInferFnTypeParameters(inferType);
|
||||
} catch (e) {
|
||||
_log(e.message);
|
||||
return [{name: e.message, type: ""}];
|
||||
return [{name: e.message, type: ''}];
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
_log("newFnType = " + newFnType);
|
||||
_log("convert to params:");
|
||||
|
||||
params.forEach(function (value) {
|
||||
_log(value);
|
||||
});
|
||||
|
||||
return params;
|
||||
} else {
|
||||
return [];
|
||||
|
||||
+27
-5
@@ -164,19 +164,16 @@ require(["MyModule"], function (myModule) {
|
||||
/**
|
||||
* Test record type google annoations.
|
||||
*
|
||||
* @param {{line: number, type: string}} t
|
||||
* @param {{index: number, type: string}} t
|
||||
*/
|
||||
function testRecordTypeAnnotation(t) {
|
||||
'use strict';
|
||||
|
||||
}
|
||||
|
||||
// Function hint testing
|
||||
// More parameter hint testing
|
||||
function functionHintTesting() {
|
||||
"use strict";
|
||||
|
||||
// simple function with primative arguments
|
||||
s.lastIndexOf(s);
|
||||
|
||||
// function with record type argument
|
||||
testRecordTypeAnnotation();
|
||||
@@ -187,10 +184,35 @@ function functionHintTesting() {
|
||||
// function with array argument
|
||||
arr.concat();
|
||||
|
||||
s.lastIndexOf(s, 5);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Array annotation
|
||||
*
|
||||
* @param {Array.<string>} a
|
||||
*/
|
||||
function testArrayAnnotation(a) {
|
||||
"use strict";
|
||||
|
||||
}
|
||||
|
||||
testArrayAnnotation();
|
||||
|
||||
/**
|
||||
* Test multiple optional args
|
||||
*
|
||||
* @param {string=} a
|
||||
* @param {string=} b
|
||||
*
|
||||
*/
|
||||
function testOptionalArgs(a, b) {
|
||||
"use strict";
|
||||
|
||||
}
|
||||
|
||||
testOptionalArgs();
|
||||
/* Add large comment to make this test over 250 lines which will trigger
|
||||
* partial updates to be used.
|
||||
*
|
||||
|
||||
@@ -347,6 +347,13 @@ define(function (require, exports, module) {
|
||||
expect(ParameterHintManager.popUpHint()).toBe(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the parameter hint is not visible.
|
||||
*/
|
||||
function expectParameterHintClosed() {
|
||||
expect(ParameterHintManager.isHintDisplayed()).toBe(false);
|
||||
}
|
||||
|
||||
/*
|
||||
* Wait for a hint response object to resolve, then apply a callback
|
||||
* to the result
|
||||
@@ -376,11 +383,12 @@ define(function (require, exports, module) {
|
||||
* Show a function hint based on the code at the cursor. Verify the
|
||||
* hint matches the passed in value.
|
||||
*
|
||||
* @param {?Array.<{name: string, type: string}> expectedParams - array of
|
||||
* records, where each element of the array describes a function parameter.
|
||||
* If null, then no hint is expected.
|
||||
* @param {Array<{name: string, type: string, isOptional: boolean}>}
|
||||
* expectedParams - array of records, where each element of the array
|
||||
* describes a function parameter. If null, then no hint is expected.
|
||||
* @param {number} expectedParameter - the parameter at cursor.
|
||||
*/
|
||||
function expectParameterHint(expectedParams)
|
||||
function expectParameterHint(expectedParams, expectedParameter)
|
||||
{
|
||||
var request = ParameterHintManager.popUpHint();
|
||||
if (expectedParams === null) {
|
||||
@@ -388,21 +396,33 @@ define(function (require, exports, module) {
|
||||
return;
|
||||
}
|
||||
|
||||
_waitForParameterHint(request, function (hint) {
|
||||
function expectHint(hint) {
|
||||
var params = hint.parameters,
|
||||
n = params.length,
|
||||
i;
|
||||
|
||||
// compare params to expected params
|
||||
expect(params.length).toBe(expectedParams.length);
|
||||
expect(hint.currentIndex).toBe(expectedParameter);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
|
||||
expect(params[i].name).toBe(expectedParams[i].name);
|
||||
expect(params[i].type).toBe(expectedParams[i].type);
|
||||
if (params[i].isOptional) {
|
||||
expect(expectedParams[i].isOptional).toBeTruthy();
|
||||
} else {
|
||||
expect(expectedParams[i].isOptional).toBeFalsy();
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
if (request) {
|
||||
_waitForParameterHint(request, expectHint);
|
||||
} else {
|
||||
expectHint(JSCodeHints.getSession().getParameterHint());
|
||||
}
|
||||
}
|
||||
|
||||
function setupTest(path, primePump) {
|
||||
@@ -805,7 +825,8 @@ define(function (require, exports, module) {
|
||||
testDoc.replaceRange("funD(", start, start);
|
||||
testEditor.setCursorPos(middle);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "a", type: "String"}, {name: "b", type: "Number"}]);
|
||||
expectParameterHint([{name: "a", type: "String"},
|
||||
{name: "b", type: "Number"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -888,7 +909,7 @@ define(function (require, exports, module) {
|
||||
var start = { line: 59, ch: 10 };
|
||||
testEditor.setCursorPos(start);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "a4", type: "Number"}, {name: "b4", type: "Number"}]);
|
||||
expectParameterHint([{name: "a4", type: "Number"}, {name: "b4", type: "Number"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -908,7 +929,7 @@ define(function (require, exports, module) {
|
||||
testDoc.replaceRange("myCustomer.setAmountDue(", start);
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name:"amountDue", type: "Object"}]);
|
||||
expectParameterHint([{name:"amountDue", type: "Object"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -917,7 +938,7 @@ define(function (require, exports, module) {
|
||||
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "arg", type: "String"}]);
|
||||
expectParameterHint([{name: "arg", type: "String"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -927,7 +948,7 @@ define(function (require, exports, module) {
|
||||
testEditor.setCursorPos(testPos);
|
||||
var hintObj = expectHints(JSCodeHints.jsHintProvider);
|
||||
runs(function () {
|
||||
expectParameterHint([]);
|
||||
expectParameterHint([], 0);
|
||||
});
|
||||
|
||||
});
|
||||
@@ -951,7 +972,7 @@ define(function (require, exports, module) {
|
||||
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "f", type: "function(): number"}]);
|
||||
expectParameterHint([{name: "f", type: "function(): number"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -961,7 +982,7 @@ define(function (require, exports, module) {
|
||||
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "f", type: "function(String, Number):String"}]);
|
||||
expectParameterHint([{name: "f", type: "function(String, Number):String"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -982,7 +1003,7 @@ define(function (require, exports, module) {
|
||||
testDoc.replaceRange("funArr.index1(", start);
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([]);
|
||||
expectParameterHint([], 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1174,7 +1195,89 @@ define(function (require, exports, module) {
|
||||
testDoc.replaceRange("myCustomer.setAmountDue(10)", start);
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "amountDue", type: "Number"}]);
|
||||
expectParameterHint([{name: "amountDue", type: "Number"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
// log a bug against Tern. Tern can't parse its own function hint.
|
||||
xit("should list parameter hint for record type annotation", function () {
|
||||
var testPos = { line: 178, ch: 25 };
|
||||
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "t", type: "{index: Number, type: String}"}], -1);
|
||||
});
|
||||
});
|
||||
|
||||
it("should list parameter hint for a function parameter", function () {
|
||||
var testPos = { line: 181, ch: 12 };
|
||||
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "compare",
|
||||
type: "function(Object, Object):Number",
|
||||
isOptional: true}], -1);
|
||||
});
|
||||
});
|
||||
|
||||
it("should list parameter hint for an array parameter", function () {
|
||||
var testPos = { line: 184, ch: 12 };
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "other", type: "Array.<Object>"}], -1);
|
||||
});
|
||||
});
|
||||
|
||||
// Tern is not returning the correct function type info for the array annotation
|
||||
xit("should list parameter hint for a source array annotation", function () {
|
||||
var testPos = { line: 200, ch: 20 };
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "a", type: "Array.<String>"}],0);
|
||||
});
|
||||
});
|
||||
|
||||
it("should close parameter hint when move off function", function () {
|
||||
var testPos = { line: 184, ch: 12 },
|
||||
endPos = { line: 184, ch: 19 };
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "other", type: "Array.<Object>"}], -1);
|
||||
});
|
||||
|
||||
runs(function () {
|
||||
testEditor.setCursorPos(endPos);
|
||||
expectParameterHintClosed();
|
||||
});
|
||||
});
|
||||
|
||||
it("should close parameter hint when move off function to another function", function () {
|
||||
var testPos = { line: 184, ch: 12 },
|
||||
newPos = { line: 181, ch: 12 };
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "other", type: "Array.<Object>"}], -1);
|
||||
});
|
||||
|
||||
runs(function () {
|
||||
testEditor.setCursorPos(newPos);
|
||||
expectParameterHintClosed();
|
||||
});
|
||||
});
|
||||
|
||||
it("should update current parameter as the cursor moves", function () {
|
||||
var testPos = { line: 186, ch: 19 },
|
||||
newPos = { line: 186, ch: 20 };
|
||||
testEditor.setCursorPos(testPos);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "char", type: "String"},
|
||||
{name: "from", type: "Number", isOptional: true}], 0);
|
||||
});
|
||||
|
||||
runs(function () {
|
||||
testEditor.setCursorPos(newPos);
|
||||
expectParameterHint([{name: "char", type: "String"},
|
||||
{name: "from", type: "Number", isOptional: true}], 1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1207,7 +1310,7 @@ define(function (require, exports, module) {
|
||||
|
||||
testEditor.setCursorPos(start);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "a", type: "Number"}]);
|
||||
expectParameterHint([{name: "a", type: "Number"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1216,7 +1319,7 @@ define(function (require, exports, module) {
|
||||
|
||||
testEditor.setCursorPos(start);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "a", type: "String"}, {name: "b", type: "Number"}]);
|
||||
expectParameterHint([{name: "a", type: "String"}, {name: "b", type: "Number"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1225,7 +1328,7 @@ define(function (require, exports, module) {
|
||||
|
||||
testEditor.setCursorPos(start);
|
||||
runs(function () {
|
||||
expectParameterHint([{name: "paramE1", type: "D1"}, {name: "paramE2", type: "Number"}]);
|
||||
expectParameterHint([{name: "paramE1", type: "D1"}, {name: "paramE2", type: "Number"}], 0);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user