tern.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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="//ternjs.net/node_modules/acorn/dist/acorn.js"></script>
  15. <script src="//ternjs.net/node_modules/acorn/dist/acorn_loose.js"></script>
  16. <script src="//ternjs.net/node_modules/acorn/dist/walk.js"></script>
  17. <script src="//ternjs.net/doc/demo/polyfill.js"></script>
  18. <script src="//ternjs.net/lib/signal.js"></script>
  19. <script src="//ternjs.net/lib/tern.js"></script>
  20. <script src="//ternjs.net/lib/def.js"></script>
  21. <script src="//ternjs.net/lib/comment.js"></script>
  22. <script src="//ternjs.net/lib/infer.js"></script>
  23. <script src="//ternjs.net/plugin/doc_comment.js"></script>
  24. <style>
  25. .CodeMirror {border: 1px solid #ddd;}
  26. </style>
  27. <div id=nav>
  28. <a href="https://codemirror.net"><h1>CodeMirror</h1><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/codemirror/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-o 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></form>
  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-O</dt><dd>Find docs for the expression at the cursor</dd>
  71. <dt>Ctrl-I</dt><dd>Find type at cursor</dd>
  72. <dt>Alt-.</dt><dd>Jump to definition (Alt-, to jump back)</dd>
  73. <dt>Ctrl-Q</dt><dd>Rename variable</dd>
  74. <dt>Ctrl-.</dt><dd>Select all occurrences of a variable</dd>
  75. </dl>
  76. <p>Documentation is sparse for now. See the top of
  77. the <a href="../addon/tern/tern.js">script</a> for a rough API
  78. overview.</p>
  79. <script>
  80. function getURL(url, c) {
  81. var xhr = new XMLHttpRequest();
  82. xhr.open("get", url, true);
  83. xhr.send();
  84. xhr.onreadystatechange = function() {
  85. if (xhr.readyState != 4) return;
  86. if (xhr.status < 400) return c(null, xhr.responseText);
  87. var e = new Error(xhr.responseText || "No response");
  88. e.status = xhr.status;
  89. c(e);
  90. };
  91. }
  92. var server;
  93. getURL("//ternjs.net/defs/ecmascript.json", function(err, code) {
  94. if (err) throw new Error("Request for ecmascript.json: " + err);
  95. server = new CodeMirror.TernServer({defs: [JSON.parse(code)]});
  96. editor.setOption("extraKeys", {
  97. "Ctrl-Space": function(cm) { server.complete(cm); },
  98. "Ctrl-I": function(cm) { server.showType(cm); },
  99. "Ctrl-O": function(cm) { server.showDocs(cm); },
  100. "Alt-.": function(cm) { server.jumpToDef(cm); },
  101. "Alt-,": function(cm) { server.jumpBack(cm); },
  102. "Ctrl-Q": function(cm) { server.rename(cm); },
  103. "Ctrl-.": function(cm) { server.selectName(cm); }
  104. })
  105. editor.on("cursorActivity", function(cm) { server.updateArgHints(cm); });
  106. });
  107. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  108. lineNumbers: true,
  109. mode: "javascript"
  110. });
  111. </script>
  112. </article>