public/lib/CodeMirror/addon/hint/anyword-hint.js   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 1
dl 0
loc 8
rs 9.3333
c 0
b 0
f 0
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
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
8
    define(["../../lib/codemirror"], mod);
9
  else // Plain browser env
10
    mod(CodeMirror);
0 ignored issues
show
Bug introduced by
The variable CodeMirror seems to be never declared. If this is a global, consider adding a /** global: CodeMirror */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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 = [], 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])) {
0 ignored issues
show
Best Practice introduced by
Comparing m.0.lastIndexOf(curWord, 0) to 0 using the == operator is not safe. Consider using === instead.
Loading history...
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
});
42