Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 3f58338

Browse files
committed
Type Inference in hint list.
1 parent 94f4f6e commit 3f58338

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/extensions/default/JavaScriptCodeHints/main.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,64 @@ define(function (require, exports, module) {
149149
console.debug("Hints", _.pluck(hints, "label"));
150150
}
151151

152+
var _infered = true;
153+
154+
function getInferHelper(type) {
155+
return function (element, index, array) {
156+
if (element === type && _infered) {
157+
_infered = true;
158+
} else {
159+
_infered = false;
160+
}
161+
};
162+
}
163+
164+
function inferArrayType(typeExpr) {
165+
var type = "[...]";
166+
var types = typeExpr.split('[')[1].split(']')[0].split(',');
167+
168+
_infered = true;
169+
170+
types.every(getInferHelper('string'));
171+
if (_infered) {
172+
type = '[Abc]';
173+
} else {
174+
_infered = true;
175+
types.every(getInferHelper('number'));
176+
if (_infered) {
177+
type = '[123]';
178+
} else {
179+
_infered = true;
180+
types.every(getInferHelper('Object'));
181+
if (_infered) {
182+
type = '[{ }]';
183+
}
184+
}
185+
}
186+
187+
return type;
188+
}
189+
190+
function getRenderType(type) {
191+
var typeString = " ";
192+
if (type) {
193+
if (type.indexOf('Object') === 0) {
194+
typeString = '{ }';
195+
} else if (type.indexOf('[') === 0) {
196+
typeString = inferArrayType(type);
197+
} else if (type.indexOf('fn') === 0) {
198+
typeString = 'fn( )';
199+
} else if (type.indexOf('string') === 0) {
200+
typeString = "Abc";
201+
} else if (type.indexOf('number') === 0) {
202+
typeString = '123';
203+
} else if (type.indexOf('bool') === 0) {
204+
typeString = 'bool';
205+
}
206+
}
207+
return typeString;
208+
}
209+
152210
/*
153211
* Returns a formatted list of hints with the query substring
154212
* highlighted.
@@ -165,7 +223,7 @@ define(function (require, exports, module) {
165223
function formatHints(hints, query) {
166224
return hints.map(function (token) {
167225
var $hintObj = $("<span>").addClass("brackets-js-hints");
168-
226+
$('<span>' + getRenderType(token.type) + '</span>').appendTo($hintObj).addClass("brackets-js-hints-type");
169227
// level indicates either variable scope or property confidence
170228
if (!type.property && !token.builtin && token.depth !== undefined) {
171229
switch (token.depth) {

src/extensions/default/JavaScriptCodeHints/styles/brackets-js-hints.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
*
2222
*/
2323

24+
.brackets-js-hints-type {
25+
width: 30px;
26+
color: cornflowerblue;
27+
font-weight: 900;
28+
padding-left: 5px;
29+
text-align: center;
30+
float: left;
31+
box-sizing: border-box;
32+
}
33+
2434
.brackets-js-hints.priority-high {
2535
color: #486c00; /* green */
2636
}

0 commit comments

Comments
 (0)