Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
51007 views
1
(function() {
2
"use strict";
3
4
var HINT_ELEMENT_CLASS = "CodeMirror-hint";
5
var ACTIVE_HINT_ELEMENT_CLASS = "CodeMirror-hint-active";
6
7
CodeMirror.showHint = function(cm, getHints, options) {
8
// We want a single cursor position.
9
if (cm.somethingSelected()) return;
10
if (getHints == null) {
11
if (options && options.async) return;
12
else getHints = CodeMirror.hint.auto;
13
}
14
15
if (cm.state.completionActive) cm.state.completionActive.close();
16
17
var completion = cm.state.completionActive = new Completion(cm, getHints, options || {});
18
CodeMirror.signal(cm, "startCompletion", cm);
19
if (completion.options.async)
20
getHints(cm, function(hints) { completion.showHints(hints); }, completion.options);
21
else
22
return completion.showHints(getHints(cm, completion.options));
23
};
24
25
function Completion(cm, getHints, options) {
26
this.cm = cm;
27
this.getHints = getHints;
28
this.options = options;
29
this.widget = this.onClose = null;
30
}
31
32
Completion.prototype = {
33
close: function() {
34
if (!this.active()) return;
35
this.cm.state.completionActive = null;
36
37
if (this.widget) this.widget.close();
38
if (this.onClose) this.onClose();
39
CodeMirror.signal(this.cm, "endCompletion", this.cm);
40
},
41
42
active: function() {
43
return this.cm.state.completionActive == this;
44
},
45
46
pick: function(data, i) {
47
var completion = data.list[i];
48
if (completion.hint) completion.hint(this.cm, data, completion);
49
else this.cm.replaceRange(getText(completion), completion.from||data.from, completion.to||data.to);
50
CodeMirror.signal(data, "pick", completion);
51
this.close();
52
},
53
54
showHints: function(data) {
55
if (!data || !data.list.length || !this.active()) return this.close();
56
57
if (this.options.completeSingle != false && data.list.length == 1)
58
this.pick(data, 0);
59
else
60
this.showWidget(data);
61
},
62
63
showWidget: function(data) {
64
this.widget = new Widget(this, data);
65
CodeMirror.signal(data, "shown");
66
67
var debounce = 0, completion = this, finished;
68
var closeOn = this.options.closeCharacters || /[\s()\[\]{};:>,]/;
69
var startPos = this.cm.getCursor(), startLen = this.cm.getLine(startPos.line).length;
70
71
var requestAnimationFrame = window.requestAnimationFrame || function(fn) {
72
return setTimeout(fn, 1000/60);
73
};
74
var cancelAnimationFrame = window.cancelAnimationFrame || clearTimeout;
75
76
function done() {
77
if (finished) return;
78
finished = true;
79
completion.close();
80
completion.cm.off("cursorActivity", activity);
81
if (data) CodeMirror.signal(data, "close");
82
}
83
84
function update() {
85
if (finished) return;
86
CodeMirror.signal(data, "update");
87
if (completion.options.async)
88
completion.getHints(completion.cm, finishUpdate, completion.options);
89
else
90
finishUpdate(completion.getHints(completion.cm, completion.options));
91
}
92
function finishUpdate(data_) {
93
data = data_;
94
if (finished) return;
95
if (!data || !data.list.length) return done();
96
completion.widget = new Widget(completion, data);
97
}
98
99
function clearDebounce() {
100
if (debounce) {
101
cancelAnimationFrame(debounce);
102
debounce = 0;
103
}
104
}
105
106
function activity() {
107
clearDebounce();
108
var pos = completion.cm.getCursor(), line = completion.cm.getLine(pos.line);
109
if (pos.line != startPos.line || line.length - pos.ch != startLen - startPos.ch ||
110
pos.ch < startPos.ch || completion.cm.somethingSelected() ||
111
(pos.ch && closeOn.test(line.charAt(pos.ch - 1)))) {
112
completion.close();
113
} else {
114
debounce = requestAnimationFrame(update);
115
if (completion.widget) completion.widget.close();
116
}
117
}
118
this.cm.on("cursorActivity", activity);
119
this.onClose = done;
120
}
121
};
122
123
function getText(completion) {
124
if (typeof completion == "string") return completion;
125
else return completion.text;
126
}
127
128
function buildKeyMap(options, handle) {
129
var baseMap = {
130
Up: function() {handle.moveFocus(-1);},
131
Down: function() {handle.moveFocus(1);},
132
PageUp: function() {handle.moveFocus(-handle.menuSize() + 1, true);},
133
PageDown: function() {handle.moveFocus(handle.menuSize() - 1, true);},
134
Home: function() {handle.setFocus(0);},
135
End: function() {handle.setFocus(handle.length - 1);},
136
Enter: handle.pick,
137
Tab: handle.pick,
138
Esc: handle.close
139
};
140
var ourMap = options.customKeys ? {} : baseMap;
141
function addBinding(key, val) {
142
var bound;
143
if (typeof val != "string")
144
bound = function(cm) { return val(cm, handle); };
145
// This mechanism is deprecated
146
else if (baseMap.hasOwnProperty(val))
147
bound = baseMap[val];
148
else
149
bound = val;
150
ourMap[key] = bound;
151
}
152
if (options.customKeys)
153
for (var key in options.customKeys) if (options.customKeys.hasOwnProperty(key))
154
addBinding(key, options.customKeys[key]);
155
if (options.extraKeys)
156
for (var key in options.extraKeys) if (options.extraKeys.hasOwnProperty(key))
157
addBinding(key, options.extraKeys[key]);
158
return ourMap;
159
}
160
161
function getHintElement(hintsElement, el) {
162
while (el && el != hintsElement) {
163
if (el.nodeName.toUpperCase() === "LI" && el.parentNode == hintsElement) return el;
164
el = el.parentNode;
165
}
166
}
167
168
function Widget(completion, data) {
169
this.completion = completion;
170
this.data = data;
171
var widget = this, cm = completion.cm, options = completion.options;
172
173
var hints = this.hints = document.createElement("ul");
174
hints.className = "CodeMirror-hints";
175
this.selectedHint = options.getDefaultSelection ? options.getDefaultSelection(cm,options,data) : 0;
176
177
var completions = data.list;
178
for (var i = 0; i < completions.length; ++i) {
179
var elt = hints.appendChild(document.createElement("li")), cur = completions[i];
180
var className = HINT_ELEMENT_CLASS + (i != this.selectedHint ? "" : " " + ACTIVE_HINT_ELEMENT_CLASS);
181
if (cur.className != null) className = cur.className + " " + className;
182
elt.className = className;
183
if (cur.render) cur.render(elt, data, cur);
184
else elt.appendChild(document.createTextNode(cur.displayText || getText(cur)));
185
elt.hintId = i;
186
}
187
188
var pos = cm.cursorCoords(options.alignWithWord !== false ? data.from : null);
189
var left = pos.left, top = pos.bottom, below = true;
190
hints.style.left = left + "px";
191
hints.style.top = top + "px";
192
// If we're at the edge of the screen, then we want the menu to appear on the left of the cursor.
193
var winW = window.innerWidth || Math.max(document.body.offsetWidth, document.documentElement.offsetWidth);
194
var winH = window.innerHeight || Math.max(document.body.offsetHeight, document.documentElement.offsetHeight);
195
(options.container || document.body).appendChild(hints);
196
var box = hints.getBoundingClientRect(), overlapY = box.bottom - winH;
197
if (overlapY > 0) {
198
var height = box.bottom - box.top, curTop = box.top - (pos.bottom - pos.top);
199
if (curTop - height > 0) { // Fits above cursor
200
hints.style.top = (top = curTop - height) + "px";
201
below = false;
202
} else if (height > winH) {
203
hints.style.height = (winH - 5) + "px";
204
hints.style.top = (top = pos.bottom - box.top) + "px";
205
var cursor = cm.getCursor();
206
if (data.from.ch != cursor.ch) {
207
pos = cm.cursorCoords(cursor);
208
hints.style.left = (left = pos.left) + "px";
209
box = hints.getBoundingClientRect();
210
}
211
}
212
}
213
var overlapX = box.left - winW;
214
if (overlapX > 0) {
215
if (box.right - box.left > winW) {
216
hints.style.width = (winW - 5) + "px";
217
overlapX -= (box.right - box.left) - winW;
218
}
219
hints.style.left = (left = pos.left - overlapX) + "px";
220
}
221
222
cm.addKeyMap(this.keyMap = buildKeyMap(options, {
223
moveFocus: function(n, avoidWrap) { widget.changeActive(widget.selectedHint + n, avoidWrap); },
224
setFocus: function(n) { widget.changeActive(n); },
225
menuSize: function() { return widget.screenAmount(); },
226
length: completions.length,
227
close: function() { completion.close(); },
228
pick: function() { widget.pick(); },
229
data: data
230
}));
231
232
if (options.closeOnUnfocus !== false) {
233
var closingOnBlur;
234
cm.on("blur", this.onBlur = function() { closingOnBlur = setTimeout(function() { completion.close(); }, 100); });
235
cm.on("focus", this.onFocus = function() { clearTimeout(closingOnBlur); });
236
}
237
238
var startScroll = cm.getScrollInfo();
239
cm.on("scroll", this.onScroll = function() {
240
var curScroll = cm.getScrollInfo(), editor = cm.getWrapperElement().getBoundingClientRect();
241
var newTop = top + startScroll.top - curScroll.top;
242
var point = newTop - (window.pageYOffset || (document.documentElement || document.body).scrollTop);
243
if (!below) point += hints.offsetHeight;
244
if (point <= editor.top || point >= editor.bottom) return completion.close();
245
hints.style.top = newTop + "px";
246
hints.style.left = (left + startScroll.left - curScroll.left) + "px";
247
});
248
249
CodeMirror.on(hints, "dblclick", function(e) {
250
var t = getHintElement(hints, e.target || e.srcElement);
251
if (t && t.hintId != null) {widget.changeActive(t.hintId); widget.pick();}
252
});
253
254
CodeMirror.on(hints, "click", function(e) {
255
var t = getHintElement(hints, e.target || e.srcElement);
256
if (t && t.hintId != null) {
257
widget.changeActive(t.hintId);
258
if (options.completeOnSingleClick) widget.pick();
259
}
260
});
261
262
CodeMirror.on(hints, "mousedown", function() {
263
setTimeout(function(){cm.focus();}, 20);
264
});
265
266
CodeMirror.signal(data, "select", completions[0], hints.firstChild);
267
return true;
268
}
269
270
Widget.prototype = {
271
close: function() {
272
if (this.completion.widget != this) return;
273
this.completion.widget = null;
274
this.hints.parentNode.removeChild(this.hints);
275
this.completion.cm.removeKeyMap(this.keyMap);
276
277
var cm = this.completion.cm;
278
if (this.completion.options.closeOnUnfocus !== false) {
279
cm.off("blur", this.onBlur);
280
cm.off("focus", this.onFocus);
281
}
282
cm.off("scroll", this.onScroll);
283
},
284
285
pick: function() {
286
this.completion.pick(this.data, this.selectedHint);
287
},
288
289
changeActive: function(i, avoidWrap) {
290
if (i >= this.data.list.length)
291
i = avoidWrap ? this.data.list.length - 1 : 0;
292
else if (i < 0)
293
i = avoidWrap ? 0 : this.data.list.length - 1;
294
if (this.selectedHint == i) return;
295
var node = this.hints.childNodes[this.selectedHint];
296
node.className = node.className.replace(" " + ACTIVE_HINT_ELEMENT_CLASS, "");
297
node = this.hints.childNodes[this.selectedHint = i];
298
node.className += " " + ACTIVE_HINT_ELEMENT_CLASS;
299
if (node.offsetTop < this.hints.scrollTop)
300
this.hints.scrollTop = node.offsetTop - 3;
301
else if (node.offsetTop + node.offsetHeight > this.hints.scrollTop + this.hints.clientHeight)
302
this.hints.scrollTop = node.offsetTop + node.offsetHeight - this.hints.clientHeight + 3;
303
CodeMirror.signal(this.data, "select", this.data.list[this.selectedHint], node);
304
},
305
306
screenAmount: function() {
307
return Math.floor(this.hints.clientHeight / this.hints.firstChild.offsetHeight) || 1;
308
}
309
};
310
311
CodeMirror.registerHelper("hint", "auto", function(cm, options) {
312
var helpers = cm.getHelpers(cm.getCursor(), "hint"), words;
313
if (helpers.length) {
314
for (var i = 0; i < helpers.length; i++) {
315
var cur = helpers[i](cm, options);
316
if (cur && cur.list.length) return cur;
317
}
318
} else if (words = cm.getHelper(cm.getCursor(), "hintWords")) {
319
if (words) return CodeMirror.hint.fromList(cm, {words: words});
320
} else if (CodeMirror.hint.anyword) {
321
return CodeMirror.hint.anyword(cm, options);
322
}
323
});
324
325
CodeMirror.registerHelper("hint", "fromList", function(cm, options) {
326
var cur = cm.getCursor(), token = cm.getTokenAt(cur);
327
var found = [];
328
for (var i = 0; i < options.words.length; i++) {
329
var word = options.words[i];
330
if (word.slice(0, token.string.length) == token.string)
331
found.push(word);
332
}
333
334
if (found.length) return {
335
list: found,
336
from: CodeMirror.Pos(cur.line, token.start),
337
to: CodeMirror.Pos(cur.line, token.end)
338
};
339
});
340
341
CodeMirror.commands.autocomplete = CodeMirror.showHint;
342
})();
343
344