Skip to content

Commit

Permalink
GitHub Issue #165 - fix bug with tag literal alignment (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac authored Dec 3, 2024
1 parent a68063d commit c14bce8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
37 changes: 14 additions & 23 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,9 @@
const nodeTxtLength = strLen(nodeTxt)
node._origColIdx = colIdx
colIdx = colIdx + nodeTxtLength
} else if (isTagNode(node)) {
node._origColIdx = colIdx
colIdx = colIdx + 1
}
}

Expand Down Expand Up @@ -3407,7 +3410,6 @@
let lineIdxOfClosingNsForm = -1
let nsStartStringIdx = -1
let nsEndStringIdx = -1
let taggedNodeIdx = -1
let ignoreNodesStartId = -1
let ignoreNodesEndId = -1
let insideTheIgnoreZone = false
Expand Down Expand Up @@ -3438,6 +3440,11 @@
insideTheIgnoreZone = false
}
} else {
// edge case: add '#' text to .tag nodes
if (isTagNode(node)) {
node.text = '#'
}

// record original column indexes for the first line
if (idx === 0) {
nodesArr = recordOriginalColIndexes(nodesArr, idx)
Expand All @@ -3453,7 +3460,7 @@

const currentNodeIsWhitespace = isWhitespaceNode(node)
const currentNodeIsNewline = isNewlineNode(node)
const currentNodeIsTag = isTagNode(node)

let skipPrintingThisNode = false

if (isStandardCljIgnoreKeyword(node) && idx > 1) {
Expand Down Expand Up @@ -3550,20 +3557,11 @@
}
}

// flag the index of a tagged literal node so we can mark the next one if necessary
if (node.name === '.tag') {
taggedNodeIdx = idx
}

// add nodes to the top of the parenStack if we are on the opening line
const topOfTheParenStack = stackPeek(parenStack, 0)
if (topOfTheParenStack && nodeContainsText(node)) {
const onOpeningLineOfParenStack = lineIdx === topOfTheParenStack._parenOpenerLineIdx
if (onOpeningLineOfParenStack) {
if (taggedNodeIdx === dec(idx)) {
node._nodeIsTagLiteral = true
}

node._colIdx = colIdx
node._lineIdx = lineIdx
stackPush(topOfTheParenStack._openingLineNodes, node)
Expand Down Expand Up @@ -3677,21 +3675,16 @@
while (searchForAlignmentNode) {
const openingLineNode = topOfTheParenStack._openingLineNodes[openingLineNodeIdx]
if (openingLineNode) {
// Is the first node on this new line vertically aligned with any of the nodes
// on the line above that are in the same paren stack?
// Is the first node on this new line vertically aligned with any of the nodes
// on the line above that are in the same paren stack?
if (pastFirstWhitespaceNode && isNodeWithNonBlankText(openingLineNode) && openingLineNode._origColIdx === numSpacesOnNextLine) {
// Rule 3 is activated 👍
// Rule 3 is activated 👍
topOfTheParenStack._rule3Active = true

// NOTE: we use the original _colIdx of this node in order to determine Rule 3 alignment,
// but we use the _printedColIdx of this node to determine the number of leading spaces
topOfTheParenStack._rule3NumSpaces = openingLineNode._printedColIdx

// edge case: align tagged literals to the # char
if (openingLineNode._nodeIsTagLiteral) {
topOfTheParenStack._rule3NumSpaces = dec(openingLineNode._printedColIdx)
}

// we are done searching at this point
searchForAlignmentNode = false
} else if (!pastFirstWhitespaceNode && isWhitespaceNode(openingLineNode)) {
Expand Down Expand Up @@ -3797,15 +3790,13 @@
skipPrintingThisNode = true
} // end currentNodeIsNewline

if ((nodeContainsText(node) || currentNodeIsTag) && !skipPrintingThisNode) {
if (nodeContainsText(node) && !skipPrintingThisNode) {
const isTokenFollowedByOpener = isTokenNode(node) && nextTextNode && isParenOpener(nextTextNode)
const isParenCloserFollowedByText = isParenCloser(node) && nextTextNode && (isTokenNode(nextTextNode) || isParenOpener(nextTextNode))
const addSpaceAfterThisNode = isTokenFollowedByOpener || isParenCloserFollowedByText

let nodeTxt = node.text
if (currentNodeIsTag) {
nodeTxt = '#'
} else if (isCommentNode(node)) {
if (isCommentNode(node)) {
if (commentNeedsSpaceInside(nodeTxt)) {
nodeTxt = strReplaceFirst(nodeTxt, /^(;+)([^ ])/, '$1 $2')
}
Expand Down
44 changes: 44 additions & 0 deletions test_format/format.eno
Original file line number Diff line number Diff line change
Expand Up @@ -2093,3 +2093,47 @@ bbb ;; bar
;; bar
,])
--Expected

# GitHub Issue #165 - tag literal alignment 1

--Input
(case
#uuid "96bbb029-40e7-4566-83d5-7e6088a908fc" :a
;; a comment
#uuid "148ccf8b-6122-4435-9a39-001e13a8aad7" :b)
--Input

--Expected
(case
#uuid "96bbb029-40e7-4566-83d5-7e6088a908fc" :a
;; a comment
#uuid "148ccf8b-6122-4435-9a39-001e13a8aad7" :b)
--Expected

# GitHub Issue #165 - tag literal alignment 2

--Input
(foo bar #aaa "aaa"
;; bbb
:ccc)

(foo bar #aaa "aaa"
;; bbb
:ccc)

(foo
#aaa "aaa")
--Input

--Expected
(foo bar #aaa "aaa"
;; bbb
:ccc)

(foo bar #aaa "aaa"
;; bbb
:ccc)

(foo
#aaa "aaa")
--Expected

0 comments on commit c14bce8

Please sign in to comment.