PageSourceSearch

https://learn-golang.org/static/js/codemirror/mode/go/go.js

js learn-golang.org collected 2026-09-28 07:56:57 UTC 5,314 bytes, 165 lines download raw bytes

vendor: 5,314 bytes, lines 1-165
1CodeMirror.defineMode("go", function(config) {
2  var indentUnit = config.indentUnit;
3
4  var keywords = {
5    "break":true, "case":true, "chan":true, "const":true, "continue":true,
6    "default":true, "defer":true, "else":true, "fallthrough":true, "for":true,
7    "func":true, "go":true, "goto":true, "if":true, "import":true,
8    "interface":true, "map":true, "package":true, "range":true, "return":true,
9    "select":true, "struct":true, "switch":true, "type":true, "var":true,
10    "bool":true, "byte":true, "complex64":true, "complex128":true,
11    "float32":true, "float64":true, "int8":true, "int16":true, "int32":true,
12    "int64":true, "string":true, "uint8":true, "uint16":true, "uint32":true,
13    "uint64":true, "int":true, "uint":true, "uintptr":true
14  };
15
16  var atoms = {
17    "true":true, "false":true, "iota":true, "nil":true, "append":true,
18    "cap":true, "close":true, "complex":true, "copy":true, "imag":true,
19    "len":true, "make":true, "new":true, "panic":true, "print":true,
20    "println":true, "real":true, "recover":true
21  };
22
23  var isOperatorChar = /[+\-*&^%:=<>!|\/]/;
24
25  var curPunc;
26
27  function tokenBase(stream, state) {
28    var ch = stream.next();
29    if (ch == '"' || ch == "'" || ch == "`") {
30      state.tokenize = tokenString(ch);
31      return state.tokenize(stream, state);
32    }
33    if (/[\d\.]/.test(ch)) {
34      if (ch == ".") {
35        stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/);
36      } else if (ch == "0") {
37        stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/);
38      } else {
39        stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/);
40      }
41      return "number";
42    }
43    if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
44      curPunc = ch;
45      return null;
46    }
47    if (ch == "/") {
48      if (stream.eat("*")) {
49        state.tokenize = tokenComment;
50        return tokenComment(stream, state);
51      }
52      if (stream.eat("/")) {
53        stream.skipToEnd();
54        return "comment";
55      }
56    }
57    if (isOperatorChar.test(ch)) {
58      stream.eatWhile(isOperatorChar);
59      return "operator";
60    }
61    stream.eatWhile(/[\w\$_]/);
62    var cur = stream.current();
63    if (keywords.propertyIsEnumerable(cur)) {
64      if (cur == "case" || cur == "default") curPunc = "case";
65      return "keyword";
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 || quote == "`"))
79        state.tokenize = tokenBase;
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 = tokenBase;
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    return state.context = new Context(state.indented, col, type, null, state.context);
105  }
106  function popContext(state) {
107    var t = state.context.type;
108    if (t == ")" || t == "]" || t == "}")
109      state.indented = state.context.indented;
110    return state.context = state.context.prev;
111  }
112
113  // Interface
114
115  return {
116    startState: function(basecolumn) {
117      return {
118        tokenize: null,
119        context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
120        indented: 0,
121        startOfLine: true
122      };
123    },
124
125    token: function(stream, state) {
126      var ctx = state.context;
127      if (stream.sol()) {
128        if (ctx.align == null) ctx.align = false;
129        state.indented = stream.indentation();
130        state.startOfLine = true;
131        if (ctx.type == "case") ctx.type = "}";
132      }
133      if (stream.eatSpace()) return null;
134      curPunc = null;
135      var style = (state.tokenize || tokenBase)(stream, state);
136      if (style == "comment") return style;
137      if (ctx.align == null) ctx.align = true;
138
139      if (curPunc == "{") pushContext(state, stream.column(), "}");
140      else if (curPunc == "[") pushContext(state, stream.column(), "]");
141      else if (curPunc == "(") pushContext(state, stream.column(), ")");
142      else if (curPunc == "case") ctx.type = "case";
143      else if (curPunc == "}" && ctx.type == "}") ctx = popContext(state);
144      else if (curPunc == ctx.type) popContext(state);
145      state.startOfLine = false;
146      return style;
147    },
148
149    indent: function(state, textAfter) {
150      if (state.tokenize != tokenBase && state.tokenize != null) return 0;
151      var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
152      if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) {
153        state.context.type = "}";
154        return ctx.indented;
155      }
156      var closing = firstChar == ctx.type;
157      if (ctx.align) return ctx.column + (closing ? 0 : 1);
158      else return ctx.indented + (closing ? 0 : indentUnit);
159    },
160
161    electricChars: "{}:"
162  };
163});
164
165CodeMirror.defineMIME("text/x-go", "go");

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.