Skip to content

Commit c3d8336

Browse files
finalfantasiamarijnh
authored andcommitted
[clojure mode] Use whitelist for indenting forms with body parameter.
The current regular expression-based heuristics erroneously matches symbols that don't necessary have body parameter (e.g., `default-for`).
1 parent 2787836 commit c3d8336

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

mode/clojure/clojure.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"use strict";
1313

1414
CodeMirror.defineMode("clojure", function (options) {
15-
var commonAtoms = ["false", "nil", "true"];
16-
var commonSpecialForms = [".", "catch", "def", "do", "if", "monitor-enter",
15+
var atoms = ["false", "nil", "true"];
16+
var specialForms = [".", "catch", "def", "do", "if", "monitor-enter",
1717
"monitor-exit", "new", "quote", "recur", "set!", "throw", "try", "var"];
18-
var commonCoreSymbols = ["*", "*'", "*1", "*2", "*3", "*agent*",
18+
var coreSymbols = ["*", "*'", "*1", "*2", "*3", "*agent*",
1919
"*allow-unresolved-vars*", "*assert*", "*clojure-version*",
2020
"*command-line-args*", "*compile-files*", "*compile-path*",
2121
"*compiler-options*", "*data-readers*", "*default-data-reader-fn*", "*e",
@@ -137,23 +137,22 @@ CodeMirror.defineMode("clojure", function (options) {
137137
"with-local-vars", "with-meta", "with-open", "with-out-str",
138138
"with-precision", "with-redefs", "with-redefs-fn", "xml-seq", "zero?",
139139
"zipmap"];
140-
var commonIndentSymbols = [
140+
var formsThatHaveBodyParameter = [
141141
"assoc", "binding", "bound-fn", "case", "catch", "comment", "cond",
142-
"condp", "def", "defmethod", "defn", "defprotocol", "defrecord",
143-
"defstruct", "deftype", "do", "doseq", "dotimes", "doto", "extend",
144-
"extend-protocol", "extend-type", "fn", "for", "future", "if", "if-let",
145-
"if-not", "let", "letfn", "locking", "loop", "ns", "proxy", "reify",
146-
"struct-map", "try", "when", "when-first", "when-let", "when-not",
147-
"when-some", "while", "with-open", "with-precision"];
142+
"condp", "def", "defmethod", "defn", "defmacro", "defprotocol",
143+
"defrecord", "defstruct", "deftype", "do", "doseq", "dotimes", "doto",
144+
"extend", "extend-protocol", "extend-type", "fn", "for", "future", "if",
145+
"if-let", "if-not", "let", "letfn", "locking", "loop", "ns", "proxy",
146+
"reify", "struct-map", "try", "when", "when-first", "when-let",
147+
"when-not", "when-some", "while", "with-open", "with-precision"];
148148

149149
CodeMirror.registerHelper("hintWords", "clojure",
150-
commonAtoms.concat(commonSpecialForms, commonCoreSymbols));
150+
[].concat(atoms, specialForms, coreSymbols));
151151

152-
var atom = createLookupMap(commonAtoms);
153-
var specialForm = createLookupMap(commonSpecialForms);
154-
var coreSymbol = createLookupMap(commonCoreSymbols);
155-
var indentSymbol = createLookupMap(commonIndentSymbols);
156-
var assumeBody = /^(?:def|with)[^\/]+$|\/(?:def|with)/;
152+
var atom = createLookupMap(atoms);
153+
var specialForm = createLookupMap(specialForms);
154+
var coreSymbol = createLookupMap(coreSymbols);
155+
var hasBodyParameter = createLookupMap(formsThatHaveBodyParameter);
157156
var numberLiteral = /^[+\-]?\d+(?:(?:N|(?:[eE][+\-]?\d+))|(?:\.?\d*(?:M|(?:[eE][+\-]?\d+))?)|\/\d+|[xX][0-9a-fA-F]+|r[0-9a-zA-Z]+)?/;
158157
var symbol = /[!#'*+\-.\/:<>?_\w\xa1-\uffff]/;
159158

@@ -253,8 +252,7 @@ CodeMirror.defineMode("clojure", function (options) {
253252

254253
if (type !== "space") {
255254
if (state.lastToken === "(" && state.ctx.indentTo === null) {
256-
if (type === "symbol" &&
257-
(is(current, indentSymbol) || is(current, assumeBody)))
255+
if (type === "symbol" && is(current, hasBodyParameter))
258256
state.ctx.indentTo = state.ctx.start + options.indentUnit;
259257
else state.ctx.indentTo = "next";
260258
} else if (state.ctx.indentTo === "next") {

mode/clojure/test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -650,12 +650,6 @@
650650
" [bracket (][keyword println] [variable b][bracket )))]"
651651
);
652652

653-
MT("should indent deftest and similar forms that assume body parameter",
654-
"[bracket (][builtin clojure.test/deftest] [variable foo-test]",
655-
" [bracket (][builtin testing] [string \"that foo should work\"]",
656-
" [bracket (][builtin is] [bracket (][keyword =] [atom :baz] [bracket (][builtin foo] [atom :bar][bracket )))))]"
657-
);
658-
659653
function typeTokenPairs(type, tokens) {
660654
return "[" + type + " " + tokens.join("] [" + type + " ") + "]";
661655
}

0 commit comments

Comments
 (0)