github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/public/codemirror/addon/hint/anyword-hint.js (about)

     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 WORD = /[\w$]+/, RANGE = 500;
    15  
    16    CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
    17      var word = options && options.word || WORD;
    18      var range = options && options.range || RANGE;
    19      var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
    20      var end = cur.ch, start = end;
    21      while (start && word.test(curLine.charAt(start - 1))) --start;
    22      var curWord = start != end && curLine.slice(start, end);
    23  
    24      var list = options && options.list || [], seen = {};
    25      var re = new RegExp(word.source, "g");
    26      for (var dir = -1; dir <= 1; dir += 2) {
    27        var line = cur.line, endLine = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
    28        for (; line != endLine; line += dir) {
    29          var text = editor.getLine(line), m;
    30          while (m = re.exec(text)) {
    31            if (line == cur.line && m[0] === curWord) continue;
    32            if ((!curWord || m[0].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[0])) {
    33              seen[m[0]] = true;
    34              list.push(m[0]);
    35            }
    36          }
    37        }
    38      }
    39      return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
    40    });
    41  });