Skip to content

Issue #189 - set alignment bug #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
161 changes: 161 additions & 0 deletions test_format/format.eno
Original file line number Diff line number Diff line change
Expand Up @@ -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