Skip to content

Commit

Permalink
GitHub Issue #87 - adjust ns indentation to match community style (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac authored Oct 18, 2024
1 parent fcf7e80 commit 8aa0cdd
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 308 deletions.
211 changes: 122 additions & 89 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -2835,14 +2835,14 @@
const wrapRequireMacrosWithReaderConditional = arraySize(cljsPlatformRequireMacros) === numRequireMacros
let rmLastLineCommentAfter = null

let rmIndentation = ' '
let rmIndentation = ' '
if (wrapRequireMacrosWithReaderConditional) {
outTxt = strConcat(outTxt, '\n')
outTxt = strConcat(outTxt, ' #?(:cljs\n')
outTxt = printCommentsAbove(outTxt, ns.requireMacrosCommentsAbove, ' ')
outTxt = strConcat(outTxt, ' (:require-macros\n')

rmIndentation = ' '
rmIndentation = ' '
} else {
outTxt = strConcat(outTxt, '\n')
outTxt = printCommentsAbove(outTxt, ns.requireMacrosCommentsAbove, ' ')
Expand Down Expand Up @@ -2891,29 +2891,61 @@
let closeRequireParenTrail = ')'
let lastRequireHasComment = false
let lastRequireComment = null
const reqPlatforms = getPlatformsFromArray(ns.requires)
const numPlatforms = arraySize(reqPlatforms)

let allRequiresUnderOnePlatform = false
if (numPlatforms === 1) {
const onePlatformRequires = filterOnPlatform(ns.requires, reqPlatforms[0])
if (numRequires === arraySize(onePlatformRequires)) {
allRequiresUnderOnePlatform = true
}
}

let requireLineIndentation = ' '
if (allRequiresUnderOnePlatform) {
outTxt = strConcat(outTxt, '\n #?(')
outTxt = strConcat(outTxt, reqPlatforms[0])

if (isArray(ns.requireCommentsAbove) && arraySize(ns.requireCommentsAbove) > 0) {
outTxt = strConcat(outTxt, '\n ')
outTxt = strConcat(outTxt, strJoin(ns.requireCommentsAbove, '\n '))
}

outTxt = strConcat(outTxt, '\n (:require')
if (isString(ns.requireCommentAfter) && ns.requireCommentAfter !== '') {
outTxt = strConcat3(outTxt, ' ', ns.requireCommentAfter)
}
outTxt = strConcat(outTxt, '\n')

if (isArray(ns.requireCommentsAbove) && arraySize(ns.requireCommentsAbove) > 0) {
outTxt = strConcat(outTxt, '\n ')
outTxt = strConcat(outTxt, strJoin(ns.requireCommentsAbove, '\n '))
requireLineIndentation = ' '
} else {
if (isArray(ns.requireCommentsAbove) && arraySize(ns.requireCommentsAbove) > 0) {
outTxt = strConcat(outTxt, '\n ')
outTxt = strConcat(outTxt, strJoin(ns.requireCommentsAbove, '\n '))
}
outTxt = strConcat(outTxt, '\n (:require\n')
}
outTxt = strConcat(outTxt, '\n (:require\n')

let requiresIdx = 0
while (requiresIdx < numRequires) {
const req = ns.requires[requiresIdx]
// FIXME: this no longer works correctly with reader conditionals
// NOTE: I am not sure this works correctly with reader conditionals
const isLastRequire1 = inc(requiresIdx) === numRequires

if (!req.platform) {
outTxt = strConcat(outTxt, formatRequireLine(req, ' '))
if (!req.platform || allRequiresUnderOnePlatform) {
outTxt = strConcat(outTxt, formatRequireLine(req, requireLineIndentation))

if (req.commentAfter && !isLastRequire1) {
outTxt = strConcat(outTxt, ' ')
outTxt = strConcat(outTxt, req.commentAfter)
outTxt = strConcat(outTxt, '\n')
} else if (isLastRequire1 && req.commentAfter && requireIsLastMainForm) {
} else if (isLastRequire1 && req.commentAfter && requireIsLastMainForm && !allRequiresUnderOnePlatform) {
closeRequireParenTrail = strConcat(')) ', req.commentAfter)
trailingParensArePrinted = true
} else if (isLastRequire1 && req.commentAfter && allRequiresUnderOnePlatform) {
lastRequireComment = req.commentAfter
lastRequireHasComment = true
} else if (isLastRequire1 && req.commentAfter) {
closeRequireParenTrail = strConcat(') ', req.commentAfter)
} else if (isLastRequire1 && !req.commentAfter) {
Expand All @@ -2926,93 +2958,94 @@
requiresIdx = inc(requiresIdx)
}

const reqPlatforms = getPlatformsFromArray(ns.requires)
let platformIdx = 0
const numPlatforms = arraySize(reqPlatforms)

const requireBlockHasReaderConditionals = numPlatforms > 0
const useStandardReaderConditional = onlyOneRequirePerPlatform(ns.requires)

if (!allRequiresUnderOnePlatform) {
// use standard reader conditional #?(
if (useStandardReaderConditional) {
while (platformIdx < numPlatforms) {
const platform = reqPlatforms[platformIdx]

if (platformIdx === 0) {
outTxt = strTrim(outTxt)
outTxt = strConcat3(outTxt, '\n #?(', platform)
outTxt = strConcat(outTxt, ' ')
} else {
outTxt = strConcat(outTxt, '\n ')
outTxt = strConcat3(outTxt, platform, ' ')
}
if (useStandardReaderConditional) {
while (platformIdx < numPlatforms) {
const platform = reqPlatforms[platformIdx]

// only look at requires for this platform
const platformRequires = filterOnPlatform(ns.requires, platform)
const req = platformRequires[0]
outTxt = strConcat(outTxt, formatRequireLine(req, ''))
if (platformIdx === 0) {
outTxt = strTrim(outTxt)
outTxt = strConcat3(outTxt, '\n #?(', platform)
outTxt = strConcat(outTxt, ' ')
} else {
outTxt = strConcat(outTxt, '\n ')
outTxt = strConcat3(outTxt, platform, ' ')
}

// FIXME: need to add commentsBefore and commentsAfter here
// only look at requires for this platform
const platformRequires = filterOnPlatform(ns.requires, platform)
const req = platformRequires[0]
outTxt = strConcat(outTxt, formatRequireLine(req, ''))

platformIdx = inc(platformIdx)
}
// use splicing reader conditional #?@(
} else {
while (platformIdx < numPlatforms) {
const platform = reqPlatforms[platformIdx]
const isLastPlatform = inc(platformIdx) === numPlatforms
// FIXME: need to add commentsBefore and commentsAfter here

if (platformIdx === 0) {
outTxt = strTrim(outTxt)
outTxt = strConcat(outTxt, '\n #?@(')
outTxt = strConcat3(outTxt, platform, '\n [')
} else {
outTxt = strConcat(outTxt, '\n\n ')
outTxt = strConcat3(outTxt, platform, '\n [')
platformIdx = inc(platformIdx)
}

// only look at requires for this platform
const platformRequires = filterOnPlatform(ns.requires, platform)
const numFilteredReqs = arraySize(platformRequires)
let printedFirstReqLine = false
let printPlatformClosingBracket = true
let reqIdx2 = 0
while (reqIdx2 < numFilteredReqs) {
const req = platformRequires[reqIdx2]
const isLastRequireForThisPlatform = inc(reqIdx2) === numFilteredReqs

if (printedFirstReqLine) {
outTxt = strConcat(outTxt, formatRequireLine(req, ' '))
// use splicing reader conditional #?@(
} else {
while (platformIdx < numPlatforms) {
const platform = reqPlatforms[platformIdx]
const isLastPlatform = inc(platformIdx) === numPlatforms

if (platformIdx === 0) {
outTxt = strTrim(outTxt)
outTxt = strConcat(outTxt, '\n #?@(')
outTxt = strConcat3(outTxt, platform, '\n [')
} else {
printedFirstReqLine = true
outTxt = strConcat(outTxt, formatRequireLine(req, ''))
outTxt = strConcat(outTxt, '\n\n ')
outTxt = strConcat3(outTxt, platform, '\n [')
}

if (req.commentAfter && !isLastRequireForThisPlatform) {
outTxt = strConcat(outTxt, ' ')
outTxt = strConcat(outTxt, req.commentAfter)
outTxt = strConcat(outTxt, '\n')
} else if (req.commentAfter && isLastRequireForThisPlatform && !isLastPlatform) {
outTxt = strConcat3(outTxt, '] ', req.commentAfter)
printPlatformClosingBracket = false
} else if (req.commentAfter && isLastRequireForThisPlatform && (isLastPlatform || requireIsLastMainForm)) {
lastRequireHasComment = true
lastRequireComment = req.commentAfter
} else if (isLastRequireForThisPlatform && req.commentAfter) {
closeRequireParenTrail = strConcat(') ', req.commentAfter)
} else if (isLastRequireForThisPlatform && !req.commentAfter) {
closeRequireParenTrail = ']'
} else {
outTxt = strConcat(outTxt, '\n')
// only look at requires for this platform
const platformRequires = filterOnPlatform(ns.requires, platform)
const numFilteredReqs = arraySize(platformRequires)
let printedFirstReqLine = false
let printPlatformClosingBracket = true
let reqIdx2 = 0
while (reqIdx2 < numFilteredReqs) {
const req = platformRequires[reqIdx2]
const isLastRequireForThisPlatform = inc(reqIdx2) === numFilteredReqs

if (printedFirstReqLine) {
outTxt = strConcat(outTxt, formatRequireLine(req, ' '))
} else {
printedFirstReqLine = true
outTxt = strConcat(outTxt, formatRequireLine(req, ''))
}

if (req.commentAfter && !isLastRequireForThisPlatform) {
outTxt = strConcat(outTxt, ' ')
outTxt = strConcat(outTxt, req.commentAfter)
outTxt = strConcat(outTxt, '\n')
} else if (req.commentAfter && isLastRequireForThisPlatform && !isLastPlatform) {
outTxt = strConcat3(outTxt, '] ', req.commentAfter)
printPlatformClosingBracket = false
} else if (req.commentAfter && isLastRequireForThisPlatform && (isLastPlatform || requireIsLastMainForm)) {
lastRequireHasComment = true
lastRequireComment = req.commentAfter
} else if (isLastRequireForThisPlatform && req.commentAfter) {
closeRequireParenTrail = strConcat(') ', req.commentAfter)
} else if (isLastRequireForThisPlatform && !req.commentAfter) {
closeRequireParenTrail = ']'
} else {
outTxt = strConcat(outTxt, '\n')
}

reqIdx2 = inc(reqIdx2)
}

reqIdx2 = inc(reqIdx2)
}
if (printPlatformClosingBracket) {
outTxt = strConcat(outTxt, ']')
}

if (printPlatformClosingBracket) {
outTxt = strConcat(outTxt, ']')
platformIdx = inc(platformIdx)
}

platformIdx = inc(platformIdx)
}
}

Expand Down Expand Up @@ -3063,7 +3096,7 @@
const imp = nonPlatformSpecificImports[importsIdx]
const isLastImport = inc(importsIdx) === numNonPlatformSpecificImports

outTxt = strConcat3(outTxt, ' (', imp.package)
outTxt = strConcat3(outTxt, ' (', imp.package)

const numClasses = arraySize(imp.classes)
let classNameIdx = 0
Expand Down Expand Up @@ -3100,15 +3133,15 @@
outTxt = strConcat(outTxt, platformStr)
outTxt = strConcat(outTxt, '\n')
outTxt = strConcat(outTxt, ' (:import\n')
outTxt = strConcat(outTxt, ' ')
outTxt = strConcat(outTxt, ' ')
isImportKeywordPrinted = true
} else if (isFirstPlatform) {
outTxt = strConcat3(outTxt, '\n #?@(', platformStr)
outTxt = strConcat(outTxt, '\n [')
outTxt = strConcat3(outTxt, '\n #?@(', platformStr)
outTxt = strConcat(outTxt, '\n [')
isFirstPlatform = false
} else {
outTxt = strConcat3(outTxt, '\n\n ', platformStr)
outTxt = strConcat(outTxt, '\n [')
outTxt = strConcat3(outTxt, '\n\n ', platformStr)
outTxt = strConcat(outTxt, '\n [')
}

const importsForThisPlatform = filterOnPlatform(ns.imports, platformStr)
Expand Down Expand Up @@ -3136,9 +3169,9 @@
outTxt = strConcat3(outTxt, ' ', imp.commentAfter)
}
if (placeReaderConditionalOutsideOfImport) {
outTxt = strConcat(outTxt, '\n ')
outTxt = strConcat(outTxt, '\n ')
} else {
outTxt = strConcat(outTxt, '\n ')
outTxt = strConcat(outTxt, '\n ')
}
}

Expand Down Expand Up @@ -3192,7 +3225,7 @@
outTxt = strConcat3(outTxt, ' ', ns.genClass.commentAfter)
}

const genClassValueIndentationLevel = genClassIndentationLevel + 2
const genClassValueIndentationLevel = inc(genClassIndentationLevel)
const indentationStr2 = repeatString(' ', genClassValueIndentationLevel)

// print the :gen-class keys in the order in which they appear in the clojure.core.genclass documentation
Expand Down
1 change: 0 additions & 1 deletion test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ const ignoreTests = new Set()
ignoreTests.add('Surrounding newlines removed 3')

ignoreTests.add('ambiguous import comment')
ignoreTests.add('ns: figwheel.main')

allTestCases.forEach(testCase => {
let runThisTest = true
Expand Down
16 changes: 8 additions & 8 deletions test_format/format.eno
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,8 @@ y 4]
(ns metabase.api.common
"Dynamic variables and utility functions/macros for writing API functions."
(:require
[clojure.set :as set]
[potemkin :as p]))
[clojure.set :as set]
[potemkin :as p]))
--Expected

# slurp closing parens around comments 1
Expand Down Expand Up @@ -1097,7 +1097,7 @@ y 4]
--Expected
(ns foo.bar
(:require
[baz.core]))
[baz.core]))

(comment)
;; foo
Expand Down Expand Up @@ -1538,7 +1538,7 @@ ddd}
;; :standard-clj/ignore-file
(ns com.example
(:require
[clojure.string]))
[clojure.string]))

"aaa"
--Expected
Expand All @@ -1559,7 +1559,7 @@ ddd}
:standard-clj/ignore-file
(ns com.example
(:require
[clojure.string]))
[clojure.string]))

"aaa"
--Expected
Expand All @@ -1582,7 +1582,7 @@ ddd}
:standard-clj/ignore-file
(ns com.example
(:require
[clojure.string]))
[clojure.string]))

"aaa"
--Expected
Expand Down Expand Up @@ -1649,7 +1649,7 @@ ddd}
#_ {:standard-clj/ignore-file false}
(ns com.example
(:require
[clojure.string]))
[clojure.string]))

"aaa"
--Expected
Expand Down Expand Up @@ -1696,7 +1696,7 @@ false}
false}
(ns com.example
(:require
[clojure.string]))
[clojure.string]))

"aaa"
--Expected
Expand Down
Loading

0 comments on commit 8aa0cdd

Please sign in to comment.