You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.6 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var WORD = /[\w$]+/, RANGE = 500;
  13. CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
  14. var word = options && options.word || WORD;
  15. var range = options && options.range || RANGE;
  16. var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
  17. var end = cur.ch, start = end;
  18. while (start && word.test(curLine.charAt(start - 1))) --start;
  19. var curWord = start != end && curLine.slice(start, end);
  20. var list = options && options.list || [], seen = {};
  21. var re = new RegExp(word.source, "g");
  22. for (var dir = -1; dir <= 1; dir += 2) {
  23. var line = cur.line, endLine = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
  24. for (; line != endLine; line += dir) {
  25. var text = editor.getLine(line), m;
  26. while (m = re.exec(text)) {
  27. if (line == cur.line && m[0] === curWord) continue;
  28. if ((!curWord || m[0].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[0])) {
  29. seen[m[0]] = true;
  30. list.push(m[0]);
  31. }
  32. }
  33. }
  34. }
  35. return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
  36. });
  37. });