anywordhint.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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"></script>
  10. <script src="../mode/javascript/javascript.js"></script>
  11. <div id=nav>
  12. <a href="https://codemirror.net"><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/codemirror">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$]+/g, 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 start = cur.ch, end = start;
  33. while (end < curLine.length && word.test(curLine.charAt(end))) ++end;
  34. while (start && word.test(curLine.charAt(start - 1))) --start;
  35. var curWord = start != end && curLine.slice(start, end);
  36. var list = [], seen = {};
  37. function scan(dir) {
  38. var line = cur.line, end = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
  39. for (; line != end; line += dir) {
  40. var text = editor.getLine(line), m;
  41. word.lastIndex = 0;
  42. while (m = word.exec(text)) {
  43. if ((!curWord || m[0].indexOf(curWord) == 0) && !seen.hasOwnProperty(m[0])) {
  44. seen[m[0]] = true;
  45. list.push(m[0]);
  46. }
  47. }
  48. }
  49. }
  50. scan(-1);
  51. scan(1);
  52. return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
  53. });
  54. })();
  55. </textarea></form>
  56. <p>Press <strong>ctrl-space</strong> to activate autocompletion. The
  57. completion uses
  58. the <a href="../doc/manual.html#addon_anyword-hint">anyword-hint.js</a>
  59. module, which simply looks at nearby words in the buffer and completes
  60. to those.</p>
  61. <script>
  62. CodeMirror.commands.autocomplete = function(cm) {
  63. cm.showHint({hint: CodeMirror.hint.anyword});
  64. }
  65. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  66. lineNumbers: true,
  67. extraKeys: {"Ctrl-Space": "autocomplete"}
  68. });
  69. </script>
  70. </article>