source: branches/2.2/plugins/LocalFilesEditor/codemirror/mode/clike/clike.js @ 10310

Last change on this file since 10310 was 10310, checked in by patdenice, 13 years ago

merge r10307 from trunk to branch 2.2
feature:2262
Replace editarea by Codemirror:
http://codemirror.net

File size: 6.0 KB
Line 
1CodeMirror.defineMode("clike", function(config, parserConfig) {
2  var indentUnit = config.indentUnit, keywords = parserConfig.keywords,
3      cpp = parserConfig.useCPP, multiLineStrings = parserConfig.multiLineStrings, $vars = parserConfig.$vars;
4  var isOperatorChar = /[+\-*&%=<>!?|]/;
5
6  function chain(stream, state, f) {
7    state.tokenize = f;
8    return f(stream, state);
9  }
10
11  var type;
12  function ret(tp, style) {
13    type = tp;
14    return style;
15  }
16
17  function tokenBase(stream, state) {
18    var ch = stream.next();
19    if (ch == '"' || ch == "'")
20      return chain(stream, state, tokenString(ch));
21    else if (/[\[\]{}\(\),;\:\.]/.test(ch))
22      return ret(ch);
23    else if (ch == "#" && cpp && state.startOfLine) {
24      stream.skipToEnd();
25      return ret("directive", "c-like-preprocessor");
26    }
27    else if (/\d/.test(ch)) {
28      stream.eatWhile(/[\w\.]/)
29      return ret("number", "c-like-number");
30    }
31    else if (ch == "/") {
32      if (stream.eat("*")) {
33        return chain(stream, state, tokenComment);
34      }
35      else if (stream.eat("/")) {
36        stream.skipToEnd();
37        return ret("comment", "c-like-comment");
38      }
39      else {
40        stream.eatWhile(isOperatorChar);
41        return ret("operator");
42      }
43    }
44    else if (isOperatorChar.test(ch)) {
45      stream.eatWhile(isOperatorChar);
46      return ret("operator");
47    }
48    else if ($vars && ch == "$") {
49      stream.eatWhile(/[\w\$_]/);
50      return ret("word", "c-like-var");
51    }
52    else {
53      stream.eatWhile(/[\w\$_]/);
54      if (keywords && keywords.propertyIsEnumerable(stream.current())) return ret("keyword", "c-like-keyword");
55      return ret("word", "c-like-word");
56    }
57  }
58
59  function tokenString(quote) {
60    return function(stream, state) {
61      var escaped = false, next, end = false;
62      while ((next = stream.next()) != null) {
63        if (next == quote && !escaped) {end = true; break;}
64        escaped = !escaped && next == "\\";
65      }
66      if (end || !(escaped || multiLineStrings))
67        state.tokenize = tokenBase;
68      return ret("string", "c-like-string");
69    };
70  }
71
72  function tokenComment(stream, state) {
73    var maybeEnd = false, ch;
74    while (ch = stream.next()) {
75      if (ch == "/" && maybeEnd) {
76        state.tokenize = tokenBase;
77        break;
78      }
79      maybeEnd = (ch == "*");
80    }
81    return ret("comment", "c-like-comment");
82  }
83
84  function Context(indented, column, type, align, prev) {
85    this.indented = indented;
86    this.column = column;
87    this.type = type;
88    this.align = align;
89    this.prev = prev;
90  }
91
92  function pushContext(state, col, type) {
93    return state.context = new Context(state.indented, col, type, null, state.context);
94  }
95  function popContext(state) {
96    return state.context = state.context.prev;
97  }
98
99  // Interface
100
101  return {
102    startState: function(basecolumn) {
103      return {
104        tokenize: tokenBase,
105        context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
106        indented: 0,
107        startOfLine: true
108      };
109    },
110
111    token: function(stream, state) {
112      var ctx = state.context;
113      if (stream.sol()) {
114        if (ctx.align == null) ctx.align = false;
115        state.indented = stream.indentation();
116        state.startOfLine = true;
117      }
118      if (stream.eatSpace()) return null;
119      var style = state.tokenize(stream, state);
120      if (type == "comment") return style;
121      if (ctx.align == null) ctx.align = true;
122
123      if ((type == ";" || type == ":") && ctx.type == "statement") popContext(state);
124      else if (type == "{") pushContext(state, stream.column(), "}");
125      else if (type == "[") pushContext(state, stream.column(), "]");
126      else if (type == "(") pushContext(state, stream.column(), ")");
127      else if (type == "}") {
128        if (ctx.type == "statement") ctx = popContext(state);
129        if (ctx.type == "}") ctx = popContext(state);
130        if (ctx.type == "statement") ctx = popContext(state);
131      }
132      else if (type == ctx.type) popContext(state);
133      else if (ctx.type == "}") pushContext(state, stream.column(), "statement");
134      state.startOfLine = false;
135      return style;
136    },
137
138    indent: function(state, textAfter) {
139      if (state.tokenize != tokenBase) return 0;
140      var firstChar = textAfter && textAfter.charAt(0), ctx = state.context, closing = firstChar == ctx.type;
141      if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : indentUnit);
142      else if (ctx.align) return ctx.column + (closing ? 0 : 1);
143      else return ctx.indented + (closing ? 0 : indentUnit);
144    },
145
146    electricChars: "{}"
147  };
148});
149
150(function() {
151  function keywords(str) {
152    var obj = {}, words = str.split(" ");
153    for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
154    return obj;
155  }
156  var cKeywords = "auto if break int case long char register continue return default short do sizeof " +
157    "double static else struct entry switch extern typedef float union for unsigned " +
158    "goto while enum void const signed volatile";
159
160  CodeMirror.defineMIME("text/x-csrc", {
161    name: "clike",
162    useCPP: true,
163    keywords: keywords(cKeywords)
164  });
165  CodeMirror.defineMIME("text/x-c++src", {
166    name: "clike",
167    useCPP: true,
168    keywords: keywords(cKeywords + " asm dynamic_cast namespace reinterpret_cast try bool explicit new " +
169                       "static_cast typeid catch false operator template typename class friend private " +
170                       "this using const_cast inline public throw virtual delete mutable protected true " +
171                       "wchar_t")
172  });
173  CodeMirror.defineMIME("text/x-java", {
174    name: "clike",
175    keywords: keywords("abstract assert boolean break byte case catch char class const continue default " + 
176                       "do double else enum extends false final finally float for goto if implements import " +
177                       "instanceof int interface long native new null package private protected public " +
178                       "return short static strictfp super switch synchronized this throw throws transient " +
179                       "true try void volatile while")
180  });
181}());
Note: See TracBrowser for help on using the repository browser.