-
Notifications
You must be signed in to change notification settings - Fork 119
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 #45: Thread first changes the indentation of the inner forms #344
Open
lambdank
wants to merge
4
commits into
weavejester:master
Choose a base branch
from
lambdank:thread-first
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1dfbc99
Thread first changes the indentation of the inner forms
lambdank 65e8156
Fixes related to comments
lambdank b400730
Fixed issue :inner checked for thread-wrapping in current zloc instea…
lambdank e3c2e78
Created :thread rule and handled idx and :even/:odd
lambdank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,15 +275,41 @@ | |
(symbol? key) (= key sym) | ||
(pattern? key) (re-find key (str sym))))) | ||
|
||
(defn form-matches-key? [zloc key context] | ||
(defn- form-symbol-matches? [zloc pred context] | ||
(let [possible-sym (form-symbol zloc)] | ||
(or (symbol-matches-key? (fully-qualified-symbol possible-sym context) key) | ||
(symbol-matches-key? (remove-namespace possible-sym) key)))) | ||
(or (pred (fully-qualified-symbol possible-sym context)) | ||
(pred (remove-namespace possible-sym))))) | ||
|
||
(defn- form-matches-key? [zloc key context] | ||
(form-symbol-matches? zloc #(symbol-matches-key? % key) context)) | ||
|
||
(defn- form-matches-thread-macro? [zloc context] | ||
(form-symbol-matches? zloc (:threads context) context)) | ||
|
||
(defn- get-zipper-position [zloc] | ||
(loop [idx 0 | ||
loop-zloc (z/leftmost zloc)] | ||
(cond | ||
(= zloc loop-zloc) idx | ||
(= loop-zloc (z/rightmost zloc)) nil | ||
:else (recur (inc idx) (z/right loop-zloc))))) | ||
|
||
(defn- in-thread-macro? [zloc context] | ||
(when (= zloc (z/root zloc)) | ||
(when-some [idx (form-matches-thread-macro? zloc context)] | ||
(let [position (cond-> (get-zipper-position (z/up zloc)) | ||
(in-thread-macro? (z/up (z/up zloc)) context) inc)] | ||
(cond | ||
(< position 2) false | ||
(= idx :odd) (odd? position) | ||
(= idx :even) (even? position) | ||
:else (<= idx position)))))) | ||
|
||
(defn- inner-indent [zloc key depth idx context] | ||
(let [top (nth (iterate z/up zloc) depth)] | ||
(let [top (nth (iterate z/up zloc) depth) | ||
adjusted-idx (cond-> idx (in-thread-macro? zloc context) (some-> dec))] | ||
(when (and (form-matches-key? top key context) | ||
(or (nil? idx) (index-matches-top-argument? zloc depth idx))) | ||
(or (nil? idx) (index-matches-top-argument? zloc depth adjusted-idx))) | ||
(let [zup (z/up zloc)] | ||
(+ (margin zup) (indent-width zup)))))) | ||
|
||
|
@@ -302,9 +328,10 @@ | |
|
||
(defn- block-indent [zloc key idx context] | ||
(when (form-matches-key? zloc key context) | ||
(let [zloc-after-idx (some-> zloc (nth-form (inc idx)))] | ||
(let [adjusted-idx (cond-> idx (in-thread-macro? zloc context) (some-> dec (max 0))) | ||
zloc-after-idx (some-> zloc (nth-form (inc adjusted-idx)))] | ||
(if (and (or (nil? zloc-after-idx) (first-form-in-line? zloc-after-idx)) | ||
(> (index-of zloc) idx)) | ||
(> (index-of zloc) adjusted-idx)) | ||
(inner-indent zloc key 0 nil context) | ||
(list-indent zloc context))))) | ||
|
||
|
@@ -336,6 +363,9 @@ | |
(defmethod indenter-fn :block [sym context [_ idx]] | ||
(fn [zloc] (block-indent zloc sym idx context))) | ||
|
||
(defmethod indenter-fn :thread [_ _ _] | ||
(constantly nil)) | ||
|
||
Comment on lines
+366
to
+368
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to rethink how the indentation functions are applied, as the abstraction has clearly failed if we have an indenter function that does nothing. |
||
(defn- make-indenter [[key opts] context] | ||
(apply some-fn (map (partial indenter-fn key context) opts))) | ||
|
||
|
@@ -372,6 +402,13 @@ | |
(defn- find-namespace [zloc] | ||
(some-> zloc root z/down (z/find z/right ns-form?) z/down z/next z/sexpr)) | ||
|
||
(defn- get-thread-indents [indents] | ||
(->> indents | ||
(keep (fn [[k v]] | ||
(when-first [[_ idx] (filter (fn [[rule]] (= rule :thread)) v)] | ||
[k (or idx 2)]))) | ||
(into {}))) | ||
|
||
(defn indent | ||
([form] | ||
(indent form default-indents {})) | ||
|
@@ -384,7 +421,8 @@ | |
sorted-indents (sort-by indent-order indents) | ||
context (merge (select-keys opts [:function-arguments-indentation]) | ||
{:alias-map alias-map | ||
:ns-name ns-name})] | ||
:ns-name ns-name | ||
:threads (get-thread-indents indents)})] | ||
(transform form edit-all should-indent? | ||
#(indent-line % sorted-indents context))))) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
How does this differ from
index-of
?