comparison .cms/lib/codemirror/demo/anywordhint.html @ 0:78edf6b517a0 draft

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