Any Word Completion Demo

1
(function() {
2
  "use strict";
3
 
4
  var WORD = /[\w$]+/g, RANGE = 500;
5
 
6
  CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
7
    var word = options && options.word || WORD;
8
    var range = options && options.range || RANGE;
9
    var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
10
    var start = cur.ch, end = start;
11
    while (end < curLine.length && word.test(curLine.charAt(end))) ++end;
12
    while (start && word.test(curLine.charAt(start - 1))) --start;
13
    var curWord = start != end && curLine.slice(start, end);
14
 
15
    var list = [], seen = {};
16
    function scan(dir) {
17
      var line = cur.line, end = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
18
      for (; line != end; line += dir) {
19
        var text = editor.getLine(line), m;
20
        word.lastIndex = 0;
21
        while (m = word.exec(text)) {
22
          if ((!curWord || m[0].indexOf(curWord) == 0) && !seen.hasOwnProperty(m[0])) {
23
            seen[m[0]] = true;
24
            list.push(m[0]);
25
          }
26
        }
27
      }
28
    }
29
    scan(-1);
30
    scan(1);
31
    return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
32
  });
33
})();
34
 
 

Press ctrl-space to activate autocompletion. The completion uses the anyword-hint.js module, which simply looks at nearby words in the buffer and completes to those.