~ubuntu-branches/ubuntu/utopic/codemirror-js/utopic

« back to all changes in this revision

Viewing changes to demo/complete.html

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2012-04-12 12:25:28 UTC
  • Revision ID: package-import@ubuntu.com-20120412122528-8xp5a8frj4h1d3ee
Tags: upstream-2.23
ImportĀ upstreamĀ versionĀ 2.23

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!doctype html>
 
2
<html>
 
3
  <head>
 
4
    <title>CodeMirror: Autocomplete Demo</title>
 
5
    <link rel="stylesheet" href="../lib/codemirror.css">
 
6
    <script src="../lib/codemirror.js"></script>
 
7
    <script src="../lib/util/simple-hint.js"></script>
 
8
    <link rel="stylesheet" href="../lib/util/simple-hint.css">
 
9
    <script src="../lib/util/javascript-hint.js"></script>
 
10
    <script src="../mode/javascript/javascript.js"></script>
 
11
    <link rel="stylesheet" href="../doc/docs.css">
 
12
    <style type="text/css">.CodeMirror {border: 1px solid #eee;} .CodeMirror-scroll { height: 100% }</style>
 
13
  </head>
 
14
  <body>
 
15
    <h1>CodeMirror: Autocomplete demo</h1>
 
16
 
 
17
    <form><textarea id="code" name="code">
 
18
function getCompletions(token, context) {
 
19
  var found = [], start = token.string;
 
20
  function maybeAdd(str) {
 
21
    if (str.indexOf(start) == 0) found.push(str);
 
22
  }
 
23
  function gatherCompletions(obj) {
 
24
    if (typeof obj == "string") forEach(stringProps, maybeAdd);
 
25
    else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
 
26
    else if (obj instanceof Function) forEach(funcProps, maybeAdd);
 
27
    for (var name in obj) maybeAdd(name);
 
28
  }
 
29
 
 
30
  if (context) {
 
31
    // If this is a property, see if it belongs to some object we can
 
32
    // find in the current environment.
 
33
    var obj = context.pop(), base;
 
34
    if (obj.className == "js-variable")
 
35
      base = window[obj.string];
 
36
    else if (obj.className == "js-string")
 
37
      base = "";
 
38
    else if (obj.className == "js-atom")
 
39
      base = 1;
 
40
    while (base != null && context.length)
 
41
      base = base[context.pop().string];
 
42
    if (base != null) gatherCompletions(base);
 
43
  }
 
44
  else {
 
45
    // If not, just look in the window object and any local scope
 
46
    // (reading into JS mode internals to get at the local variables)
 
47
    for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
 
48
    gatherCompletions(window);
 
49
    forEach(keywords, maybeAdd);
 
50
  }
 
51
  return found;
 
52
}
 
53
</textarea></form>
 
54
 
 
55
<p>Press <strong>ctrl-space</strong> to activate autocompletion. See
 
56
the code (<a href="../lib/util/simple-hint.js">here</a>
 
57
and <a href="../lib/util/javascript-hint.js">here</a>) to figure out
 
58
how it works.</p>
 
59
 
 
60
    <script>
 
61
      var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
 
62
        lineNumbers: true,
 
63
        extraKeys: {"Ctrl-Space": function(cm) {CodeMirror.simpleHint(cm, CodeMirror.javascriptHint);}}
 
64
      });
 
65
    </script>
 
66
  </body>
 
67
</html>