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