Skip to content
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

Fix nested :inner specs #350

Merged
merged 1 commit into from
Sep 23, 2024
Merged

Fix nested :inner specs #350

merged 1 commit into from
Sep 23, 2024

Conversation

camsaul
Copy link
Contributor

@camsaul camsaul commented Aug 21, 2024

Fixes #349

After spending a few hours trying to figure out the best way to solve this problem without changing too much stuff I decided to changed the indent order code as follows:

  • specs with multiple parts like [[:block 1] [:inner 1]] now get compiled to separate indenters for each part of the spec e.g. one for [:block 1] and one for [:inner 1]
  • :inner specs with depth greater than zero are sorted preferentially with deeper depths sorted first.

Previously in #349 the sorted-indents here

sorted-indents (sort-by indent-order indents)

would have looked like

[[my.namespace/with-x [[:block 0]]]
 [extend-protocol [[:block 1] [:inner 1]]]

This PR changes them so they now look like

[[extend-protocol [[:inner 1]]
 [my.namespace/with-x [[:block 0]]]
 [extend-protocol [[:block 1]]]

This ensures nested :inner indenters take precedence over general top-level indenters and defprotocol/extend-protocol/etc. are applied in a sane way.

camsaul added a commit to metabase/metabase that referenced this pull request Aug 21, 2024
camsaul added a commit to metabase/metabase that referenced this pull request Aug 21, 2024
* Cljfmt config part 2

* Part 3 WIP

* Part 3 WIP

* Part 3 WIP

* Part 3 WIP

* Part 3 WIP

* Use fork with weavejester/cljfmt#348 and weavejester/cljfmt#350

* Backport updated config and linter fork from part 3

* Update formatting

* Reformat

* Fix bad indentation from #47064
@weavejester
Copy link
Owner

What's the advantage of splitting up the indentation rules, compared to fixing the indentation ordering to prefer more 'outer' indentations?

@camsaul
Copy link
Contributor Author

camsaul commented Aug 22, 2024

Originally I thought keeping them as one rule it would potentially break stuff in weird edge cases , e.g. if you have

letfn [[:block 0] [:inner 1]]
my.namespace/letfn [[:block 0]]

the stuff at depth one inside a my.namespace/letfn could be getting formatted as [:inner 1] but now that I squint at it a bit, it's a) a separate issue, b) splitting the rules up doesn't fix the problem, and c) a really contrived issue anyway.

I updated the PR so it keeps things as one rule and only updates the indent-order function

Copy link
Owner

@weavejester weavejester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I only had one small suggestion around readability.

Comment on lines 342 to 356
(defn- indent-order [[key specs]]
(let [max-depth (transduce
(map (fn [[spec-type :as spec]]
(case spec-type
:inner (let [[_inner depth] spec]
depth)
0)))
max
0
specs)
key-type* (cond
(qualified-symbol? key) 0
(simple-symbol? key) 1
(pattern? key) 2)]
[(- max-depth) key-type* (str key)]))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps instead:

(defn- indent-order [[key specs]]
  (let [get-depth (fn [[type depth]] (if (= type :inner) depth 0))
        max-depth (transduce (map get-depth) max 0 specs)
        key-order  (cond
                    (qualified-symbol? key) 0
                    (simple-symbol? key)    1
                    (pattern? key)          2)]
    [(- max-depth) key-order (str key)]))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@weavejester weavejester merged commit 9c0a19a into weavejester:master Sep 23, 2024
1 check passed
@weavejester
Copy link
Owner

Thanks for the update, and apologies for the delay in reviewing it. This past month has been rather busy for me.

@camsaul camsaul deleted the fix-349 branch October 7, 2024 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

extend-protocol/defprotocol/etc. incorrectly uses indentation specs for method names
2 participants