Skip to content

Commit

Permalink
move to dot named parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
D00mch committed Jul 15, 2022
1 parent 742725e commit 4598a6a
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 74 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ Center(

```clojure
(m/Center
:child (m/Text
.child (m/Text
(if _active "Active" "Inactive")
:style (m/TextStyle :fontSize 32.0 :color m.Colors/white)))
.style (m/TextStyle .fontSize 32.0 .color m.Colors/white)))
```

3 more examples:
Expand Down Expand Up @@ -137,13 +137,13 @@ Add Cli/deps:
```clojure
{:deps
{
org.clojars.liverm0r/dartclojure {:mvn/version "0.2.10-SNAPSHOT"}
org.clojars.liverm0r/dartclojure {:mvn/version "0.2.11-SNAPSHOT"}
}}
```

Or Leiningen/Boot:
```clojure
[org.clojars.liverm0r/dartclojure "0.2.10-SNAPSHOT"]
[org.clojars.liverm0r/dartclojure "0.2.11-SNAPSHOT"]
```

Convert dart code (simplify and wrap-nest under the hood):
Expand All @@ -164,7 +164,7 @@ If you just need to wrap clojure code with nest:
'[dumch.improve :as impr]
'[rewrite-clj.zip :as z])

(-> "(Container :child (Box :child (Padding :child (:Text \"2\"))))"
(-> "(Container .child (Box .child (Padding .child (:Text \"2\"))))"
z/of-string
impr/wrap-nest
z/sexpr)
Expand All @@ -175,8 +175,8 @@ If you just need to wrap clojure code with nest:

```bash
clojure -Sdeps \
'{:deps {org.clojars.liverm0r/dartclojure {:mvn/version "0.2.10-SNAPSHOT"}}}' \
-e "(require '[dumch.convert :refer [convert]]) (convert \"Text('1')\" :material \"m\" :flutter \"f\")"
'{:deps {org.clojars.liverm0r/dartclojure {:mvn/version "0.2.11-SNAPSHOT"}}}' \
-e "(require '[dumch.convert :refer [convert]]) (convert \"Text('1')\" .material \"m\" .flutter \"f\")"
```

### API with Jar
Expand All @@ -189,7 +189,7 @@ $ java -jar dartclj.jar "Divider(
color: Colors.grey,
)"
(m/Divider :height 0 :color m.Colors/grey)
(m/Divider .height 0 .color m.Colors/grey)
```

Second one is to run repl-like console (and send code into it with your editor/idea):
Expand All @@ -202,7 +202,7 @@ Divider(
color: Colors.grey,
)
(material/Divider :height 0 :color material.Colors/grey)
(material/Divider .height 0 .color material.Colors/grey)
```

Colors are also supported:
Expand Down Expand Up @@ -317,9 +317,9 @@ See the License for the specific language governing permissions and
limitations under the License.

[1]: https://github.com/Tensegritics/ClojureDart/blob/main/doc/flutter-helpers.md#widget-macro
[2]: https://clojars.org/org.clojars.liverm0r/dartclojure/versions/0.2.10-SNAPSHOT
[2]: https://clojars.org/org.clojars.liverm0r/dartclojure/versions/0.2.11-SNAPSHOT
[3]: https://www.jetbrains.com/help/idea/configuring-third-party-tools.html
[4]: https://github.com/Liverm0r/DartClojure/releases/tag/0.2.10
[4]: https://github.com/Liverm0r/DartClojure/releases/tag/0.2.11
[5]: https://calva.io/clojuredart/
[6]: https://marketplace.visualstudio.com/items?itemName=betterthantomorrow.calva
[7]: https://www.reddit.com/r/Clojure/comments/vib5ie/how_to_translate_dart_to_clojuredart_inside/
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dartclojure",
"description": "Opinionated Dart to Clojure converter for Flutter widgets.",
"version": "0.2.10",
"version": "0.2.11",
"publisher": "liverm0r",
"author": {
"name": "Artur Dumchev",
Expand All @@ -26,6 +26,6 @@
"shadow-cljs": "^2.19.3"
},
"dependencies": {
"dartclojure": "^0.2.10"
"dartclojure": "^0.2.11"
}
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.clojars.liverm0r</groupId>
<artifactId>dartclojure</artifactId>
<version>0.2.10-SNAPSHOT</version>
<version>0.2.11-SNAPSHOT</version>
<name>dumch/dartclojure</name>
<description>Opinionated Dart->Clojure translater</description>
<url>https://github.com/Liverm0r/dartclojure</url>
Expand All @@ -22,7 +22,7 @@
<url>https://github.com/Liverm0r/dartclojure</url>
<connection>scm:git:git://github.com/Liverm0r/dartclojure.git</connection>
<developerConnection>scm:git:ssh://[email protected]/Liverm0r/dartclojure.git</developerConnection>
<tag>v0.2.10-SNAPSHOT</tag>
<tag>v0.2.11-SNAPSHOT</tag>
</scm>
<dependencies>
<dependency>
Expand Down
42 changes: 21 additions & 21 deletions src/dumch/improve.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
[rewrite-clj.zip :as z]))

(defn- remove-child-named-arg [zloc]
(-> zloc z/down (z/find-value z/right :child) z/right z/remove z/remove* z/up))
(-> zloc z/down (z/find-value z/right '.child) z/right z/remove z/remove* z/up))

(defn- nested-children? [zloc times]
(let [zloc (-> zloc z/down (z/find-value z/right :child))]
(let [zloc (-> zloc z/down (z/find-value z/right '.child))]
(cond
(not= :child (z/sexpr zloc)) false
(not= '.child (z/sexpr zloc)) false
(= 1 times) true
:else (recur (-> zloc z/next) (dec times)))))

(defn- nest-flatten [zloc & {fl :flutter :or {fl "f"}}]
(loop [zloc zloc
rslt []]
(if-let [child (-> zloc z/down (z/find-value z/right :child))]
(if-let [child (-> zloc z/down (z/find-value z/right '.child))]
(recur (-> child z/right) (conj rslt (remove-child-named-arg zloc)))
(z/edn
(n/list-node (lists* (n/token-node (symbol (str (when fl (str fl "/")) "nest")))
Expand Down Expand Up @@ -90,44 +90,44 @@

(comment
(def nested "(Container
:child
(Box :child (Padding :child (Text 1)) :a 1))")
.child
(Box .child (Padding .child (Text 1)) .a 1))")

(-> nested z/of-string (nested-children? 3))

(-> nested z/of-string
z/down
(z/find-value :child)
(z/find-value '.child)
z/right
p/raise
z/up
z/string
)

(def animation "(Column
:children
.children
[(Text \"name\")
(Icon m.Icons/widgets)
(IgnorePointer.
:ignoring (boolean @open)
:child
.ignoring (boolean @open)
.child
(AnimatedContainer.
:transformAlignment m.Alignment/center
:transform (m.Matrix4/diagonal3Values
.transformAlignment m.Alignment/center
.transform (m.Matrix4/diagonal3Values
(if @open 0.7 1.0)
(if @open 0.7 1.0)
1.0)
:duration ^:const (Duration. :milliseconds 250)
:curve ^:const (Interval. 0.0 0.5 :curve m.Curves/easeOut)
:child
.duration ^:const (Duration. .milliseconds 250)
.curve ^:const (Interval. 0.0 0.5 .curve m.Curves/easeOut)
.child
(AnimatedOpacity.
:opacity (if @open 0.0 (+ (+ 2.0 1.0) (+ -3.0) 1.0))
:curve ^:const (Interval. 0.25 1.0 :curve m.Curves/easeInOut)
:duration ^:const (Duration. :milliseconds 250)
:child
.opacity (if @open 0.0 (+ (+ 2.0 1.0) (+ -3.0) 1.0))
.curve ^:const (Interval. 0.25 1.0 .curve m.Curves/easeInOut)
.duration ^:const (Duration. .milliseconds 250)
.child
(FloatingActionButton.
:onPressed toggle
:child
.onPressed toggle
.child
(Icon. m.Icons/create)))))])
")

Expand Down
2 changes: 1 addition & 1 deletion src/dumch/parse.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@
:constructor (lnode (list* (ast->clj v1) ws (ast->clj v2)))
:params (mapcats ast->clj (rest node))
:argument (if v2 (map ast->clj (rest node)) [(ast->clj v1)])
:named-arg (knode (keyword (ast->clj v1)))
:named-arg (tnode (symbol (str "." (ast->clj v1))))

:dot (flatten-dot node ast->clj)
:dot-op (flatten-dot node ast->clj)
Expand Down
24 changes: 12 additions & 12 deletions test/dumch/improve_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
(def ^:private simplify-clj-str (comp z/sexpr simplify p/parse-string))

(deftest nesting-test
(testing "3 nested :child should be replaced with f/nest macro"
(testing "3 nested .child should be replaced with f/nest macro"
(is
(= (->
"(Container
:child
(Box :child (Padding :child (Text \"2\"))))"
.child
(Box .child (Padding .child (Text \"2\"))))"
z/of-string
wrap-nest
z/sexpr)
'(f/nest (Container) (Box) (Padding) (Text "2")))))

(testing "2- nested :child should be unchanged"
(testing "2- nested .child should be unchanged"
(is
(= (-> "(Box :child (Padding :child (Text \"2\")))"
(= (-> "(Box .child (Padding .child (Text \"2\")))"
z/of-string
wrap-nest
z/sexpr)
'(Box :child (Padding :child (Text "2")))))))
'(Box .child (Padding .child (Text "2")))))))

(deftest redundant-do
(testing "fn with do body"
Expand All @@ -42,11 +42,11 @@

(deftest material-alias-test
(testing "No alias on core"
(is (= (simplify-clj-str "(Duration :milliseconds 250)")
'(Duration :milliseconds 250))))
(is (= (simplify-clj-str "(Duration .milliseconds 250)")
'(Duration .milliseconds 250))))
(testing "material alias with slash"
(is (= (simplify-clj-str "(MyDuration :milliseconds 250)")
'(m/MyDuration :milliseconds 250))))
(is (= (simplify-clj-str "(MyDuration .milliseconds 250)")
'(m/MyDuration .milliseconds 250))))
(testing "material alias with dot"
(is (= (simplify-clj-str "(My.Duration :milliseconds 250)")
'(m.My/Duration :milliseconds 250)))))
(is (= (simplify-clj-str "(My.Duration .milliseconds 250)")
'(m.My/Duration .milliseconds 250)))))
24 changes: 12 additions & 12 deletions test/dumch/integration_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,26 @@ AnimatedContainer(
(read-string (convert code))
'(f/nest
(m/AnimatedContainer
:transformAlignment (.-center m/Alignment)
:transform (.diagonal3Values
.transformAlignment (.-center m/Alignment)
.transform (.diagonal3Values
m/Matrix4
(if _isOpened 0.7 1.0)
(if _isOpened 0.7 1.0)
1.0)
:duration (Duration :milliseconds 250)
:curve (m/Interval 0.0 0.5 :curve (.-easeOut m/Curves)))
.duration (Duration .milliseconds 250)
.curve (m/Interval 0.0 0.5 .curve (.-easeOut m/Curves)))
(m/AnimatedRotation
:turns (if _isOpened -0.1 0)
:curve (m/Interval 0.25 1.0 :curve (.-easeInOut m/Curves))
:duration (Duration :milliseconds 250))
.turns (if _isOpened -0.1 0)
.curve (m/Interval 0.25 1.0 .curve (.-easeInOut m/Curves))
.duration (Duration .milliseconds 250))
(m/FloatingActionButton
:onPressed
.onPressed
(fn [] :unidiomatic)
:backgroundColor
.backgroundColor
(if _isOpened (.-white m/Colors) (.-primaryColor theme)))
(m/Icon
(.-add m/Icons)
:color (if _isOpened (.-primaryColor theme) (.-white m/Colors))))))))
.color (if _isOpened (.-primaryColor theme) (.-white m/Colors))))))))

(def code2 "
Column(
Expand All @@ -82,10 +82,10 @@ Column(
(=
(-> code2 convert read-string)
'(m/Column
:children
.children
[(m/Question (get (get questions _questionIndex) "questionText"))
(->
(get (get questions _questionIndex) "answers")
(.map (fn [answer] (m/Answer _answerQuestion answer)))
.toList)
(m/Numb :sorted (and (= a b) (< 1 2 3 a)))])))))
(m/Numb .sorted (and (= a b) (< 1 2 3 a)))])))))
Loading

0 comments on commit 4598a6a

Please sign in to comment.