Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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