diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index 177f2ee..d64cb80 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -1208,6 +1208,9 @@ const nodeTxtLength = strLen(nodeTxt) node._origColIdx = colIdx colIdx = colIdx + nodeTxtLength + } else if (isTagNode(node)) { + node._origColIdx = colIdx + colIdx = colIdx + 1 } } @@ -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 @@ -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) @@ -3453,7 +3460,7 @@ const currentNodeIsWhitespace = isWhitespaceNode(node) const currentNodeIsNewline = isNewlineNode(node) - const currentNodeIsTag = isTagNode(node) + let skipPrintingThisNode = false if (isStandardCljIgnoreKeyword(node) && idx > 1) { @@ -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) @@ -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)) { @@ -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') } diff --git a/test_format/format.eno b/test_format/format.eno index dfeaafc..8df5bf7 100644 --- a/test_format/format.eno +++ b/test_format/format.eno @@ -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