Commit | Line | Data |
---|---|---|
44f4c339 | 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others |
373d1232 TD |
2 | // Distributed under an MIT license: https://codemirror.net/LICENSE |
3 | ||
704cb1bf | 4 | // declare global: DOMRect |
44f4c339 | 5 | |
837afb80 TD |
6 | (function(mod) { |
7 | if (typeof exports == "object" && typeof module == "object") // CommonJS | |
8 | mod(require("../../lib/codemirror")); | |
9 | else if (typeof define == "function" && define.amd) // AMD | |
10 | define(["../../lib/codemirror"], mod); | |
11 | else // Plain browser env | |
12 | mod(CodeMirror); | |
13 | })(function(CodeMirror) { | |
14 | "use strict"; | |
77b7b761 | 15 | |
837afb80 TD |
16 | var HINT_ELEMENT_CLASS = "CodeMirror-hint"; |
17 | var ACTIVE_HINT_ELEMENT_CLASS = "CodeMirror-hint-active"; | |
18 | ||
44f4c339 AE |
19 | // This is the old interface, kept around for now to stay |
20 | // backwards-compatible. | |
837afb80 | 21 | CodeMirror.showHint = function(cm, getHints, options) { |
44f4c339 AE |
22 | if (!getHints) return cm.showHint(options); |
23 | if (options && options.async) getHints.async = true; | |
24 | var newOpts = {hint: getHints}; | |
25 | if (options) for (var prop in options) newOpts[prop] = options[prop]; | |
26 | return cm.showHint(newOpts); | |
27 | }; | |
28 | ||
29 | CodeMirror.defineExtension("showHint", function(options) { | |
30 | options = parseOptions(this, this.getCursor("start"), options); | |
31 | var selections = this.listSelections() | |
32 | if (selections.length > 1) return; | |
33 | // By default, don't allow completion when something is selected. | |
34 | // A hint function can have a `supportsSelection` property to | |
35 | // indicate that it can handle selections. | |
36 | if (this.somethingSelected()) { | |
37 | if (!options.hint.supportsSelection) return; | |
38 | // Don't try with cross-line selections | |
39 | for (var i = 0; i < selections.length; i++) | |
40 | if (selections[i].head.line != selections[i].anchor.line) return; | |
837afb80 TD |
41 | } |
42 | ||
44f4c339 AE |
43 | if (this.state.completionActive) this.state.completionActive.close(); |
44 | var completion = this.state.completionActive = new Completion(this, options); | |
45 | if (!completion.options.hint) return; | |
77b7b761 | 46 | |
44f4c339 AE |
47 | CodeMirror.signal(this, "startCompletion", this); |
48 | completion.update(true); | |
49 | }); | |
837afb80 | 50 | |
373d1232 TD |
51 | CodeMirror.defineExtension("closeHint", function() { |
52 | if (this.state.completionActive) this.state.completionActive.close() | |
53 | }) | |
54 | ||
44f4c339 | 55 | function Completion(cm, options) { |
837afb80 | 56 | this.cm = cm; |
837afb80 | 57 | this.options = options; |
44f4c339 AE |
58 | this.widget = null; |
59 | this.debounce = 0; | |
60 | this.tick = 0; | |
61 | this.startPos = this.cm.getCursor("start"); | |
62 | this.startLen = this.cm.getLine(this.startPos.line).length - this.cm.getSelection().length; | |
63 | ||
704cb1bf MS |
64 | if (this.options.updateOnCursorActivity) { |
65 | var self = this; | |
66 | cm.on("cursorActivity", this.activityFunc = function() { self.cursorActivity(); }); | |
67 | } | |
77b7b761 TD |
68 | } |
69 | ||
44f4c339 AE |
70 | var requestAnimationFrame = window.requestAnimationFrame || function(fn) { |
71 | return setTimeout(fn, 1000/60); | |
72 | }; | |
73 | var cancelAnimationFrame = window.cancelAnimationFrame || clearTimeout; | |
74 | ||
837afb80 TD |
75 | Completion.prototype = { |
76 | close: function() { | |
77 | if (!this.active()) return; | |
78 | this.cm.state.completionActive = null; | |
44f4c339 | 79 | this.tick = null; |
704cb1bf MS |
80 | if (this.options.updateOnCursorActivity) { |
81 | this.cm.off("cursorActivity", this.activityFunc); | |
82 | } | |
837afb80 | 83 | |
44f4c339 | 84 | if (this.widget && this.data) CodeMirror.signal(this.data, "close"); |
837afb80 | 85 | if (this.widget) this.widget.close(); |
837afb80 TD |
86 | CodeMirror.signal(this.cm, "endCompletion", this.cm); |
87 | }, | |
88 | ||
89 | active: function() { | |
90 | return this.cm.state.completionActive == this; | |
91 | }, | |
92 | ||
93 | pick: function(data, i) { | |
704cb1bf MS |
94 | var completion = data.list[i], self = this; |
95 | this.cm.operation(function() { | |
96 | if (completion.hint) | |
97 | completion.hint(self.cm, data, completion); | |
98 | else | |
99 | self.cm.replaceRange(getText(completion), completion.from || data.from, | |
100 | completion.to || data.to, "complete"); | |
101 | CodeMirror.signal(data, "pick", completion); | |
102 | self.cm.scrollIntoView(); | |
103 | }); | |
104 | if (this.options.closeOnPick) { | |
105 | this.close(); | |
106 | } | |
837afb80 TD |
107 | }, |
108 | ||
44f4c339 AE |
109 | cursorActivity: function() { |
110 | if (this.debounce) { | |
111 | cancelAnimationFrame(this.debounce); | |
112 | this.debounce = 0; | |
113 | } | |
837afb80 | 114 | |
704cb1bf MS |
115 | var identStart = this.startPos; |
116 | if(this.data) { | |
117 | identStart = this.data.from; | |
118 | } | |
119 | ||
44f4c339 AE |
120 | var pos = this.cm.getCursor(), line = this.cm.getLine(pos.line); |
121 | if (pos.line != this.startPos.line || line.length - pos.ch != this.startLen - this.startPos.ch || | |
704cb1bf | 122 | pos.ch < identStart.ch || this.cm.somethingSelected() || |
373d1232 | 123 | (!pos.ch || this.options.closeCharacters.test(line.charAt(pos.ch - 1)))) { |
44f4c339 AE |
124 | this.close(); |
125 | } else { | |
126 | var self = this; | |
127 | this.debounce = requestAnimationFrame(function() {self.update();}); | |
128 | if (this.widget) this.widget.disable(); | |
129 | } | |
837afb80 TD |
130 | }, |
131 | ||
44f4c339 AE |
132 | update: function(first) { |
133 | if (this.tick == null) return | |
134 | var self = this, myTick = ++this.tick | |
135 | fetchHints(this.options.hint, this.cm, this.options, function(data) { | |
136 | if (self.tick == myTick) self.finishUpdate(data, first) | |
137 | }) | |
138 | }, | |
837afb80 | 139 | |
44f4c339 AE |
140 | finishUpdate: function(data, first) { |
141 | if (this.data) CodeMirror.signal(this.data, "update"); | |
837afb80 | 142 | |
44f4c339 AE |
143 | var picked = (this.widget && this.widget.picked) || (first && this.options.completeSingle); |
144 | if (this.widget) this.widget.close(); | |
145 | ||
44f4c339 | 146 | this.data = data; |
837afb80 | 147 | |
44f4c339 AE |
148 | if (data && data.list.length) { |
149 | if (picked && data.list.length == 1) { | |
150 | this.pick(data, 0); | |
837afb80 | 151 | } else { |
44f4c339 AE |
152 | this.widget = new Widget(this, data); |
153 | CodeMirror.signal(data, "shown"); | |
837afb80 TD |
154 | } |
155 | } | |
837afb80 TD |
156 | } |
157 | }; | |
158 | ||
44f4c339 AE |
159 | function parseOptions(cm, pos, options) { |
160 | var editor = cm.options.hintOptions; | |
161 | var out = {}; | |
162 | for (var prop in defaultOptions) out[prop] = defaultOptions[prop]; | |
163 | if (editor) for (var prop in editor) | |
164 | if (editor[prop] !== undefined) out[prop] = editor[prop]; | |
165 | if (options) for (var prop in options) | |
166 | if (options[prop] !== undefined) out[prop] = options[prop]; | |
167 | if (out.hint.resolve) out.hint = out.hint.resolve(cm, pos) | |
168 | return out; | |
169 | } | |
170 | ||
77b7b761 TD |
171 | function getText(completion) { |
172 | if (typeof completion == "string") return completion; | |
173 | else return completion.text; | |
174 | } | |
175 | ||
44f4c339 | 176 | function buildKeyMap(completion, handle) { |
837afb80 TD |
177 | var baseMap = { |
178 | Up: function() {handle.moveFocus(-1);}, | |
179 | Down: function() {handle.moveFocus(1);}, | |
180 | PageUp: function() {handle.moveFocus(-handle.menuSize() + 1, true);}, | |
181 | PageDown: function() {handle.moveFocus(handle.menuSize() - 1, true);}, | |
182 | Home: function() {handle.setFocus(0);}, | |
183 | End: function() {handle.setFocus(handle.length - 1);}, | |
184 | Enter: handle.pick, | |
185 | Tab: handle.pick, | |
186 | Esc: handle.close | |
187 | }; | |
373d1232 | 188 | |
704cb1bf MS |
189 | var mac = /Mac/.test(navigator.platform); |
190 | ||
373d1232 TD |
191 | if (mac) { |
192 | baseMap["Ctrl-P"] = function() {handle.moveFocus(-1);}; | |
193 | baseMap["Ctrl-N"] = function() {handle.moveFocus(1);}; | |
194 | } | |
195 | ||
44f4c339 AE |
196 | var custom = completion.options.customKeys; |
197 | var ourMap = custom ? {} : baseMap; | |
837afb80 TD |
198 | function addBinding(key, val) { |
199 | var bound; | |
200 | if (typeof val != "string") | |
201 | bound = function(cm) { return val(cm, handle); }; | |
202 | // This mechanism is deprecated | |
203 | else if (baseMap.hasOwnProperty(val)) | |
204 | bound = baseMap[val]; | |
205 | else | |
206 | bound = val; | |
207 | ourMap[key] = bound; | |
208 | } | |
44f4c339 AE |
209 | if (custom) |
210 | for (var key in custom) if (custom.hasOwnProperty(key)) | |
211 | addBinding(key, custom[key]); | |
212 | var extra = completion.options.extraKeys; | |
213 | if (extra) | |
214 | for (var key in extra) if (extra.hasOwnProperty(key)) | |
215 | addBinding(key, extra[key]); | |
837afb80 | 216 | return ourMap; |
77b7b761 TD |
217 | } |
218 | ||
837afb80 TD |
219 | function getHintElement(hintsElement, el) { |
220 | while (el && el != hintsElement) { | |
221 | if (el.nodeName.toUpperCase() === "LI" && el.parentNode == hintsElement) return el; | |
222 | el = el.parentNode; | |
77b7b761 | 223 | } |
837afb80 TD |
224 | } |
225 | ||
226 | function Widget(completion, data) { | |
227 | this.completion = completion; | |
228 | this.data = data; | |
44f4c339 AE |
229 | this.picked = false; |
230 | var widget = this, cm = completion.cm; | |
373d1232 TD |
231 | var ownerDocument = cm.getInputField().ownerDocument; |
232 | var parentWindow = ownerDocument.defaultView || ownerDocument.parentWindow; | |
77b7b761 | 233 | |
373d1232 TD |
234 | var hints = this.hints = ownerDocument.createElement("ul"); |
235 | var theme = completion.cm.options.theme; | |
236 | hints.className = "CodeMirror-hints " + theme; | |
44f4c339 | 237 | this.selectedHint = data.selectedHint || 0; |
837afb80 TD |
238 | |
239 | var completions = data.list; | |
77b7b761 | 240 | for (var i = 0; i < completions.length; ++i) { |
373d1232 | 241 | var elt = hints.appendChild(ownerDocument.createElement("li")), cur = completions[i]; |
837afb80 TD |
242 | var className = HINT_ELEMENT_CLASS + (i != this.selectedHint ? "" : " " + ACTIVE_HINT_ELEMENT_CLASS); |
243 | if (cur.className != null) className = cur.className + " " + className; | |
77b7b761 | 244 | elt.className = className; |
837afb80 | 245 | if (cur.render) cur.render(elt, data, cur); |
373d1232 | 246 | else elt.appendChild(ownerDocument.createTextNode(cur.displayText || getText(cur))); |
77b7b761 TD |
247 | elt.hintId = i; |
248 | } | |
837afb80 | 249 | |
704cb1bf | 250 | var container = completion.options.container || ownerDocument.body; |
44f4c339 | 251 | var pos = cm.cursorCoords(completion.options.alignWithWord ? data.from : null); |
77b7b761 | 252 | var left = pos.left, top = pos.bottom, below = true; |
704cb1bf MS |
253 | var offsetLeft = 0, offsetTop = 0; |
254 | if (container !== ownerDocument.body) { | |
255 | // We offset the cursor position because left and top are relative to the offsetParent's top left corner. | |
256 | var isContainerPositioned = ['absolute', 'relative', 'fixed'].indexOf(parentWindow.getComputedStyle(container).position) !== -1; | |
257 | var offsetParent = isContainerPositioned ? container : container.offsetParent; | |
258 | var offsetParentPosition = offsetParent.getBoundingClientRect(); | |
259 | var bodyPosition = ownerDocument.body.getBoundingClientRect(); | |
260 | offsetLeft = (offsetParentPosition.left - bodyPosition.left - offsetParent.scrollLeft); | |
261 | offsetTop = (offsetParentPosition.top - bodyPosition.top - offsetParent.scrollTop); | |
262 | } | |
263 | hints.style.left = (left - offsetLeft) + "px"; | |
264 | hints.style.top = (top - offsetTop) + "px"; | |
265 | ||
77b7b761 | 266 | // If we're at the edge of the screen, then we want the menu to appear on the left of the cursor. |
373d1232 TD |
267 | var winW = parentWindow.innerWidth || Math.max(ownerDocument.body.offsetWidth, ownerDocument.documentElement.offsetWidth); |
268 | var winH = parentWindow.innerHeight || Math.max(ownerDocument.body.offsetHeight, ownerDocument.documentElement.offsetHeight); | |
704cb1bf | 269 | container.appendChild(hints); |
44f4c339 | 270 | |
704cb1bf MS |
271 | var box = completion.options.moveOnOverlap ? hints.getBoundingClientRect() : new DOMRect(); |
272 | var scrolls = completion.options.paddingForScrollbar ? hints.scrollHeight > hints.clientHeight + 1 : false; | |
273 | ||
274 | // Compute in the timeout to avoid reflow on init | |
275 | var startScroll; | |
276 | setTimeout(function() { startScroll = cm.getScrollInfo(); }); | |
277 | ||
278 | var overlapY = box.bottom - winH; | |
837afb80 | 279 | if (overlapY > 0) { |
44f4c339 | 280 | var height = box.bottom - box.top, curTop = pos.top - (pos.bottom - box.top); |
837afb80 | 281 | if (curTop - height > 0) { // Fits above cursor |
704cb1bf | 282 | hints.style.top = (top = pos.top - height - offsetTop) + "px"; |
837afb80 TD |
283 | below = false; |
284 | } else if (height > winH) { | |
285 | hints.style.height = (winH - 5) + "px"; | |
704cb1bf | 286 | hints.style.top = (top = pos.bottom - box.top - offsetTop) + "px"; |
837afb80 TD |
287 | var cursor = cm.getCursor(); |
288 | if (data.from.ch != cursor.ch) { | |
289 | pos = cm.cursorCoords(cursor); | |
704cb1bf | 290 | hints.style.left = (left = pos.left - offsetLeft) + "px"; |
837afb80 TD |
291 | box = hints.getBoundingClientRect(); |
292 | } | |
293 | } | |
294 | } | |
44f4c339 | 295 | var overlapX = box.right - winW; |
704cb1bf | 296 | if (scrolls) overlapX += cm.display.nativeBarWidth; |
77b7b761 TD |
297 | if (overlapX > 0) { |
298 | if (box.right - box.left > winW) { | |
299 | hints.style.width = (winW - 5) + "px"; | |
300 | overlapX -= (box.right - box.left) - winW; | |
301 | } | |
704cb1bf | 302 | hints.style.left = (left = pos.left - overlapX - offsetLeft) + "px"; |
77b7b761 | 303 | } |
44f4c339 AE |
304 | if (scrolls) for (var node = hints.firstChild; node; node = node.nextSibling) |
305 | node.style.paddingRight = cm.display.nativeBarWidth + "px" | |
77b7b761 | 306 | |
44f4c339 | 307 | cm.addKeyMap(this.keyMap = buildKeyMap(completion, { |
837afb80 TD |
308 | moveFocus: function(n, avoidWrap) { widget.changeActive(widget.selectedHint + n, avoidWrap); }, |
309 | setFocus: function(n) { widget.changeActive(n); }, | |
310 | menuSize: function() { return widget.screenAmount(); }, | |
311 | length: completions.length, | |
312 | close: function() { completion.close(); }, | |
313 | pick: function() { widget.pick(); }, | |
314 | data: data | |
315 | })); | |
77b7b761 | 316 | |
44f4c339 | 317 | if (completion.options.closeOnUnfocus) { |
837afb80 TD |
318 | var closingOnBlur; |
319 | cm.on("blur", this.onBlur = function() { closingOnBlur = setTimeout(function() { completion.close(); }, 100); }); | |
320 | cm.on("focus", this.onFocus = function() { clearTimeout(closingOnBlur); }); | |
77b7b761 TD |
321 | } |
322 | ||
837afb80 | 323 | cm.on("scroll", this.onScroll = function() { |
77b7b761 | 324 | var curScroll = cm.getScrollInfo(), editor = cm.getWrapperElement().getBoundingClientRect(); |
704cb1bf | 325 | if (!startScroll) startScroll = cm.getScrollInfo(); |
837afb80 | 326 | var newTop = top + startScroll.top - curScroll.top; |
373d1232 | 327 | var point = newTop - (parentWindow.pageYOffset || (ownerDocument.documentElement || ownerDocument.body).scrollTop); |
77b7b761 | 328 | if (!below) point += hints.offsetHeight; |
837afb80 | 329 | if (point <= editor.top || point >= editor.bottom) return completion.close(); |
77b7b761 TD |
330 | hints.style.top = newTop + "px"; |
331 | hints.style.left = (left + startScroll.left - curScroll.left) + "px"; | |
837afb80 TD |
332 | }); |
333 | ||
77b7b761 | 334 | CodeMirror.on(hints, "dblclick", function(e) { |
837afb80 TD |
335 | var t = getHintElement(hints, e.target || e.srcElement); |
336 | if (t && t.hintId != null) {widget.changeActive(t.hintId); widget.pick();} | |
77b7b761 | 337 | }); |
837afb80 | 338 | |
77b7b761 | 339 | CodeMirror.on(hints, "click", function(e) { |
837afb80 TD |
340 | var t = getHintElement(hints, e.target || e.srcElement); |
341 | if (t && t.hintId != null) { | |
342 | widget.changeActive(t.hintId); | |
44f4c339 | 343 | if (completion.options.completeOnSingleClick) widget.pick(); |
837afb80 | 344 | } |
77b7b761 | 345 | }); |
837afb80 | 346 | |
77b7b761 TD |
347 | CodeMirror.on(hints, "mousedown", function() { |
348 | setTimeout(function(){cm.focus();}, 20); | |
349 | }); | |
350 | ||
704cb1bf MS |
351 | // The first hint doesn't need to be scrolled to on init |
352 | var selectedHintRange = this.getSelectedHintRange(); | |
353 | if (selectedHintRange.from !== 0 || selectedHintRange.to !== 0) { | |
354 | this.scrollToActive(); | |
355 | } | |
356 | ||
373d1232 | 357 | CodeMirror.signal(data, "select", completions[this.selectedHint], hints.childNodes[this.selectedHint]); |
77b7b761 TD |
358 | return true; |
359 | } | |
360 | ||
837afb80 TD |
361 | Widget.prototype = { |
362 | close: function() { | |
363 | if (this.completion.widget != this) return; | |
364 | this.completion.widget = null; | |
704cb1bf | 365 | if (this.hints.parentNode) this.hints.parentNode.removeChild(this.hints); |
837afb80 TD |
366 | this.completion.cm.removeKeyMap(this.keyMap); |
367 | ||
368 | var cm = this.completion.cm; | |
44f4c339 | 369 | if (this.completion.options.closeOnUnfocus) { |
837afb80 TD |
370 | cm.off("blur", this.onBlur); |
371 | cm.off("focus", this.onFocus); | |
372 | } | |
373 | cm.off("scroll", this.onScroll); | |
374 | }, | |
375 | ||
44f4c339 AE |
376 | disable: function() { |
377 | this.completion.cm.removeKeyMap(this.keyMap); | |
378 | var widget = this; | |
379 | this.keyMap = {Enter: function() { widget.picked = true; }}; | |
380 | this.completion.cm.addKeyMap(this.keyMap); | |
381 | }, | |
382 | ||
837afb80 TD |
383 | pick: function() { |
384 | this.completion.pick(this.data, this.selectedHint); | |
385 | }, | |
386 | ||
387 | changeActive: function(i, avoidWrap) { | |
388 | if (i >= this.data.list.length) | |
389 | i = avoidWrap ? this.data.list.length - 1 : 0; | |
390 | else if (i < 0) | |
391 | i = avoidWrap ? 0 : this.data.list.length - 1; | |
392 | if (this.selectedHint == i) return; | |
393 | var node = this.hints.childNodes[this.selectedHint]; | |
373d1232 | 394 | if (node) node.className = node.className.replace(" " + ACTIVE_HINT_ELEMENT_CLASS, ""); |
837afb80 TD |
395 | node = this.hints.childNodes[this.selectedHint = i]; |
396 | node.className += " " + ACTIVE_HINT_ELEMENT_CLASS; | |
704cb1bf | 397 | this.scrollToActive() |
837afb80 TD |
398 | CodeMirror.signal(this.data, "select", this.data.list[this.selectedHint], node); |
399 | }, | |
400 | ||
704cb1bf MS |
401 | scrollToActive: function() { |
402 | var selectedHintRange = this.getSelectedHintRange(); | |
403 | var node1 = this.hints.childNodes[selectedHintRange.from]; | |
404 | var node2 = this.hints.childNodes[selectedHintRange.to]; | |
405 | var firstNode = this.hints.firstChild; | |
406 | if (node1.offsetTop < this.hints.scrollTop) | |
407 | this.hints.scrollTop = node1.offsetTop - firstNode.offsetTop; | |
408 | else if (node2.offsetTop + node2.offsetHeight > this.hints.scrollTop + this.hints.clientHeight) | |
409 | this.hints.scrollTop = node2.offsetTop + node2.offsetHeight - this.hints.clientHeight + firstNode.offsetTop; | |
410 | }, | |
411 | ||
837afb80 TD |
412 | screenAmount: function() { |
413 | return Math.floor(this.hints.clientHeight / this.hints.firstChild.offsetHeight) || 1; | |
704cb1bf MS |
414 | }, |
415 | ||
416 | getSelectedHintRange: function() { | |
417 | var margin = this.completion.options.scrollMargin || 0; | |
418 | return { | |
419 | from: Math.max(0, this.selectedHint - margin), | |
420 | to: Math.min(this.data.list.length - 1, this.selectedHint + margin), | |
421 | }; | |
837afb80 TD |
422 | } |
423 | }; | |
424 | ||
44f4c339 AE |
425 | function applicableHelpers(cm, helpers) { |
426 | if (!cm.somethingSelected()) return helpers | |
427 | var result = [] | |
428 | for (var i = 0; i < helpers.length; i++) | |
429 | if (helpers[i].supportsSelection) result.push(helpers[i]) | |
430 | return result | |
431 | } | |
432 | ||
433 | function fetchHints(hint, cm, options, callback) { | |
434 | if (hint.async) { | |
435 | hint(cm, callback, options) | |
436 | } else { | |
437 | var result = hint(cm, options) | |
438 | if (result && result.then) result.then(callback) | |
439 | else callback(result) | |
440 | } | |
441 | } | |
442 | ||
443 | function resolveAutoHints(cm, pos) { | |
444 | var helpers = cm.getHelpers(pos, "hint"), words | |
837afb80 | 445 | if (helpers.length) { |
44f4c339 AE |
446 | var resolved = function(cm, callback, options) { |
447 | var app = applicableHelpers(cm, helpers); | |
448 | function run(i) { | |
449 | if (i == app.length) return callback(null) | |
450 | fetchHints(app[i], cm, options, function(result) { | |
451 | if (result && result.list.length > 0) callback(result) | |
452 | else run(i + 1) | |
453 | }) | |
454 | } | |
455 | run(0) | |
837afb80 | 456 | } |
44f4c339 AE |
457 | resolved.async = true |
458 | resolved.supportsSelection = true | |
459 | return resolved | |
837afb80 | 460 | } else if (words = cm.getHelper(cm.getCursor(), "hintWords")) { |
44f4c339 | 461 | return function(cm) { return CodeMirror.hint.fromList(cm, {words: words}) } |
837afb80 | 462 | } else if (CodeMirror.hint.anyword) { |
44f4c339 AE |
463 | return function(cm, options) { return CodeMirror.hint.anyword(cm, options) } |
464 | } else { | |
465 | return function() {} | |
837afb80 | 466 | } |
44f4c339 AE |
467 | } |
468 | ||
469 | CodeMirror.registerHelper("hint", "auto", { | |
470 | resolve: resolveAutoHints | |
837afb80 TD |
471 | }); |
472 | ||
473 | CodeMirror.registerHelper("hint", "fromList", function(cm, options) { | |
373d1232 TD |
474 | var cur = cm.getCursor(), token = cm.getTokenAt(cur) |
475 | var term, from = CodeMirror.Pos(cur.line, token.start), to = cur | |
476 | if (token.start < cur.ch && /\w/.test(token.string.charAt(cur.ch - token.start - 1))) { | |
477 | term = token.string.substr(0, cur.ch - token.start) | |
44f4c339 | 478 | } else { |
373d1232 TD |
479 | term = "" |
480 | from = cur | |
44f4c339 | 481 | } |
837afb80 TD |
482 | var found = []; |
483 | for (var i = 0; i < options.words.length; i++) { | |
484 | var word = options.words[i]; | |
44f4c339 | 485 | if (word.slice(0, term.length) == term) |
837afb80 TD |
486 | found.push(word); |
487 | } | |
488 | ||
44f4c339 | 489 | if (found.length) return {list: found, from: from, to: to}; |
837afb80 TD |
490 | }); |
491 | ||
492 | CodeMirror.commands.autocomplete = CodeMirror.showHint; | |
44f4c339 AE |
493 | |
494 | var defaultOptions = { | |
495 | hint: CodeMirror.hint.auto, | |
496 | completeSingle: true, | |
497 | alignWithWord: true, | |
498 | closeCharacters: /[\s()\[\]{};:>,]/, | |
704cb1bf | 499 | closeOnPick: true, |
44f4c339 | 500 | closeOnUnfocus: true, |
704cb1bf | 501 | updateOnCursorActivity: true, |
44f4c339 AE |
502 | completeOnSingleClick: true, |
503 | container: null, | |
504 | customKeys: null, | |
704cb1bf MS |
505 | extraKeys: null, |
506 | paddingForScrollbar: true, | |
507 | moveOnOverlap: true, | |
44f4c339 AE |
508 | }; |
509 | ||
510 | CodeMirror.defineOption("hintOptions", null); | |
837afb80 | 511 | }); |