diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index e67ead1..ae59ee6 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -858,26 +858,36 @@ return nodeContainsText(n) && !isWhitespaceNode(n) } - function isOneSpaceOpener (opener) { - // TODO: also check node type here? - return opener.text === '{' || opener.text === '[' + function isMapLiteralOpener (n) { + return n && n.name === '.open' && n.text === '{' } - function isAnonFnOpener (opener) { - return opener.text === '#(' + function isVectorLiteralOpener (n) { + return n && n.name === '.open' && n.text === '[' } - function isNamespacedMapOpener (opener) { - return opener.name === '.open' && strStartsWith(opener.text, '#:') && strEndsWith(opener.text, '{') + function isSingleParenOpener (n) { + return n && n.name === '.open' && n.text === '(' } - function isReaderConditionalOpener (opener) { - // TODO: also check node type here? - return opener.text === '#?(' || opener.text === '#?@(' + function isAnonFnOpener (n) { + return n && n.name === '.open' && n.text === '#(' + } + + function isSetLiteralOpener (n) { + return n && n.name === '.open' && n.text === '#{' + } + + function isNamespacedMapOpener (n) { + return n && n.name === '.open' && strStartsWith(n.text, '#:') && strEndsWith(n.text, '{') + } + + function isReaderConditionalOpener (n) { + return n && n.name === '.open' && (n.text === '#?(' || n.text === '#?@(') } function isOpeningBraceNode (n) { - return n.name === 'braces' && isArray(n.children) && arraySize(n.children) === 3 && n.children[2].name === '.close' && n.children[2].text === '}' + return n && n.name === 'braces' && isArray(n.children) && arraySize(n.children) === 3 && n.children[2].name === '.close' && n.children[2].text === '}' } function commentNeedsSpaceBefore (lineTxt, nodeTxt) { @@ -1282,8 +1292,22 @@ if (isReaderConditionalOpener(wrappingOpener)) { return directlyUnderneathOpener } else if (nextNodeAfterOpener && isParenOpener(nextNodeAfterOpener)) { + if (isMapLiteralOpener(wrappingOpener)) { + return inc(openerColIdx) + } else if (isVectorLiteralOpener(wrappingOpener)) { + return inc(openerColIdx) + } else if (isSingleParenOpener(wrappingOpener)) { + return inc(openerColIdx) + } else if (isSetLiteralOpener(wrappingOpener)) { + return inc(inc(openerColIdx)) + } else if (isAnonFnOpener(wrappingOpener)) { + return inc(inc(openerColIdx)) + } else { + throw new Error('Error inside numSpacesForIndentation function. This condition should be unreachable.') + } + } else if (isMapLiteralOpener(wrappingOpener)) { return inc(openerColIdx) - } else if (isOneSpaceOpener(wrappingOpener)) { + } else if (isVectorLiteralOpener(wrappingOpener)) { return inc(openerColIdx) } else if (isAnonFnOpener(wrappingOpener)) { return openerColIdx + 3 diff --git a/test_format/format.eno b/test_format/format.eno index 5399f09..30a652c 100644 --- a/test_format/format.eno +++ b/test_format/format.eno @@ -2175,3 +2175,164 @@ com.example.my-app) [] (println "boop")) --Expected + +# GitHub Issue #189 - set alignment bug 1 + +--Input +#{1 + 2 + 3 + 4} +--Input + +--Expected +#{1 + 2 + 3 + 4} +--Expected + +# GitHub Issue #189 - set alignment bug 2 + +--Input +#{[1] + [2] + [3] + [4]} +--Input + +--Expected +#{[1] + [2] + [3] + [4]} +--Expected + +# GitHub Issue #189 - set alignment bug 3 + +--Input +#{1 + [2] + 3 + [4] + 5} +--Input + +--Expected +#{1 + [2] + 3 + [4] + 5} +--Expected + +# GitHub Issue #189 - set alignment bug 4 + +--Input +#{[1] + 2 + [3] + 4 + [5]} +--Input + +--Expected +#{[1] + 2 + [3] + 4 + [5]} +--Expected + +# GitHub Issue #189 - set alignment bug 5 + +--Input +#{{:a "a"} + :b + [:c]} +--Input + +--Expected +#{{:a "a"} + :b + [:c]} +--Expected + +# GitHub Issue #189 - set alignment bug 6 + +--Input +(defn nested [] + #{{:a "a"} + :b + [:c]}) +--Input + +--Expected +(defn nested [] + #{{:a "a"} + :b + [:c]}) +--Expected + +# GitHub Issue #189 - set alignment bug 7 + +--Input +#{#(:aaa %) + :b + #(:ccc %)} +--Input + +--Expected +#{#(:aaa %) + :b + #(:ccc %)} +--Expected + +# GitHub Issue #189 - set alignment bug 8 + +--Input +#{#{"a"} :a + :b + #(:ccc %) + } +--Input + +--Expected +#{#{"a"} :a + :b + #(:ccc %)} +--Expected + +# GitHub Issue #189 - set alignment bug 9 + +--Input +#{#?(:clj "a" + :cljs "b") + :b + #(:ccc %) + } +--Input + +--Expected +#{#?(:clj "a" + :cljs "b") + :b + #(:ccc %)} +--Expected + +# GitHub Issue #189 - set alignment bug 10 + +--Input +#{ #:xyz{ #:aaa{:a "a" + :b "b"} } + :ccc + :ddd + } +--Input + +--Expected +#{#:xyz{#:aaa{:a "a" + :b "b"}} + :ccc + :ddd} +--Expected