Fix: merge conflict
[myslice.git] / third-party / codemirror-3.15 / demo / tern.html
1 <!doctype html>
2 <html>
3   <head>
4     <meta charset="utf-8">
5     <title>CodeMirror: Tern Demo</title>
6     <link rel="stylesheet" href="../lib/codemirror.css">
7     <script src="../lib/codemirror.js"></script>
8     <script src="../mode/javascript/javascript.js"></script>
9     <link rel="stylesheet" href="../doc/docs.css">
10     <script src="../addon/dialog/dialog.js"></script>
11     <link rel="stylesheet" href="../addon/dialog/dialog.css">
12     <script src="../addon/hint/show-hint.js"></script>
13     <link rel="stylesheet" href="../addon/hint/show-hint.css">
14     <script src="../addon/tern/tern.js"></script>
15     <link rel="stylesheet" href="../addon/tern/tern.css">
16
17     <!-- NOTE: if you are going to actually deploy this in production,
18          DO NOT hot-link these files. Host them yourself. -->
19     <script src="http://marijnhaverbeke.nl/acorn/acorn.js"></script>
20     <script src="http://marijnhaverbeke.nl/acorn/acorn_loose.js"></script>
21     <script src="http://marijnhaverbeke.nl/acorn/util/walk.js"></script>
22     <script src="http://ternjs.net/lib/signal.js"></script>
23     <script src="http://ternjs.net/lib/tern.js"></script>
24     <script src="http://ternjs.net/lib/def.js"></script>
25     <script src="http://ternjs.net/lib/comment.js"></script>
26     <script src="http://ternjs.net/lib/infer.js"></script>
27     <script src="http://ternjs.net/plugin/doc_comment.js"></script>
28
29     <style>
30       .CodeMirror {border: 1px solid #ddd;}
31     </style>
32   </head>
33   <body>
34     <h1>CodeMirror: Tern Demo</h1>
35
36     <form><textarea id="code" name="code">// Use ctrl-space to complete something
37 // Put the cursor in or after an expression, press ctrl-i to
38 // find its type
39
40 var foo = ["array", "of", "strings"];
41 var bar = foo.slice(0, 2).join("").split("a")[0];
42
43 // Works for locally defined types too.
44
45 function CTor() { this.size = 10; }
46 CTor.prototype.hallo = "hallo";
47
48 var baz = new CTor;
49 baz.
50
51 // You can press ctrl-q when the cursor is on a variable name to
52 // rename it. Try it with CTor...
53
54 // When the cursor is in an argument list, the arguments are
55 // shown below the editor.
56
57 [1].reduce(  );
58
59 // And a little more advanced code...
60
61 (function(exports) {
62   exports.randomElt = function(arr) {
63     return arr[Math.floor(arr.length * Math.random())];
64   };
65   exports.strList = "foo".split("");
66   exports.intList = exports.strList.map(function(s) { return s.charCodeAt(0); });
67 })(window.myMod = {});
68
69 var randomStr = myMod.randomElt(myMod.strList);
70 var randomInt = myMod.randomElt(myMod.intList);
71 </textarea></p>
72
73 <p>Demonstrates integration of <a href="http://ternjs.net/">Tern</a>
74 and CodeMirror. The following keys are bound:</p>
75
76 <dl>
77   <dt>Ctrl-Space</dt><dd>Autocomplete</dd>
78   <dt>Ctrl-I</dt><dd>Find type at cursor</dd>
79   <dt>Alt-.</dt><dd>Jump to definition (Alt-, to jump back)</dd>
80   <dt>Ctrl-Q</dt><dd>Rename variable</dd>
81 </dl>
82
83 <p>Documentation is sparse for now. See the top of
84 the <a href="../addon/tern/tern.js">script</a> for a rough API
85 overview.</p>
86
87 <script>
88   function getURL(url, c) {
89     var xhr = new XMLHttpRequest();
90     xhr.open("get", url, true);
91     xhr.send();
92     xhr.onreadystatechange = function() {
93       if (xhr.readyState != 4) return;
94       if (xhr.status < 400) return c(null, xhr.responseText);
95       var e = new Error(xhr.responseText || "No response");
96       e.status = xhr.status;
97       c(e);
98     };
99   }
100
101   var server;
102   getURL("http://ternjs.net/defs/ecma5.json", function(err, code) {
103     if (err) throw new Error("Request for ecma5.json: " + err);
104     server = new CodeMirror.TernServer({defs: [JSON.parse(code)]});
105     editor.setOption("extraKeys", {
106       "Ctrl-Space": function(cm) { server.complete(cm); },
107       "Ctrl-I": function(cm) { server.showType(cm); },
108       "Alt-.": function(cm) { server.jumpToDef(cm); },
109       "Alt-,": function(cm) { server.jumpBack(cm); },
110       "Ctrl-Q": function(cm) { server.rename(cm); },
111     })
112     editor.on("cursorActivity", function(cm) { server.updateArgHints(cm); });
113   });
114
115   var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
116     lineNumbers: true,
117     mode: "javascript"
118   });
119 </script>
120
121   </body>
122 </html>