Skip to content

Commit c53b342

Browse files
Abdussalam Abdurrahmanmarijnh
authored andcommitted
[clojure mode] Unify equality checks for more clarity and readability.
1 parent 58acfdd commit c53b342

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

mode/clojure/clojure.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,19 @@ CodeMirror.defineMode("clojure", function (options) {
165165

166166
var ch = stream.next();
167167

168-
if (ch === "\\") {stream.next(); readSymbol(stream); return "string-2";}
169-
if (ch === '"') return (state.tokenize = inString)(stream, state);
170-
if (/[(\[{]/.test(ch)) {tokenType = "open"; return "bracket";}
171-
if (/[)\]}]/.test(ch)) {tokenType = "close"; return "bracket";}
172-
if (ch === ";") {stream.skipToEnd(); tokenType = "space"; return "comment";}
173-
if (/[#'@^`~]/.test(ch)) return "meta";
168+
if (is(ch, "\\")) {stream.next(); readSymbol(stream); return "string-2";}
169+
if (is(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 (is(ch, ";")) {stream.skipToEnd(); tokenType = "space"; return "comment";}
173+
if (is(ch, /[#'@^`~]/)) return "meta";
174174

175175
var name = readSymbol(stream);
176176
tokenType = "symbol";
177177

178-
if (atom.propertyIsEnumerable(name) || name.charAt(0) === ":") return "atom";
179-
if (specialForm.propertyIsEnumerable(name) ||
180-
coreSymbol.propertyIsEnumerable(name)) return "keyword";
181-
if (state.lastToken === "(") return "builtin"; // head symbol
178+
if (is(name, atom) || is(name.charAt(0), ":")) return "atom";
179+
if (is(name, specialForm) || is(name, coreSymbol)) return "keyword";
180+
if (is(state.lastToken, "(")) return "builtin"; // other head symbol
182181

183182
return "variable";
184183
}
@@ -187,8 +186,8 @@ CodeMirror.defineMode("clojure", function (options) {
187186
var escaped = false, next;
188187

189188
while (next = stream.next()) {
190-
if (next === '"' && !escaped) { state.tokenize = base; break; }
191-
escaped = !escaped && next === "\\";
189+
if (is(next, '"') && !escaped) {state.tokenize = base; break;}
190+
escaped = !escaped && is(next, "\\");
192191
}
193192

194193
return "string";
@@ -198,7 +197,7 @@ CodeMirror.defineMode("clojure", function (options) {
198197
var ch;
199198

200199
while (ch = stream.next()) {
201-
if (ch === "\\") stream.next();
200+
if (is(ch, "\\")) stream.next();
202201
else if (!symbol.test(ch)) {stream.backUp(1); break;}
203202
}
204203

@@ -213,6 +212,12 @@ CodeMirror.defineMode("clojure", function (options) {
213212
return obj;
214213
}
215214

215+
function is(name, test) {
216+
if (test instanceof RegExp) return test.test(name);
217+
if (test instanceof Object) return test.propertyIsEnumerable(name);
218+
return name === test;
219+
}
220+
216221
return {
217222
startState: function () {
218223
return {
@@ -229,31 +234,30 @@ CodeMirror.defineMode("clojure", function (options) {
229234
tokenType = null;
230235
var style = state.tokenize(stream, state);
231236

232-
if (tokenType !== "space") {
233-
if (state.lastToken === "(" && state.ctx.indentTo === null) {
234-
if (tokenType === "symbol" &&
235-
(indentSymbol.propertyIsEnumerable(stream.current()) ||
236-
assumeBody.test(stream.current())))
237+
if (!is(tokenType, "space")) {
238+
if (is(state.lastToken, "(") && is(state.ctx.indentTo, null)) {
239+
if (is(tokenType, "symbol") && (is(stream.current(), indentSymbol) ||
240+
is(stream.current(), assumeBody)))
237241
state.ctx.indentTo = state.ctx.start + options.indentUnit;
238242
else state.ctx.indentTo = "next";
239-
} else if (state.ctx.indentTo === "next") {
243+
} else if (is(state.ctx.indentTo, "next")) {
240244
state.ctx.indentTo = stream.column();
241245
}
242246

243247
state.lastToken = stream.current();
244248
}
245249

246-
if (tokenType === "open")
250+
if (is(tokenType, "open"))
247251
state.ctx = {prev: state.ctx, start: stream.column(), indentTo: null};
248-
else if (tokenType === "close") state.ctx = state.ctx.prev || state.ctx;
252+
else if (is(tokenType, "close")) state.ctx = state.ctx.prev || state.ctx;
249253

250254
return style;
251255
},
252256

253257
indent: function (state) {
254258
var i = state.ctx.indentTo;
255259

256-
return (typeof i === "number") ?
260+
return (is(typeof i, "number")) ?
257261
i :
258262
state.ctx.start + 1;
259263
},

0 commit comments

Comments
 (0)