tern.html 4.2 KB

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