Skip to content

Commit

Permalink
WIP - saving progress
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac committed Oct 25, 2024
1 parent 23be243 commit 9524480
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
47 changes: 28 additions & 19 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -2593,15 +2593,25 @@
return s
}

function formatReferClojureSingleKeyword (kwd, symbolsArr) {
function formatReferClojureSingleKeyword (ns, excludeOrOnly) {
// const symbolsArr = referClojure[kwd]

const symbolsArr = ns.referClojure[excludeOrOnly]
const kwd = strConcat(':', excludeOrOnly)

const platforms = getPlatformsFromArray(symbolsArr)
const numPlatforms = arraySize(platforms)
const symbolsForAllPlatforms = arrayPluck(filterOnPlatform(symbolsArr, false), 'symbol')
const numSymbolsForAllPlatforms = arraySize(symbolsForAllPlatforms)

// there are no reader conditionals: print all of the symbols
if (numPlatforms === 0) {
let s = '\n (:refer-clojure '
// printCommentsAbove(xxxx)

let s = '\n'
s = printCommentsAbove(s, ns.referClojureCommentsAbove, ' ')

s = strConcat(s, ' (:refer-clojure ')
s = strConcat(s, formatKeywordFollowedByListOfSymbols(kwd, symbolsForAllPlatforms))
s = strConcat(s, ')')
return s
Expand Down Expand Up @@ -2689,38 +2699,38 @@
}
}

function formatReferClojure (referClojure) {
const keys = getReferClojureKeys(referClojure)
function formatReferClojure (ns) {
const keys = getReferClojureKeys(ns.referClojure)
const numKeys = arraySize(keys)

// there are no :refer-clojure items, we are done
if (numKeys === 0) {
return ''
// there is only :exclude
} else if (numKeys === 1 && keys[0] === ':exclude') {
return formatReferClojureSingleKeyword(':exclude', referClojure.exclude)
return formatReferClojureSingleKeyword(ns, 'exclude')

// there is only :only
} else if (numKeys === 1 && keys[0] === ':only') {
return formatReferClojureSingleKeyword(':only', referClojure.only)
return formatReferClojureSingleKeyword(ns, 'only')

// there is only :rename
} else if (numKeys === 1 && keys[0] === ':rename') {
const platforms = getPlatformsFromArray(referClojure.rename)
const platforms = getPlatformsFromArray(ns.referClojure.rename)
const numPlatforms = arraySize(platforms)
const nonPlatformSpecificRenames = filterOnPlatform(referClojure.rename, false)
const nonPlatformSpecificRenames = filterOnPlatform(ns.referClojure.rename, false)
const numNonPlatformSpecificRenames = arraySize(nonPlatformSpecificRenames)
const allRenamesForSamePlatform = numNonPlatformSpecificRenames === 0 && arraySize(platforms) > 0

if (numPlatforms === 0) {
let s = '\n (:refer-clojure :rename {'
s = strConcat(s, formatRenamesList(referClojure.rename))
s = strConcat(s, formatRenamesList(ns.referClojure.rename))
s = strConcat(s, '})')
return s
} else if (numPlatforms === 1 && allRenamesForSamePlatform) {
let s = strConcat3('\n #?(', platforms[0], '\n')
s = strConcat(s, ' (:refer-clojure :rename {')
s = strConcat(s, formatRenamesList(referClojure.rename))
s = strConcat(s, formatRenamesList(ns.referClojure.rename))
s = strConcat(s, '}))')
return s
} else {
Expand All @@ -2731,7 +2741,7 @@
let platformIdx = 0
while (platformIdx < numPlatforms) {
const platformStr = platforms[platformIdx]
const platformRenames = filterOnPlatform(referClojure.rename, platformStr)
const platformRenames = filterOnPlatform(ns.referClojure.rename, platformStr)

if (platformIdx === 0) {
s = strConcat3(s, platformStr, ' [')
Expand All @@ -2754,21 +2764,21 @@
} else {
let s = '\n (:refer-clojure'

if (referClojure.exclude && arraySize(referClojure.exclude) > 0) {
const excludeSymbols = arrayPluck(referClojure.exclude, 'symbol')
if (ns.referClojure.exclude && arraySize(ns.referClojure.exclude) > 0) {
const excludeSymbols = arrayPluck(ns.referClojure.exclude, 'symbol')
s = strConcat(s, '\n ')
s = strConcat(s, formatKeywordFollowedByListOfSymbols(':exclude', excludeSymbols))
}

if (referClojure.only && arraySize(referClojure.only) > 0) {
const onlySymbols = arrayPluck(referClojure.only, 'symbol')
if (ns.referClojure.only && arraySize(ns.referClojure.only) > 0) {
const onlySymbols = arrayPluck(ns.referClojure.only, 'symbol')
s = strConcat(s, '\n ')
s = strConcat(s, formatKeywordFollowedByListOfSymbols(':only', onlySymbols))
}

if (referClojure.rename && arraySize(referClojure.rename) > 0) {
if (ns.referClojure.rename && arraySize(ns.referClojure.rename) > 0) {
s = strConcat(s, '\n :rename {')
s = strConcat(s, formatRenamesList(referClojure.rename))
s = strConcat(s, formatRenamesList(ns.referClojure.rename))
s = strConcat(s, '}')
}

Expand Down Expand Up @@ -2833,9 +2843,8 @@
}

// FIXME - we need reader conditionals for :refer-clojure here
// FIXME - comments for :refer-clojure
if (ns.referClojure) {
outTxt = strConcat(outTxt, formatReferClojure(ns.referClojure))
outTxt = strConcat(outTxt, formatReferClojure(ns))
}

if (numRequireMacros > 0) {
Expand Down
3 changes: 2 additions & 1 deletion test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ test('All test_format/ cases should have unique names', () => {

// dev convenience: set this to true and add specific test cases
// only those cases will run
const onlyRunSpecificTests = false
const onlyRunSpecificTests = true
const specificTests = new Set()
// specificTests.add('your test case here')
specificTests.add('GitHub Issue #140 - :refer-clojure comments')

const ignoreSomeTests = true
const ignoreTests = new Set()
Expand Down
17 changes: 17 additions & 0 deletions test_format/ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -1727,3 +1727,20 @@

(defprotocol ToJson (-to-json [value buffer]))
--Expected

# GitHub Issue #140 - :refer-clojure comments

--Input
(ns com.example.my-app
;; aaa
;; bbb
(:refer-clojure :only [number? -> set ; ccc
]))
--Input

--Expected
(ns com.example.my-app
;; aaa
;; bbb
(:refer-clojure :only [-> number? set])) ; ccc
--Expected
16 changes: 16 additions & 0 deletions test_parse_ns/parse_ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -2726,3 +2726,19 @@
]
}
--Expected



# GitHub Issue #140 - :refer-clojure comments

--Input
(ns com.example.my-app
;; comment 1
(:refer-clojure
:exclude [some-> get]) ;; comment 2
)
--Input

--Expected
--Expected

0 comments on commit 9524480

Please sign in to comment.