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