-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathHintTransform.js
205 lines (157 loc) · 6.21 KB
/
HintTransform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Some of the code in this module has been derived from brackets javascript hints
* Licensed under MIT
*/
define(function (require /*, exports, module*/) {
"use strict";
var _ = brackets.getModule("thirdparty/lodash"),
HintHelper = require("HintHelper");
var MAX_DISPLAYED_HINTS = 400,
SINGLE_QUOTE = HintHelper.SINGLE_QUOTE,
DOUBLE_QUOTE = HintHelper.DOUBLE_QUOTE;
var Priorities = {
"0": "priority-high",
"1": "priority-medium",
"-1": "priority-low"
};
var sorter = (function(){
var placementOffset = 1000;
var limit = MAX_DISPLAYED_HINTS;
function matchByType(type, criteria, token) {
token.index = token.name.indexOf(criteria);
if (token.typeInfo.name === type) {
return token.index;
}
else {
return token.index + placementOffset;
}
}
function matchByDepth(criteria, token) {
token.index = token.name.indexOf(criteria);
if (!criteria) {
return token.depth;
}
else {
// Give items that match the criteria higher priority than
// items with just perfect depth but no matching criteria.
if ( token.index !== -1 ) {
return token.index + token.depth;
}
else {
return placementOffset + token.depth;
}
}
}
function _sort(tokens, clasify, toHtml) {
var groups = {},
result = {tokens: [], hints: [], html: ""},
hint,
index,
length,
token,
group,
groupdId;
for(index = 0, length = tokens.length; index < length; index++) {
token = tokens[index];
token.typeInfo = HintHelper.typeInfo(token.type);
token.level = groupdId = clasify(token);
group = groups[groupdId] || (groups[groupdId] = {html: '', hints: [], tokens: []});
hint = toHtml(token);
group.html += "<li>" + hint + "</li>";
group.hints.push(hint);
group.tokens.push(token);
}
_.each(groups, function(group /*, groupId*/) {
result.html += group.html;
result.hints.push.apply(result.hints, group.hints);
result.tokens.push.apply(result.tokens, group.tokens);
});
return result;
}
function byFunction(tokens, criteria) {
return _sort(tokens, matchByType.bind(undefined, "function", criteria), tokenToHtml.bind(undefined, criteria));
}
function byMatch(tokens, criteria) {
return _sort(tokens, matchByDepth.bind(undefined, criteria), tokenToHtml.bind(undefined, criteria));
}
function byPass(tokens) {
return tokens.slice(0, limit);
}
function byObject(tokens, criteria) {
return _sort(tokens, matchByType.bind(undefined, "object", criteria), tokenToHtml.bind(undefined, criteria));
}
function byString(tokens, criteria) {
return _sort(tokens, matchByType.bind(undefined, "string", criteria), tokenToHtml.bind(undefined, criteria));
}
return {
byFunction: byFunction,
byMatch: byMatch,
byPass: byPass,
byObject: byObject,
byString: byString
};
})();
function tokenToHtml(criteria, token) {
var hint = token.name,
index = token.index,
icon = token.typeInfo.icon,
priority = Priorities[token.level] || Priorities['1'];
if (token.guess) {
icon += " Tern-completion-guess";
}
var hintHtml;
// higlight the matched portion of each hint
if (index >= 0) {
var prefix = _.escape(hint.slice(0, index)),
match = _.escape(hint.slice(index, index + criteria.length)),
suffix = _.escape(hint.slice(index + criteria.length));
hintHtml = ("<span class='brackets-js-hints {0}'>" +
"<span class='type {1}'></span>" +
"{2}" + //"<span class='prefix'></span>"
"<span class='matched-hint'>{3}</span>" +
"{4}" + //"<span class='suffix'></span>"
"</span>").format(priority, icon, prefix, match, suffix);
}
else {
hintHtml = ("<span class='brackets-js-hints {0}'>" +
"<span class='type {1}'></span>" +
"<span class='hint'>{2}</span>" +
"</span>").format(priority, icon, hint);
}
return hintHtml;
}
/**
* Process hints from tern to sort and create the needed html to display the thints.
*
* @param {{completions: Array}} hints tern response from completion query
* @param {HintsTransform.sort} sortType sorting type for the hints to be generated.
* @returns {Object}
*/
function HintsTransform(hints, sortType) {
var trimmedQuery, lastChar;
var query = hints.text;
var firstChar = query.charAt(0);
if (firstChar === SINGLE_QUOTE || firstChar === DOUBLE_QUOTE) {
trimmedQuery = query.substring(1);
lastChar = trimmedQuery.charAt(trimmedQuery.length - 1);
if (lastChar === SINGLE_QUOTE || lastChar === DOUBLE_QUOTE) {
trimmedQuery = trimmedQuery.substring(0, trimmedQuery.length - 1);
}
}
else {
trimmedQuery = query;
}
// Build list of hints.
return sorter[sortType || HintsTransform.sort.byMatch](hints.result.completions, trimmedQuery);
}
HintsTransform.sort = {
"byFunction": "byFunction",
"byMatch": "byMatch",
"byPass": "byPass",
"byObject": "byObject",
"byString": "byString"
};
return HintsTransform;
});