Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: experimental/webtry/res/js/cm/clike.js

Issue 261693003: Use CodeMirror for WebTry snippets. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: design.md notes Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « experimental/webtry/res/css/webtry.css ('k') | experimental/webtry/res/js/cm/codemirror.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 (function(mod) {
2 if (typeof exports == "object" && typeof module == "object") // CommonJS
3 mod(require("../../lib/codemirror"));
4 else if (typeof define == "function" && define.amd) // AMD
5 define(["../../lib/codemirror"], mod);
6 else // Plain browser env
7 mod(CodeMirror);
8 })(function(CodeMirror) {
9 "use strict";
10
11 CodeMirror.defineMode("clike", function(config, parserConfig) {
12 var indentUnit = config.indentUnit,
13 statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
14 dontAlignCalls = parserConfig.dontAlignCalls,
15 keywords = parserConfig.keywords || {},
16 builtin = parserConfig.builtin || {},
17 blockKeywords = parserConfig.blockKeywords || {},
18 atoms = parserConfig.atoms || {},
19 hooks = parserConfig.hooks || {},
20 multiLineStrings = parserConfig.multiLineStrings;
21 var isOperatorChar = /[+\-*&%=<>!?|\/]/;
22
23 var curPunc;
24
25 function tokenBase(stream, state) {
26 var ch = stream.next();
27 if (hooks[ch]) {
28 var result = hooks[ch](stream, state);
29 if (result !== false) return result;
30 }
31 if (ch == '"' || ch == "'") {
32 state.tokenize = tokenString(ch);
33 return state.tokenize(stream, state);
34 }
35 if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
36 curPunc = ch;
37 return null;
38 }
39 if (/\d/.test(ch)) {
40 stream.eatWhile(/[\w\.]/);
41 return "number";
42 }
43 if (ch == "/") {
44 if (stream.eat("*")) {
45 state.tokenize = tokenComment;
46 return tokenComment(stream, state);
47 }
48 if (stream.eat("/")) {
49 stream.skipToEnd();
50 return "comment";
51 }
52 }
53 if (isOperatorChar.test(ch)) {
54 stream.eatWhile(isOperatorChar);
55 return "operator";
56 }
57 stream.eatWhile(/[\w\$_]/);
58 var cur = stream.current();
59 if (keywords.propertyIsEnumerable(cur)) {
60 if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
61 return "keyword";
62 }
63 if (builtin.propertyIsEnumerable(cur)) {
64 if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
65 return "builtin";
66 }
67 if (atoms.propertyIsEnumerable(cur)) return "atom";
68 return "variable";
69 }
70
71 function tokenString(quote) {
72 return function(stream, state) {
73 var escaped = false, next, end = false;
74 while ((next = stream.next()) != null) {
75 if (next == quote && !escaped) {end = true; break;}
76 escaped = !escaped && next == "\\";
77 }
78 if (end || !(escaped || multiLineStrings))
79 state.tokenize = null;
80 return "string";
81 };
82 }
83
84 function tokenComment(stream, state) {
85 var maybeEnd = false, ch;
86 while (ch = stream.next()) {
87 if (ch == "/" && maybeEnd) {
88 state.tokenize = null;
89 break;
90 }
91 maybeEnd = (ch == "*");
92 }
93 return "comment";
94 }
95
96 function Context(indented, column, type, align, prev) {
97 this.indented = indented;
98 this.column = column;
99 this.type = type;
100 this.align = align;
101 this.prev = prev;
102 }
103 function pushContext(state, col, type) {
104 var indent = state.indented;
105 if (state.context && state.context.type == "statement")
106 indent = state.context.indented;
107 return state.context = new Context(indent, col, type, null, state.context);
108 }
109 function popContext(state) {
110 var t = state.context.type;
111 if (t == ")" || t == "]" || t == "}")
112 state.indented = state.context.indented;
113 return state.context = state.context.prev;
114 }
115
116 // Interface
117
118 return {
119 startState: function(basecolumn) {
120 return {
121 tokenize: null,
122 context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
123 indented: 0,
124 startOfLine: true
125 };
126 },
127
128 token: function(stream, state) {
129 var ctx = state.context;
130 if (stream.sol()) {
131 if (ctx.align == null) ctx.align = false;
132 state.indented = stream.indentation();
133 state.startOfLine = true;
134 }
135 if (stream.eatSpace()) return null;
136 curPunc = null;
137 var style = (state.tokenize || tokenBase)(stream, state);
138 if (style == "comment" || style == "meta") return style;
139 if (ctx.align == null) ctx.align = true;
140
141 if ((curPunc == ";" || curPunc == ":" || curPunc == ",") && ctx.type == "s tatement") popContext(state);
142 else if (curPunc == "{") pushContext(state, stream.column(), "}");
143 else if (curPunc == "[") pushContext(state, stream.column(), "]");
144 else if (curPunc == "(") pushContext(state, stream.column(), ")");
145 else if (curPunc == "}") {
146 while (ctx.type == "statement") ctx = popContext(state);
147 if (ctx.type == "}") ctx = popContext(state);
148 while (ctx.type == "statement") ctx = popContext(state);
149 }
150 else if (curPunc == ctx.type) popContext(state);
151 else if (((ctx.type == "}" || ctx.type == "top") && curPunc != ';') || (ct x.type == "statement" && curPunc == "newstatement"))
152 pushContext(state, stream.column(), "statement");
153 state.startOfLine = false;
154 return style;
155 },
156
157 indent: function(state, textAfter) {
158 if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirr or.Pass;
159 var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
160 if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
161 var closing = firstChar == ctx.type;
162 if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
163 else if (ctx.align && (!dontAlignCalls || ctx.type != ")")) return ctx.col umn + (closing ? 0 : 1);
164 else if (ctx.type == ")" && !closing) return ctx.indented + statementInden tUnit;
165 else return ctx.indented + (closing ? 0 : indentUnit);
166 },
167
168 electricChars: "{}",
169 blockCommentStart: "/*",
170 blockCommentEnd: "*/",
171 lineComment: "//",
172 fold: "brace"
173 };
174 });
175
176 function words(str) {
177 var obj = {}, words = str.split(" ");
178 for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
179 return obj;
180 }
181 var cKeywords = "auto if break int case long char register continue return def ault short do sizeof " +
182 "double static else struct entry switch extern typedef float union for unsig ned " +
183 "goto while enum void const signed volatile";
184
185 function cppHook(stream, state) {
186 if (!state.startOfLine) return false;
187 for (;;) {
188 if (stream.skipTo("\\")) {
189 stream.next();
190 if (stream.eol()) {
191 state.tokenize = cppHook;
192 break;
193 }
194 } else {
195 stream.skipToEnd();
196 state.tokenize = null;
197 break;
198 }
199 }
200 return "meta";
201 }
202
203 function cpp11StringHook(stream, state) {
204 stream.backUp(1);
205 // Raw strings.
206 if (stream.match(/(R|u8R|uR|UR|LR)/)) {
207 var match = stream.match(/"(.{0,16})\(/);
208 if (!match) {
209 return false;
210 }
211 state.cpp11RawStringDelim = match[1];
212 state.tokenize = tokenRawString;
213 return tokenRawString(stream, state);
214 }
215 // Unicode strings/chars.
216 if (stream.match(/(u8|u|U|L)/)) {
217 if (stream.match(/["']/, /* eat */ false)) {
218 return "string";
219 }
220 return false;
221 }
222 // Ignore this hook.
223 stream.next();
224 return false;
225 }
226
227 // C#-style strings where "" escapes a quote.
228 function tokenAtString(stream, state) {
229 var next;
230 while ((next = stream.next()) != null) {
231 if (next == '"' && !stream.eat('"')) {
232 state.tokenize = null;
233 break;
234 }
235 }
236 return "string";
237 }
238
239 // C++11 raw string literal is <prefix>"<delim>( anything )<delim>", where
240 // <delim> can be a string up to 16 characters long.
241 function tokenRawString(stream, state) {
242 var closingSequence = new RegExp(".*?\\)" + state.cpp11RawStringDelim + '"') ;
243 var match = stream.match(closingSequence);
244 if (match) {
245 state.tokenize = null;
246 } else {
247 stream.skipToEnd();
248 }
249 return "string";
250 }
251
252 function def(mimes, mode) {
253 if (typeof mimes == "string") mimes = [mimes];
254 var words = [];
255 function add(obj) {
256 if (obj) for (var prop in obj) if (obj.hasOwnProperty(prop))
257 words.push(prop);
258 }
259 add(mode.keywords);
260 add(mode.builtin);
261 add(mode.atoms);
262 if (words.length) {
263 mode.helperType = mimes[0];
264 CodeMirror.registerHelper("hintWords", mimes[0], words);
265 }
266
267 for (var i = 0; i < mimes.length; ++i)
268 CodeMirror.defineMIME(mimes[i], mode);
269 }
270
271 def(["text/x-csrc", "text/x-c", "text/x-chdr"], {
272 name: "clike",
273 keywords: words(cKeywords),
274 blockKeywords: words("case do else for if switch while struct"),
275 atoms: words("null"),
276 hooks: {"#": cppHook},
277 modeProps: {fold: ["brace", "include"]}
278 });
279
280 def(["text/x-c++src", "text/x-c++hdr"], {
281 name: "clike",
282 keywords: words(cKeywords + " asm dynamic_cast namespace reinterpret_cast tr y bool explicit new " +
283 "static_cast typeid catch operator template typename class f riend private " +
284 "this using const_cast inline public throw virtual delete mu table protected " +
285 "wchar_t alignas alignof constexpr decltype nullptr noexcept thread_local final " +
286 "static_assert override"),
287 blockKeywords: words("catch class do else finally for if struct switch try w hile"),
288 atoms: words("true false null"),
289 hooks: {
290 "#": cppHook,
291 "u": cpp11StringHook,
292 "U": cpp11StringHook,
293 "L": cpp11StringHook,
294 "R": cpp11StringHook
295 },
296 modeProps: {fold: ["brace", "include"]}
297 });
298 def("text/x-java", {
299 name: "clike",
300 keywords: words("abstract assert boolean break byte case catch char class co nst continue default " +
301 "do double else enum extends final finally float for goto if implements import " +
302 "instanceof int interface long native new package private pr otected public " +
303 "return short static strictfp super switch synchronized this throw throws transient " +
304 "try void volatile while"),
305 blockKeywords: words("catch class do else finally for if switch try while"),
306 atoms: words("true false null"),
307 hooks: {
308 "@": function(stream) {
309 stream.eatWhile(/[\w\$_]/);
310 return "meta";
311 }
312 },
313 modeProps: {fold: ["brace", "import"]}
314 });
315 def("text/x-csharp", {
316 name: "clike",
317 keywords: words("abstract as base break case catch checked class const conti nue" +
318 " default delegate do else enum event explicit extern finall y fixed for" +
319 " foreach goto if implicit in interface internal is lock nam espace new" +
320 " operator out override params private protected public read only ref return sealed" +
321 " sizeof stackalloc static struct switch this throw try type of unchecked" +
322 " unsafe using virtual void volatile while add alias ascendi ng descending dynamic from get" +
323 " global group into join let orderby partial remove select s et value var yield"),
324 blockKeywords: words("catch class do else finally for foreach if struct swit ch try while"),
325 builtin: words("Boolean Byte Char DateTime DateTimeOffset Decimal Double" +
326 " Guid Int16 Int32 Int64 Object SByte Single String TimeSpan UInt16 UInt32" +
327 " UInt64 bool byte char decimal double short int long object " +
328 " sbyte float string ushort uint ulong"),
329 atoms: words("true false null"),
330 hooks: {
331 "@": function(stream, state) {
332 if (stream.eat('"')) {
333 state.tokenize = tokenAtString;
334 return tokenAtString(stream, state);
335 }
336 stream.eatWhile(/[\w\$_]/);
337 return "meta";
338 }
339 }
340 });
341 def("text/x-scala", {
342 name: "clike",
343 keywords: words(
344
345 /* scala */
346 "abstract case catch class def do else extends false final finally for for Some if " +
347 "implicit import lazy match new null object override package private prote cted return " +
348 "sealed super this throw trait try trye type val var while with yield _ : = => <- <: " +
349 "<% >: # @ " +
350
351 /* package scala */
352 "assert assume require print println printf readLine readBoolean readByte readShort " +
353 "readChar readInt readLong readFloat readDouble " +
354
355 "AnyVal App Application Array BufferedIterator BigDecimal BigInt Char Cons ole Either " +
356 "Enumeration Equiv Error Exception Fractional Function IndexedSeq Integral Iterable " +
357 "Iterator List Map Numeric Nil NotNull Option Ordered Ordering PartialFunc tion PartialOrdering " +
358 "Product Proxy Range Responder Seq Serializable Set Specializable Stream S tringBuilder " +
359 "StringContext Symbol Throwable Traversable TraversableOnce Tuple Unit Vec tor :: #:: " +
360
361 /* package java.lang */
362 "Boolean Byte Character CharSequence Class ClassLoader Cloneable Comparabl e " +
363 "Compiler Double Exception Float Integer Long Math Number Object Package P air Process " +
364 "Runtime Runnable SecurityManager Short StackTraceElement StrictMath Strin g " +
365 "StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void"
366
367
368 ),
369 blockKeywords: words("catch class do else finally for forSome if match switc h try while"),
370 atoms: words("true false null"),
371 hooks: {
372 "@": function(stream) {
373 stream.eatWhile(/[\w\$_]/);
374 return "meta";
375 }
376 }
377 });
378 def(["x-shader/x-vertex", "x-shader/x-fragment"], {
379 name: "clike",
380 keywords: words("float int bool void " +
381 "vec2 vec3 vec4 ivec2 ivec3 ivec4 bvec2 bvec3 bvec4 " +
382 "mat2 mat3 mat4 " +
383 "sampler1D sampler2D sampler3D samplerCube " +
384 "sampler1DShadow sampler2DShadow" +
385 "const attribute uniform varying " +
386 "break continue discard return " +
387 "for while do if else struct " +
388 "in out inout"),
389 blockKeywords: words("for while do if else struct"),
390 builtin: words("radians degrees sin cos tan asin acos atan " +
391 "pow exp log exp2 sqrt inversesqrt " +
392 "abs sign floor ceil fract mod min max clamp mix step smoots tep " +
393 "length distance dot cross normalize ftransform faceforward " +
394 "reflect refract matrixCompMult " +
395 "lessThan lessThanEqual greaterThan greaterThanEqual " +
396 "equal notEqual any all not " +
397 "texture1D texture1DProj texture1DLod texture1DProjLod " +
398 "texture2D texture2DProj texture2DLod texture2DProjLod " +
399 "texture3D texture3DProj texture3DLod texture3DProjLod " +
400 "textureCube textureCubeLod " +
401 "shadow1D shadow2D shadow1DProj shadow2DProj " +
402 "shadow1DLod shadow2DLod shadow1DProjLod shadow2DProjLod " +
403 "dFdx dFdy fwidth " +
404 "noise1 noise2 noise3 noise4"),
405 atoms: words("true false " +
406 "gl_FragColor gl_SecondaryColor gl_Normal gl_Vertex " +
407 "gl_MultiTexCoord0 gl_MultiTexCoord1 gl_MultiTexCoord2 gl_MultiT exCoord3 " +
408 "gl_MultiTexCoord4 gl_MultiTexCoord5 gl_MultiTexCoord6 gl_MultiT exCoord7 " +
409 "gl_FogCoord " +
410 "gl_Position gl_PointSize gl_ClipVertex " +
411 "gl_FrontColor gl_BackColor gl_FrontSecondaryColor gl_BackSecond aryColor " +
412 "gl_TexCoord gl_FogFragCoord " +
413 "gl_FragCoord gl_FrontFacing " +
414 "gl_FragColor gl_FragData gl_FragDepth " +
415 "gl_ModelViewMatrix gl_ProjectionMatrix gl_ModelViewProjectionMa trix " +
416 "gl_TextureMatrix gl_NormalMatrix gl_ModelViewMatrixInverse " +
417 "gl_ProjectionMatrixInverse gl_ModelViewProjectionMatrixInverse " +
418 "gl_TexureMatrixTranspose gl_ModelViewMatrixInverseTranspose " +
419 "gl_ProjectionMatrixInverseTranspose " +
420 "gl_ModelViewProjectionMatrixInverseTranspose " +
421 "gl_TextureMatrixInverseTranspose " +
422 "gl_NormalScale gl_DepthRange gl_ClipPlane " +
423 "gl_Point gl_FrontMaterial gl_BackMaterial gl_LightSource gl_Lig htModel " +
424 "gl_FrontLightModelProduct gl_BackLightModelProduct " +
425 "gl_TextureColor gl_EyePlaneS gl_EyePlaneT gl_EyePlaneR gl_EyePl aneQ " +
426 "gl_FogParameters " +
427 "gl_MaxLights gl_MaxClipPlanes gl_MaxTextureUnits gl_MaxTextureC oords " +
428 "gl_MaxVertexAttribs gl_MaxVertexUniformComponents gl_MaxVarying Floats " +
429 "gl_MaxVertexTextureImageUnits gl_MaxTextureImageUnits " +
430 "gl_MaxFragmentUniformComponents gl_MaxCombineTextureImageUnits " +
431 "gl_MaxDrawBuffers"),
432 hooks: {"#": cppHook},
433 modeProps: {fold: ["brace", "include"]}
434 });
435
436 });
OLDNEW
« no previous file with comments | « experimental/webtry/res/css/webtry.css ('k') | experimental/webtry/res/js/cm/codemirror.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698