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

Commit 60eaca4

Browse files
committed
Merge pull request #11949 from adobe/swmitra/TypeInferenceInHintlist
[WIP]Type Inference in hint list.
2 parents 79f3e2c + 84e3cb4 commit 60eaca4

4 files changed

Lines changed: 301 additions & 4 deletions

File tree

src/extensions/default/JavaScriptCodeHints/Session.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,14 @@ define(function (require, exports, module) {
570570
if (hint.depth !== undefined) {
571571
searchResult.depth = hint.depth;
572572
}
573+
574+
if (hint.doc) {
575+
searchResult.doc = hint.doc;
576+
}
577+
578+
if (hint.url) {
579+
searchResult.url = hint.url;
580+
}
573581

574582
if (!type.property && !type.showFunctionType && hint.origin &&
575583
isBuiltin(hint.origin)) {

src/extensions/default/JavaScriptCodeHints/main.js

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,63 @@ 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 inferArrayTypeClass(typeExpr) {
165+
var type = "type-array";
166+
var types = typeExpr.split('[')[1].split(']')[0].split(',');
167+
168+
_infered = true;
169+
170+
types.every(getInferHelper('string'));
171+
if (_infered) {
172+
type = 'type-string-array';
173+
} else {
174+
_infered = true;
175+
types.every(getInferHelper('number'));
176+
if (_infered) {
177+
type = 'type-num-array';
178+
} else {
179+
_infered = true;
180+
types.every(getInferHelper('Object'));
181+
if (_infered) {
182+
type = 'type-object-array';
183+
}
184+
}
185+
}
186+
return type;
187+
}
188+
189+
function getRenderTypeClass(type) {
190+
var typeClass = 'type-undetermined';
191+
if (type) {
192+
if (type.indexOf('Object') === 0) {
193+
typeClass = 'type-object';
194+
} else if (type.indexOf('[') === 0) {
195+
typeClass = inferArrayTypeClass(type);
196+
} else if (type.indexOf('fn') === 0) {
197+
typeClass = 'type-function';
198+
} else if (type.indexOf('string') === 0) {
199+
typeClass = "type-string";
200+
} else if (type.indexOf('number') === 0) {
201+
typeClass = 'type-number';
202+
} else if (type.indexOf('bool') === 0) {
203+
typeClass = 'type-boolean';
204+
}
205+
}
206+
return typeClass;
207+
}
208+
152209
/*
153210
* Returns a formatted list of hints with the query substring
154211
* highlighted.
@@ -165,7 +222,8 @@ define(function (require, exports, module) {
165222
function formatHints(hints, query) {
166223
return hints.map(function (token) {
167224
var $hintObj = $("<span>").addClass("brackets-js-hints");
168-
225+
($hintObj).addClass(getRenderTypeClass(token.type));
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) {
@@ -211,9 +269,37 @@ define(function (require, exports, module) {
211269
} else {
212270
$hintObj.text(token.value);
213271
}
214-
272+
215273
$hintObj.data("token", token);
216274

275+
function _appendLink() {
276+
if (token.url) {
277+
$('<a></a>').appendTo($hintObj).addClass("jshint-link").attr('href', token.url).on("click", function (event) {
278+
event.stopImmediatePropagation();
279+
event.stopPropagation();
280+
});
281+
}
282+
}
283+
284+
if (token.type) {
285+
if (token.type.length > 40) {
286+
_appendLink();
287+
$('<span>' + token.type.split('->').join(':').toString().trim() + '</span>').appendTo($hintObj).addClass("jshint-description");
288+
} else {
289+
$('<span>' + token.type.split('->').join(':').toString().trim() + '</span>').appendTo($hintObj).addClass("brackets-js-hints-type-details");
290+
_appendLink();
291+
}
292+
} else {
293+
if (token.keyword) {
294+
$('<span>keyword</span>').appendTo($hintObj).addClass("brackets-js-hints-type-details").addClass("keyword");
295+
}
296+
}
297+
298+
if (token.doc) {
299+
$hintObj.attr('title', token.doc);
300+
$('<span></span>').text(token.doc.trim()).appendTo($hintObj).addClass("jshint-jsdoc");
301+
}
302+
217303
return $hintObj;
218304
});
219305
}
@@ -234,7 +320,7 @@ define(function (require, exports, module) {
234320
handleWideResults: hints.handleWideResults
235321
};
236322
}
237-
323+
238324
/**
239325
* @constructor
240326
*/

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

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

24+
25+
span.brackets-js-hints {
26+
width: 400px;
27+
display: inline-block;
28+
}
29+
30+
.brackets-js-hints.type-undetermined:before {
31+
color: cornflowerblue;
32+
content: '12';
33+
border: 1px cornflowerblue solid;
34+
float:left;
35+
width:25px;
36+
text-align: center;
37+
margin-left:-5px;
38+
margin-right:5px;
39+
visibility: hidden;
40+
line-height: 1.2em;
41+
}
42+
43+
.brackets-js-hints.type-number:before {
44+
color: cornflowerblue;
45+
content: '12';
46+
border: 1px cornflowerblue solid;
47+
float:left;
48+
width:25px;
49+
text-align: center;
50+
margin-left:-5px;
51+
margin-right:5px;
52+
line-height: 1.2em;
53+
}
54+
55+
.brackets-js-hints.type-string:before {
56+
color: cornflowerblue;
57+
content: 'ab';
58+
border: 1px cornflowerblue solid;
59+
float:left;
60+
width:25px;
61+
text-align: center;
62+
margin-left:-5px;
63+
margin-right:5px;
64+
line-height: 1.2em;
65+
}
66+
67+
.brackets-js-hints.type-function:before {
68+
color: cornflowerblue;
69+
content: 'fn( )';
70+
border: 1px cornflowerblue solid;
71+
float:left;
72+
width:25px;
73+
text-align: center;
74+
margin-left:-5px;
75+
margin-right:5px;
76+
line-height: 1.2em;
77+
}
78+
79+
.brackets-js-hints.type-array:before {
80+
color: cornflowerblue;
81+
content: '[ ]';
82+
border: 1px cornflowerblue solid;
83+
float:left;
84+
width:25px;
85+
text-align: center;
86+
margin-left:-5px;
87+
margin-right:5px;
88+
line-height: 1.2em;
89+
}
90+
91+
.brackets-js-hints.type-object:before {
92+
color: cornflowerblue;
93+
content: '{ :}';
94+
border: 1px cornflowerblue solid;
95+
float:left;
96+
width:25px;
97+
text-align: center;
98+
margin-left:-5px;
99+
margin-right:5px;
100+
line-height: 1.2em;
101+
}
102+
103+
.brackets-js-hints.type-num-array:before {
104+
color: cornflowerblue;
105+
content: '[12]';
106+
border: 1px cornflowerblue solid;
107+
float:left;
108+
width:25px;
109+
text-align: center;
110+
margin-left:-5px;
111+
margin-right:5px;
112+
line-height: 1.2em;
113+
}
114+
115+
.brackets-js-hints.type-string-array:before {
116+
color: cornflowerblue;
117+
content: '[ab]';
118+
border: 1px cornflowerblue solid;
119+
float:left;
120+
width:25px;
121+
text-align: center;
122+
margin-left:-5px;
123+
margin-right:5px;
124+
line-height: 1.2em;
125+
}
126+
127+
.brackets-js-hints.type-boolean:before {
128+
color: cornflowerblue;
129+
content: 'bool';
130+
border: 1px cornflowerblue solid;
131+
float:left;
132+
width:25px;
133+
text-align: center;
134+
margin-left:-5px;
135+
margin-right:5px;
136+
line-height: 1.2em;
137+
}
138+
139+
.brackets-js-hints-type-details {
140+
color: #d3d3d3 !important;
141+
font-weight: 100;
142+
font-style: italic !important;
143+
margin-right: 5px;
144+
float:right;
145+
}
146+
147+
.jshint-description {
148+
display: none;
149+
padding-left: 28px !important;
150+
padding-right:10px !important;
151+
color: #d4d4d4;
152+
word-wrap: break-word;
153+
white-space: normal;
154+
width:400px;
155+
box-sizing: border-box;
156+
}
157+
158+
.dark .jshint-description {
159+
color: #ccc;
160+
}
161+
162+
.jshint-jsdoc {
163+
display: none;
164+
padding-left: 28px !important;
165+
padding-right: 10px !important;
166+
color: grey;
167+
word-wrap: break-word;
168+
white-space: normal;
169+
width: 400px;
170+
box-sizing: border-box;
171+
float: left;
172+
clear: left;
173+
max-height: 3em;
174+
overflow: hidden;
175+
}
176+
177+
.dark .jshint-jsdoc {
178+
color: #ccc;
179+
}
180+
181+
.jshint-link {
182+
float:right;
183+
display:none;
184+
margin-right: 10px !important;
185+
}
186+
187+
.jshint-link:before {
188+
float: right;
189+
color: orangered !important;
190+
content: '?' !important;
191+
border: 1px orangered solid;
192+
width: 10px;
193+
text-align: center;
194+
line-height: 1em;
195+
visibility: visible !important;
196+
}
197+
198+
.highlight .jshint-link {
199+
display:block;
200+
}
201+
202+
.highlight .jshint-description {
203+
display: block;
204+
color: #6495ed !important;
205+
}
206+
207+
.highlight .jshint-jsdoc {
208+
display: block;
209+
}
210+
211+
.dark .brackets-js-hints-type-details {
212+
color: #696969 !important;
213+
}
214+
215+
.highlight .brackets-js-hints-type-details {
216+
color: #6495ed !important;
217+
display: inline-block;
218+
}
219+
220+
.brackets-js-hints-type-details.keyword {
221+
color: #6495ed !important;
222+
}
223+
224+
24225
.brackets-js-hints.priority-high {
25226
color: #486c00; /* green */
26227
}

src/extensions/default/JavaScriptCodeHints/tern-worker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ var config = {};
168168
query.types = true;
169169
query.expandWordForward = false;
170170
query.lineCharPositions = true;
171+
query.docs = true;
172+
query.urls = true;
171173

172174
var request = {query: query, files: [], offset: offset, timeout: inferenceTimeout};
173175
if (fileInfo.type !== MessageIds.TERN_FILE_INFO_TYPE_EMPTY) {
@@ -294,7 +296,7 @@ var config = {};
294296
for (i = 0; i < data.completions.length; ++i) {
295297
var completion = data.completions[i];
296298
completions.push({value: completion.name, type: completion.type, depth: completion.depth,
297-
guess: completion.guess, origin: completion.origin});
299+
guess: completion.guess, origin: completion.origin, doc: completion.doc, url: completion.url});
298300
}
299301
}
300302

0 commit comments

Comments
 (0)