Skip to content

Commit 2787836

Browse files
finalfantasiamarijnh
authored andcommitted
[clojure mode] Get rid of a shared, mutable variable tokenType,
1 parent a38047a commit 2787836

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

mode/clojure/clojure.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,28 @@ CodeMirror.defineMode("clojure", function (options) {
157157
var numberLiteral = /^[+\-]?\d+(?:(?:N|(?:[eE][+\-]?\d+))|(?:\.?\d*(?:M|(?:[eE][+\-]?\d+))?)|\/\d+|[xX][0-9a-fA-F]+|r[0-9a-zA-Z]+)?/;
158158
var symbol = /[!#'*+\-.\/:<>?_\w\xa1-\uffff]/;
159159

160-
var tokenType;
161-
162160
function base(stream, state) {
163-
if (stream.eatSpace()) {tokenType = "space"; return null;}
164-
if (stream.match(numberLiteral)) return "number";
161+
if (stream.eatSpace()) return ["space", null];
162+
if (stream.match(numberLiteral)) return [null, "number"];
165163

166164
var ch = stream.next();
167165

168-
if (ch === "\\") {stream.next(); readSymbol(stream); return "string-2";}
166+
if (ch === "\\") {stream.next(); readSymbol(stream); return [null, "string-2"];}
169167
if (ch === '"') return (state.tokenize = inString)(stream, state);
170-
if (is(ch, /[(\[{]/)) {tokenType = "open"; return "bracket";}
171-
if (is(ch, /[)\]}]/)) {tokenType = "close"; return "bracket";}
172-
if (ch === ";") {stream.skipToEnd(); tokenType = "space"; return "comment";}
173-
if (is(ch, /[#'@^`~]/)) return "meta";
168+
if (is(ch, /[(\[{]/)) return ["open", "bracket"];
169+
if (is(ch, /[)\]}]/)) return ["close", "bracket"];
170+
if (ch === ";") {stream.skipToEnd(); return ["space", "comment"];}
171+
if (is(ch, /[#'@^`~]/)) return [null, "meta"];
174172

175173
var name = readSymbol(stream);
176-
tokenType = "symbol";
177174

178175
if (name === "comment" && state.lastToken === "(")
179176
return (state.tokenize = inComment)(stream, state);
180-
if (is(name, atom) || name.charAt(0) === ":") return "atom";
181-
if (is(name, specialForm) || is(name, coreSymbol)) return "keyword";
182-
if (state.lastToken === "(") return "builtin"; // other head symbol
177+
if (is(name, atom) || name.charAt(0) === ":") return ["symbol", "atom"];
178+
if (is(name, specialForm) || is(name, coreSymbol)) return ["symbol", "keyword"];
179+
if (state.lastToken === "(") return ["symbol", "builtin"]; // other operator
183180

184-
return "variable";
181+
return ["symbol", "variable"];
185182
}
186183

187184
function inString(stream, state) {
@@ -192,7 +189,7 @@ CodeMirror.defineMode("clojure", function (options) {
192189
escaped = !escaped && next === "\\";
193190
}
194191

195-
return "string";
192+
return [null, "string"];
196193
}
197194

198195
function inComment(stream, state) {
@@ -209,9 +206,7 @@ CodeMirror.defineMode("clojure", function (options) {
209206
}
210207
}
211208

212-
tokenType = "ws";
213-
214-
return "comment";
209+
return ["space", "comment"];
215210
}
216211

217212
function readSymbol(stream) {
@@ -251,26 +246,27 @@ CodeMirror.defineMode("clojure", function (options) {
251246
if (stream.sol() && (typeof state.ctx.indentTo !== "number"))
252247
state.ctx.indentTo = state.ctx.start + 1;
253248

254-
tokenType = null;
255-
var style = state.tokenize(stream, state);
256-
var currentToken = stream.current();
249+
var typeStylePair = state.tokenize(stream, state);
250+
var type = typeStylePair[0];
251+
var style = typeStylePair[1];
252+
var current = stream.current();
257253

258-
if (tokenType !== "space") {
254+
if (type !== "space") {
259255
if (state.lastToken === "(" && state.ctx.indentTo === null) {
260-
if (tokenType === "symbol" && (is(currentToken, indentSymbol) ||
261-
is(currentToken, assumeBody)))
256+
if (type === "symbol" &&
257+
(is(current, indentSymbol) || is(current, assumeBody)))
262258
state.ctx.indentTo = state.ctx.start + options.indentUnit;
263259
else state.ctx.indentTo = "next";
264260
} else if (state.ctx.indentTo === "next") {
265261
state.ctx.indentTo = stream.column();
266262
}
267263

268-
state.lastToken = currentToken;
264+
state.lastToken = current;
269265
}
270266

271-
if (tokenType === "open")
267+
if (type === "open")
272268
state.ctx = {prev: state.ctx, start: stream.column(), indentTo: null};
273-
else if (tokenType === "close") state.ctx = state.ctx.prev || state.ctx;
269+
else if (type === "close") state.ctx = state.ctx.prev || state.ctx;
274270

275271
return style;
276272
},

0 commit comments

Comments
 (0)