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.

77 lines
2.7 KiB

2 months ago
  1. <!doctype html>
  2. <title>CodeMirror: Any Word Completion Demo</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../doc/docs.css">
  5. <link rel="stylesheet" href="../lib/codemirror.css">
  6. <link rel="stylesheet" href="../addon/hint/show-hint.css">
  7. <script src="../lib/codemirror.js"></script>
  8. <script src="../addon/hint/show-hint.js"></script>
  9. <script src="../addon/hint/anyword-hint.js" id=anyword></script>
  10. <script src="../mode/javascript/javascript.js"></script>
  11. <div id=nav>
  12. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
  13. <ul>
  14. <li><a href="../index.html">Home</a>
  15. <li><a href="../doc/manual.html">Manual</a>
  16. <li><a href="https://github.com/codemirror/codemirror5">Code</a>
  17. </ul>
  18. <ul>
  19. <li><a class=active href="#">Any Word Completion</a>
  20. </ul>
  21. </div>
  22. <article>
  23. <h2>Any Word Completion Demo</h2>
  24. <form><textarea id="code" name="code">
  25. (function() {
  26. "use strict";
  27. var WORD = /[\w$]+/, RANGE = 500;
  28. CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
  29. var word = options && options.word || WORD;
  30. var range = options && options.range || RANGE;
  31. var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
  32. var end = cur.ch, start = end;
  33. while (start && word.test(curLine.charAt(start - 1))) --start;
  34. var curWord = start != end && curLine.slice(start, end);
  35. var list = options && options.list || [], seen = {};
  36. var re = new RegExp(word.source, "g");
  37. for (var dir = -1; dir <= 1; dir += 2) {
  38. var line = cur.line, endLine = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
  39. for (; line != endLine; line += dir) {
  40. var text = editor.getLine(line), m;
  41. while (m = re.exec(text)) {
  42. if (line == cur.line && m[0] === curWord) continue;
  43. if ((!curWord || m[0].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[0])) {
  44. seen[m[0]] = true;
  45. list.push(m[0]);
  46. }
  47. }
  48. }
  49. }
  50. return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
  51. });
  52. })();
  53. </textarea></form>
  54. <p>Press <strong>ctrl-space</strong> to activate autocompletion. The
  55. completion uses
  56. the <a href="../doc/manual.html#addon_anyword-hint">anyword-hint.js</a>
  57. module, which simply looks at nearby words in the buffer and completes
  58. to those.</p>
  59. <script>
  60. CodeMirror.commands.autocomplete = function(cm) {
  61. cm.showHint({hint: CodeMirror.hint.anyword});
  62. }
  63. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  64. lineNumbers: true,
  65. extraKeys: {"Ctrl-Space": "autocomplete"}
  66. });
  67. </script>
  68. </article>