1- CodeMirror . defineMode ( "javascript" , function ( config , parserConfig ) {
1+ /**
2+ * The TypeScript extensions are (C) Copyright 2012 by ComFreek <[email protected] > 3+ */
4+
5+ CodeMirror . defineMode ( "javascript" , function ( config , parserConfig ) {
26 var indentUnit = config . indentUnit ;
37 var jsonMode = parserConfig . json ;
8+ var isTS = parserConfig . typescript ;
49
510 // Tokenizer
611
712 var keywords = function ( ) {
813 function kw ( type ) { return { type : type , style : "keyword" } ; }
914 var A = kw ( "keyword a" ) , B = kw ( "keyword b" ) , C = kw ( "keyword c" ) ;
1015 var operator = kw ( "operator" ) , atom = { type : "atom" , style : "atom" } ;
11- return {
16+
17+ var jsKeywords = {
1218 "if" : A , "while" : A , "with" : A , "else" : B , "do" : B , "try" : B , "finally" : B ,
1319 "return" : C , "break" : C , "continue" : C , "new" : C , "delete" : C , "throw" : C ,
1420 "var" : kw ( "var" ) , "const" : kw ( "var" ) , "let" : kw ( "var" ) ,
@@ -17,6 +23,34 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
1723 "in" : operator , "typeof" : operator , "instanceof" : operator ,
1824 "true" : atom , "false" : atom , "null" : atom , "undefined" : atom , "NaN" : atom , "Infinity" : atom
1925 } ;
26+
27+ // Extend the 'normal' keywords with the TypeScript language extensions
28+ if ( isTS ) {
29+ var tsKeywords = {
30+ // object-like things
31+ "interface" : kw ( "interface" ) ,
32+ "class" : kw ( "class" ) ,
33+ "extends" : kw ( "extends" ) ,
34+ "constructor" : kw ( "constructor" ) ,
35+
36+ // scope modifiers
37+ "public" : kw ( "public" ) ,
38+ "private" : kw ( "private" ) ,
39+ "protected" : kw ( "protected" ) ,
40+ "static" : kw ( "static" ) ,
41+
42+ "super" : kw ( "super" ) ,
43+
44+ // types
45+ "string" : type , "number" : type , "bool" : type , "any" : type
46+ } ;
47+
48+ for ( var attr in tsKeywords ) {
49+ jsKeywords [ attr ] = tsKeywords [ attr ] ;
50+ }
51+ }
52+
53+ return jsKeywords ;
2054 } ( ) ;
2155
2256 var isOperatorChar = / [ + \- * & % = < > ! ? | ] / ;
@@ -360,3 +394,5 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
360394
361395CodeMirror . defineMIME ( "text/javascript" , "javascript" ) ;
362396CodeMirror . defineMIME ( "application/json" , { name : "javascript" , json : true } ) ;
397+ CodeMirror . defineMIME ( "text/typescript" , { name : "javascript" , typescript : true } ) ;
398+ CodeMirror . defineMIME ( "application/typescript" , { name : "javascript" , typescript : true } ) ;
0 commit comments