github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/public/codemirror/mode/go/go.js (about) 1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 // Distributed under an MIT license: http://codemirror.net/LICENSE 3 4 (function(mod) { 5 if (typeof exports == "object" && typeof module == "object") // CommonJS 6 mod(require("../../lib/codemirror")); 7 else if (typeof define == "function" && define.amd) // AMD 8 define(["../../lib/codemirror"], mod); 9 else // Plain browser env 10 mod(CodeMirror); 11 })(function(CodeMirror) { 12 "use strict"; 13 14 CodeMirror.defineMode("go", function(config) { 15 var indentUnit = config.indentUnit; 16 17 var keywords = { 18 "break":true, "case":true, "chan":true, "const":true, "continue":true, 19 "default":true, "defer":true, "else":true, "fallthrough":true, "for":true, 20 "func":true, "go":true, "goto":true, "if":true, "import":true, 21 "interface":true, "map":true, "package":true, "range":true, "return":true, 22 "select":true, "struct":true, "switch":true, "type":true, "var":true, 23 "bool":true, "byte":true, "complex64":true, "complex128":true, 24 "float32":true, "float64":true, "int8":true, "int16":true, "int32":true, 25 "int64":true, "string":true, "uint8":true, "uint16":true, "uint32":true, 26 "uint64":true, "int":true, "uint":true, "uintptr":true 27 }; 28 29 var atoms = { 30 "true":true, "false":true, "iota":true, "nil":true, "append":true, 31 "cap":true, "close":true, "complex":true, "copy":true, "imag":true, 32 "len":true, "make":true, "new":true, "panic":true, "print":true, 33 "println":true, "real":true, "recover":true 34 }; 35 36 var isOperatorChar = /[+\-*&^%:=<>!|\/]/; 37 38 var curPunc; 39 40 function tokenBase(stream, state) { 41 var ch = stream.next(); 42 if (ch == '"' || ch == "'" || ch == "`") { 43 state.tokenize = tokenString(ch); 44 return state.tokenize(stream, state); 45 } 46 if (/[\d\.]/.test(ch)) { 47 if (ch == ".") { 48 stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/); 49 } else if (ch == "0") { 50 stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/); 51 } else { 52 stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/); 53 } 54 return "number"; 55 } 56 if (/[\[\]{}\(\),;\:\.]/.test(ch)) { 57 curPunc = ch; 58 return null; 59 } 60 if (ch == "/") { 61 if (stream.eat("*")) { 62 state.tokenize = tokenComment; 63 return tokenComment(stream, state); 64 } 65 if (stream.eat("/")) { 66 stream.skipToEnd(); 67 return "comment"; 68 } 69 } 70 if (isOperatorChar.test(ch)) { 71 stream.eatWhile(isOperatorChar); 72 return "operator"; 73 } 74 stream.eatWhile(/[\w\$_\xa1-\uffff]/); 75 var cur = stream.current(); 76 if (keywords.propertyIsEnumerable(cur)) { 77 if (cur == "case" || cur == "default") curPunc = "case"; 78 return "keyword"; 79 } 80 if (atoms.propertyIsEnumerable(cur)) return "atom"; 81 return "variable"; 82 } 83 84 function tokenString(quote) { 85 return function(stream, state) { 86 var escaped = false, next, end = false; 87 while ((next = stream.next()) != null) { 88 if (next == quote && !escaped) {end = true; break;} 89 escaped = !escaped && quote != "`" && next == "\\"; 90 } 91 if (end || !(escaped || quote == "`")) 92 state.tokenize = tokenBase; 93 return "string"; 94 }; 95 } 96 97 function tokenComment(stream, state) { 98 var maybeEnd = false, ch; 99 while (ch = stream.next()) { 100 if (ch == "/" && maybeEnd) { 101 state.tokenize = tokenBase; 102 break; 103 } 104 maybeEnd = (ch == "*"); 105 } 106 return "comment"; 107 } 108 109 function Context(indented, column, type, align, prev) { 110 this.indented = indented; 111 this.column = column; 112 this.type = type; 113 this.align = align; 114 this.prev = prev; 115 } 116 function pushContext(state, col, type) { 117 return state.context = new Context(state.indented, col, type, null, state.context); 118 } 119 function popContext(state) { 120 if (!state.context.prev) return; 121 var t = state.context.type; 122 if (t == ")" || t == "]" || t == "}") 123 state.indented = state.context.indented; 124 return state.context = state.context.prev; 125 } 126 127 // Interface 128 129 return { 130 startState: function(basecolumn) { 131 return { 132 tokenize: null, 133 context: new Context((basecolumn || 0) - indentUnit, 0, "top", false), 134 indented: 0, 135 startOfLine: true 136 }; 137 }, 138 139 token: function(stream, state) { 140 var ctx = state.context; 141 if (stream.sol()) { 142 if (ctx.align == null) ctx.align = false; 143 state.indented = stream.indentation(); 144 state.startOfLine = true; 145 if (ctx.type == "case") ctx.type = "}"; 146 } 147 if (stream.eatSpace()) return null; 148 curPunc = null; 149 var style = (state.tokenize || tokenBase)(stream, state); 150 if (style == "comment") return style; 151 if (ctx.align == null) ctx.align = true; 152 153 if (curPunc == "{") pushContext(state, stream.column(), "}"); 154 else if (curPunc == "[") pushContext(state, stream.column(), "]"); 155 else if (curPunc == "(") pushContext(state, stream.column(), ")"); 156 else if (curPunc == "case") ctx.type = "case"; 157 else if (curPunc == "}" && ctx.type == "}") ctx = popContext(state); 158 else if (curPunc == ctx.type) popContext(state); 159 state.startOfLine = false; 160 return style; 161 }, 162 163 indent: function(state, textAfter) { 164 if (state.tokenize != tokenBase && state.tokenize != null) return 0; 165 var ctx = state.context, firstChar = textAfter && textAfter.charAt(0); 166 if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) { 167 state.context.type = "}"; 168 return ctx.indented; 169 } 170 var closing = firstChar == ctx.type; 171 if (ctx.align) return ctx.column + (closing ? 0 : 1); 172 else return ctx.indented + (closing ? 0 : indentUnit); 173 }, 174 175 electricChars: "{}):", 176 fold: "brace", 177 blockCommentStart: "/*", 178 blockCommentEnd: "*/", 179 lineComment: "//" 180 }; 181 }); 182 183 CodeMirror.defineMIME("text/x-go", "go"); 184 185 });