|
| 1 | +// CodeMirror, copyright (c) by Marijn Haverbeke and others |
| 2 | +// Distributed under an MIT license: https://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 | + |
| 18 | + var word = options && options.word || WORD; |
| 19 | + var range = options && options.range || RANGE; |
| 20 | + var cur = editor.getCursor(), curLine = editor.getLine(cur.line); |
| 21 | + var end = cur.ch, start = end; |
| 22 | + while (start && word.test(curLine.charAt(start - 1))) --start; |
| 23 | + var curWord = start != end && curLine.slice(start, end); |
| 24 | + |
| 25 | + options.list = MPG.mongoDBKeywords.concat(MPG.collectionFields); |
| 26 | + |
| 27 | + /** |
| 28 | + * Code block taken from Mongolo project. |
| 29 | + * @see https://github.com/tetreum/mongolo/blob/develop/htdocs/js/src/custom-hint.js |
| 30 | + */ |
| 31 | + |
| 32 | + var list = [], |
| 33 | + suggestedWord, |
| 34 | + k; |
| 35 | + |
| 36 | + if (curWord) |
| 37 | + { |
| 38 | + for (k in options.list) |
| 39 | + { |
| 40 | + if (!options.list.hasOwnProperty(k)) {continue;} |
| 41 | + suggestedWord = options.list[k]; |
| 42 | + |
| 43 | + if (suggestedWord.toLowerCase().startsWith(curWord.toLowerCase()) && list.indexOf(suggestedWord) == -1) { |
| 44 | + list.push(suggestedWord); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)}; |
| 50 | + }); |
| 51 | +}); |
0 commit comments