-
Notifications
You must be signed in to change notification settings - Fork 123
Remove blank lines in forms #375
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
base: master
Are you sure you want to change the base?
Conversation
61fae52
to
3de65e9
Compare
Thanks for the PR. Is there any reason to limit this to definition forms? What if we instead had options:
The latter should include Additionally, what if we narrowed down where the blank lines were allowed, as in {let #{0}
cond :all
comment :all} |
I guess it's not really limited to definition forms, it removes blank lines anywhere except the top level, I just wasn't sure what to call this so I tried to match the naming of the rule in the Community Style Guide https://guide.clojure.style/#no-blank-lines-within-def-forms . But that's not quite the same as what this does so I'll change it
For binding vectors this builds on the recently-added |
3de65e9
to
68c4bb1
Compare
68c4bb1
to
0e99679
Compare
Updated the name of this to make it clearer that it works on all forms and not just |
0e99679
to
6d32d9a
Compare
Ideally we want to follow the least complex solution. Having both In fact, this has led me to reconsider
And for this feature, I think it would be best to have:
This way the options are isolated and therefore more predictable. |
Makes sense. I'll rework this PR later today |
6d32d9a
to
b5f7bb0
Compare
Alright @weavejester I think I made all the changes you suggested, please take another look |
There was a problem hiding this 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've reviewed the code and have some suggestions/improvements.
(defn remove-blank-lines-in-forms | ||
[form blank-line-forms alias-map] | ||
(let [ns-name (find-namespace (z/of-node form)) | ||
context {:alias-map alias-map, :ns-name ns-name}] | ||
(transform | ||
form | ||
edit-all | ||
#(blank-line-in-form? % blank-line-forms context) | ||
replace-blank-lines-in-forms))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a little more concise:
(defn remove-blank-lines-in-forms [form blank-line-forms alias-map]
(let [ns-name (find-namespace (z/of-node form))
context {:alias-map alias-map, :ns-name ns-name}
blank-line? #(blank-line-in-form? % blank-line-forms context)]
(transform form edit-all blank-line? replace-blank-lines-in-forms)))
(defn- blank-line-rule [zloc blank-line-forms context] | ||
(when-let [form-symb (form-symbol zloc)] | ||
(some blank-line-forms | ||
[(remove-namespace form-symb) | ||
(fully-qualified-symbol form-symb context)]))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use form-matches-key?
here. Something more like:
(defn- blank-lines-allowed-in-form? [zloc blank-line-forms context]
(and (z/list? (z/up zloc))
(some (fn [[k indexes]]
(and (form-matches-key? zloc k context)
(or (= :all indexes)
(contains? (set indexes) (dec (index-of zloc)))))
blank-line-forms)))
In fact, we could use the same logic for both forms with blank lines and forms we need to align. So let's do something like:
(defn- matching-form? [zloc forms context]
(and (z/list? (z/up zloc))
(some (fn [[k indexes]]
(and (form-matches-key? zloc k context)
(or (= :all indexes)
(contains? (set indexes) (dec (index-of zloc)))))
forms)))
This replaces aligned-form?
.
(defn- blank-line-in-form? [zloc blank-line-forms context] | ||
(and (z/linebreak? zloc) | ||
(> (count-newlines zloc) 1) | ||
(cond | ||
(z/list? (z/up zloc)) | ||
(not= (blank-line-rule zloc blank-line-forms context) :all) | ||
|
||
(z/list? (z/up (z/up zloc))) | ||
(let [index (dec (index-of (z/up zloc))) | ||
rule (blank-line-rule | ||
(z/up zloc) | ||
blank-line-forms | ||
context) | ||
allowed-indexes (if (set? rule) | ||
rule | ||
#{})] | ||
(not (allowed-indexes index)))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem like it'll work for an arbitrary nesting depth. What we want to do is determine whether the zloc represents multiple newlines, and then iterate up the tree until we hit root (nil
), checking if any containing form is also a matching form.
(defn- replace-blank-lines-in-forms [zloc] | ||
(z/replace zloc (n/newline-node "\n"))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more accurate name for the function is replace-with-single-newline
, as that's what it does, rather than what it's for.
" (sequential? x)" | ||
" :seq)"]] | ||
(is (reformats-to? form form {:remove-blank-lines-in-forms? true | ||
:extra-indents '{better-cond.core/cond* [[:block 0]]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep lines to 80 characters or less, even in tests.
;; I don't think it's appropriate to second-guess things like map formatting | ||
;; at this point, I'd rather error on the side of being too conservative in | ||
;; what we rewrite |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment can be removed.
|
||
* `:remove-blank-lines-in-forms?` - whether to remove blank lines inside forms | ||
per the [community style | ||
recommendation](https://guide.clojure.style/#no-blank-lines-within-def-forms). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use an external URL to this link, like [community style recommendation][no-blank-lines]
.
Resolves #374