LaravelTest
187 строк · 5.9 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14CodeMirror.defineMode("go", function(config) {15var indentUnit = config.indentUnit;16
17var 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, "error": true,27"rune":true28};29
30var atoms = {31"true":true, "false":true, "iota":true, "nil":true, "append":true,32"cap":true, "close":true, "complex":true, "copy":true, "delete":true, "imag":true,33"len":true, "make":true, "new":true, "panic":true, "print":true,34"println":true, "real":true, "recover":true35};36
37var isOperatorChar = /[+\-*&^%:=<>!|\/]/;38
39var curPunc;40
41function tokenBase(stream, state) {42var ch = stream.next();43if (ch == '"' || ch == "'" || ch == "`") {44state.tokenize = tokenString(ch);45return state.tokenize(stream, state);46}47if (/[\d\.]/.test(ch)) {48if (ch == ".") {49stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/);50} else if (ch == "0") {51stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/);52} else {53stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/);54}55return "number";56}57if (/[\[\]{}\(\),;\:\.]/.test(ch)) {58curPunc = ch;59return null;60}61if (ch == "/") {62if (stream.eat("*")) {63state.tokenize = tokenComment;64return tokenComment(stream, state);65}66if (stream.eat("/")) {67stream.skipToEnd();68return "comment";69}70}71if (isOperatorChar.test(ch)) {72stream.eatWhile(isOperatorChar);73return "operator";74}75stream.eatWhile(/[\w\$_\xa1-\uffff]/);76var cur = stream.current();77if (keywords.propertyIsEnumerable(cur)) {78if (cur == "case" || cur == "default") curPunc = "case";79return "keyword";80}81if (atoms.propertyIsEnumerable(cur)) return "atom";82return "variable";83}84
85function tokenString(quote) {86return function(stream, state) {87var escaped = false, next, end = false;88while ((next = stream.next()) != null) {89if (next == quote && !escaped) {end = true; break;}90escaped = !escaped && quote != "`" && next == "\\";91}92if (end || !(escaped || quote == "`"))93state.tokenize = tokenBase;94return "string";95};96}97
98function tokenComment(stream, state) {99var maybeEnd = false, ch;100while (ch = stream.next()) {101if (ch == "/" && maybeEnd) {102state.tokenize = tokenBase;103break;104}105maybeEnd = (ch == "*");106}107return "comment";108}109
110function Context(indented, column, type, align, prev) {111this.indented = indented;112this.column = column;113this.type = type;114this.align = align;115this.prev = prev;116}117function pushContext(state, col, type) {118return state.context = new Context(state.indented, col, type, null, state.context);119}120function popContext(state) {121if (!state.context.prev) return;122var t = state.context.type;123if (t == ")" || t == "]" || t == "}")124state.indented = state.context.indented;125return state.context = state.context.prev;126}127
128// Interface129
130return {131startState: function(basecolumn) {132return {133tokenize: null,134context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),135indented: 0,136startOfLine: true137};138},139
140token: function(stream, state) {141var ctx = state.context;142if (stream.sol()) {143if (ctx.align == null) ctx.align = false;144state.indented = stream.indentation();145state.startOfLine = true;146if (ctx.type == "case") ctx.type = "}";147}148if (stream.eatSpace()) return null;149curPunc = null;150var style = (state.tokenize || tokenBase)(stream, state);151if (style == "comment") return style;152if (ctx.align == null) ctx.align = true;153
154if (curPunc == "{") pushContext(state, stream.column(), "}");155else if (curPunc == "[") pushContext(state, stream.column(), "]");156else if (curPunc == "(") pushContext(state, stream.column(), ")");157else if (curPunc == "case") ctx.type = "case";158else if (curPunc == "}" && ctx.type == "}") popContext(state);159else if (curPunc == ctx.type) popContext(state);160state.startOfLine = false;161return style;162},163
164indent: function(state, textAfter) {165if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;166var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);167if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) {168state.context.type = "}";169return ctx.indented;170}171var closing = firstChar == ctx.type;172if (ctx.align) return ctx.column + (closing ? 0 : 1);173else return ctx.indented + (closing ? 0 : indentUnit);174},175
176electricChars: "{}):",177closeBrackets: "()[]{}''\"\"``",178fold: "brace",179blockCommentStart: "/*",180blockCommentEnd: "*/",181lineComment: "//"182};183});184
185CodeMirror.defineMIME("text/x-go", "go");186
187});188