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

Trying out Standard Clojure formatter #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .joyride/src/portal/format.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(ns portal.format
(:require
["path" :as path]
["vscode" :as vscode]))

(defn- get-workspace-folder []
(let [^js uri (-> vscode/workspace .-workspaceFolders (aget 0) .-uri)
fs-path (.-fsPath uri)]
(if-not (undefined? fs-path) fs-path (.-path uri))))

(defn- get-standard-npm-path []
(path/join (get-workspace-folder)
"node_modules"
"@chrisoakman/standard-clojure-style"
"dist/standard-clojure-style.js"))

(def ^:private standard (js/require (get-standard-npm-path)))

;; (js/require "@chrisoakman/standard-clojure-style")

;; ("./package.json")

;; (defn- require-string [src file-name]
;; (let [Module (js/require "module")
;; ^js m (Module. file-name (some-> js/module .-parent))]
;; (set! (.-filename m) file-name)
;; (._compile m src file-name)
;; (.-exports m)))

;; (defn require-url [url]
;; (-> (js/fetch url)
;; (.then #(.text %))
;; (.then #(require-string % "standard-clojure-style.js"))))

;; (defonce lib (atom nil))

;; (when-not @lib
;; (-> (require-url "https://raw.githubusercontent.com/oakmac/standard-clojure-style-js/refs/heads/master/lib/standard-clojure-style.js")
;; (.then #(reset! lib %))))

(defn formatter [model _options _token]
(let [code (.-out (.format standard (.getText model)))]
#js [(.replace vscode/TextEdit
(vscode.Range.
(.. (.lineAt model 0) -range -start)
(.. (.lineAt model (dec (.-lineCount model))) -range -end))
code)]))

(comment
(.registerDocumentFormattingEditProvider
vscode/languages "clojure"
#js {:displayName "Clojure Standard Formatter"
:provideDocumentFormattingEdits (fn [& args] (apply formatter args))})

(require '[portal.api :as p])
(p/open {:launcher :vs-code})
(add-tap #'p/submit)

,)
3 changes: 0 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
{:extra-paths ["dev"]
:main-opts ["-m" "tasks.kondo"]
:extra-deps {clj-kondo/clj-kondo {:mvn/version "2024.08.01"}}}
:cljfmt
{:main-opts ["-m" "cljfmt.main"]
:extra-deps {cljfmt/cljfmt {:mvn/version "0.9.2"}}}
:cider
{:extra-deps
{nrepl/nrepl {:mvn/version "1.3.0"}
Expand Down
2 changes: 1 addition & 1 deletion dev/cljs/user.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
(require '[examples.data :refer [data]])
(dotimes [_i 25] (tap> data))

comment)
comment)
13 changes: 7 additions & 6 deletions dev/notebook/ci.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
(ns notebook.ci
(:require [tasks.build :refer [build install]]
[tasks.check :as check]
[tasks.format :as fmt]
[tasks.parallel :refer [with-out-data]]
[tasks.test :as test]
[tasks.tools :as tool]))
(:require
[tasks.build :refer [build install]]
[tasks.check :as check]
[tasks.format :as fmt]
[tasks.parallel :refer [with-out-data]]
[tasks.test :as test]
[tasks.tools :as tool]))
(check/cloc)
(with-out-data (fmt/check))
(with-out-data (check/clj-kondo))
Expand Down
9 changes: 5 additions & 4 deletions dev/notebook/data.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns notebook.data
(:require [examples.data :as d]
[portal.colors :as c]
[portal.viewer :as v]))
(:require
[examples.data :as d]
[portal.colors :as c]
[portal.viewer :as v]))

(::d/hacker-news d/data)

Expand All @@ -24,7 +25,7 @@
(v/tree d/hiccup)

(v/hiccup
[:portal.viewer/markdown (::d/markdown d/string-data)])
[:portal.viewer/markdown (::d/markdown d/string-data)])

(-> d/exception-data)

Expand Down
89 changes: 45 additions & 44 deletions dev/notebook/scittle.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(ns notebook.scittle
(:require [clojure.java.browse :as browse]
[clojure.string :as str]
[hiccup.page :as page]
[portal.api :as p]
[portal.viewer :as v]))
(:require
[clojure.java.browse :as browse]
[clojure.string :as str]
[hiccup.page :as page]
[portal.api :as p]
[portal.viewer :as v]))

(def portal-dev (p/open {:mode :dev :launcher false}))

Expand All @@ -21,8 +22,8 @@
(defn div-and-script [id widget]
[[:div {:id id}]
(scittle-script
(list 'dom/render (list 'fn [] widget)
(list '.getElementById 'js/document id)))])
(list 'dom/render (list 'fn [] widget)
(list '.getElementById 'js/document id)))])

(defn pr-str-with-meta [value]
(binding [*print-meta* true]
Expand All @@ -33,51 +34,51 @@

(defn page [widgets]
(page/html5
[:head]
(into
[:body
(page/include-js "https://unpkg.com/react@18/umd/react.production.min.js"
"https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
"https://scicloj.github.io/scittle/js/scittle.js"
"https://scicloj.github.io/scittle/js/scittle.reagent.js"
(portal-main-url portal-dev))
(scittle-script '(ns main
(:require [reagent.core :as r]
[reagent.dom :as dom]))
'(defn portal-viewer [{:keys [edn-str]}]
(let [embed (js/portal_api.embed)]
[:div
[:div
{:ref (fn [el]
(.renderOutputItem embed
(clj->js {:mime "x-application/edn"
:theme "portal.colors/nord-light"
:text (fn [] edn-str)})
el))}]])))]
(->> widgets
(map-indexed (fn [i widget]
(div-and-script (str "widget" i)
widget)))
(apply concat)))))
[:head]
(into
[:body
(page/include-js "https://unpkg.com/react@18/umd/react.production.min.js"
"https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
"https://scicloj.github.io/scittle/js/scittle.js"
"https://scicloj.github.io/scittle/js/scittle.reagent.js"
(portal-main-url portal-dev))
(scittle-script '(ns main
(:require [reagent.core :as r]
[reagent.dom :as dom]))
'(defn portal-viewer [{:keys [edn-str]}]
(let [embed (js/portal_api.embed)]
[:div
[:div
{:ref (fn [el]
(.renderOutputItem embed
(clj->js {:mime "x-application/edn"
:theme "portal.colors/nord-light"
:text (fn [] edn-str)})
el))}]])))]
(->> widgets
(map-indexed (fn [i widget]
(div-and-script (str "widget" i)
widget)))
(apply concat)))))

(defn img [url]
(v/hiccup
[:img {:height 50 :width 50
:src url}]))
[:img {:height 50 :width 50
:src url}]))

(defn md [text]
(v/hiccup [:portal.viewer/markdown text]))

(defn vega-lite-point-plot [data]
(v/hiccup
[:portal.viewer/vega-lite
(-> {:data {:values data},
:mark "point"
:encoding
{:size {:field "w" :type "quantitative"}
:x {:field "x", :type "quantitative"},
:y {:field "y", :type "quantitative"},
:fill {:field "z", :type "nominal"}}})]))
[:portal.viewer/vega-lite
(-> {:data {:values data}
:mark "point"
:encoding
{:size {:field "w" :type "quantitative"}
:x {:field "x", :type "quantitative"}
:y {:field "y", :type "quantitative"}
:fill {:field "z", :type "nominal"}}})]))

(defn random-data [n]
(->> (repeatedly n #(- (rand) 0.5))
Expand Down Expand Up @@ -110,4 +111,4 @@
(map portal-widget)
page
(spit "target/scittle.html"))
(browse/browse-url "target/scittle.html"))
(browse/browse-url "target/scittle.html"))
15 changes: 8 additions & 7 deletions dev/portal/runtime/debug.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns portal.runtime.debug
(:require [portal.api :as p]
[portal.runtime :as rt]))
(:require
[portal.api :as p]
[portal.runtime :as rt]))

(defn- section [title value]
[(name title)
Expand Down Expand Up @@ -28,10 +29,10 @@

(defn open [session]
(p/inspect
(dashboard session)
(-> (:options session)
(dissoc :debug)
(assoc :window-title "portal-debug-server"))))
(dashboard session)
(-> (:options session)
(dissoc :debug)
(assoc :window-title "portal-debug-server"))))

(defn close [instance]
(when instance (p/close instance)))
(when instance (p/close instance)))
35 changes: 18 additions & 17 deletions dev/portal/setup.cljs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
(ns portal.setup
(:require [clojure.datafy :refer [datafy]]
[examples.data :refer [data]]
[portal.client.web :as remote]
[portal.console :as log]
[portal.runtime :as rt]
#_[portal.shadow.remote :as remote]
[portal.shortcuts :as shortcuts]
[portal.ui.api :as api]
[portal.ui.commands :as commands]
[portal.ui.inspector :as ins]
[portal.ui.rpc :as rpc]
[portal.ui.select :as select]
[portal.ui.state :as state]
[portal.web :as p]))
(:require
[clojure.datafy :refer [datafy]]
[examples.data :refer [data]]
[portal.client.web :as remote]
[portal.console :as log]
[portal.runtime :as rt]
#_[portal.shadow.remote :as remote]
[portal.shortcuts :as shortcuts]
[portal.ui.api :as api]
[portal.ui.commands :as commands]
[portal.ui.inspector :as ins]
[portal.ui.rpc :as rpc]
[portal.ui.select :as select]
[portal.ui.state :as state]
[portal.web :as p]))

(defonce tap-list
(atom (with-meta (list)
Expand Down Expand Up @@ -89,9 +90,9 @@
(section "Selection Index" select/selection-index)]]})

(p/set-defaults!
{:mode :dev
:value (dashboard)
:window-title "portal-ui-runtime"})
{:mode :dev
:value (dashboard)
:window-title "portal-ui-runtime"})

(defn- error-handler [event]
#_(tap> (or (.-error event) (.-reason event)))
Expand Down
36 changes: 19 additions & 17 deletions dev/portal/share.clj
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
(ns portal.share
(:require [clojure.pprint :as pp]
[clojure.string :as str]
[org.httpkit.client :as http]
[portal.api :as p]
[portal.runtime.json :as json])
(:import [java.net URL]))
(:require
[clojure.pprint :as pp]
[clojure.string :as str]
[org.httpkit.client :as http]
[portal.api :as p]
[portal.runtime.json :as json])
(:import
(java.net URL)))

(defn- pprint [value]
(binding [*print-meta* true]
(with-out-str (pp/pprint value))))

(defn- create-gist [content]
(-> @(http/post
"https://api.github.com/gists"
{:basic-auth [(System/getenv "GIT_USERNAME")
(System/getenv "GIT_PASSWORD")]
:headers {"Accept" "application/vnd.github.v3+json"}
:body (json/write
{:public false
:description "Portal Share"
:files {"data.clj" {:content content}}})})
"https://api.github.com/gists"
{:basic-auth [(System/getenv "GIT_USERNAME")
(System/getenv "GIT_PASSWORD")]
:headers {"Accept" "application/vnd.github.v3+json"}
:body (json/write
{:public false
:description "Portal Share"
:files {"data.clj" {:content content}}})})
:body
json/read
(get-in [:files :data.clj :raw_url])))
Expand All @@ -34,9 +36,9 @@

(defn- shorten-url [url]
(-> @(http/post
"https://url.api.stdlib.com/[email protected]/create/"
{:headers {"Content-Type" "application/json"}
:body (json/write {:url url :ttl 86400})})
"https://url.api.stdlib.com/[email protected]/create/"
{:headers {"Content-Type" "application/json"}
:body (json/write {:url url :ttl 86400})})
:body
json/read
:link_url))
Expand Down
3 changes: 2 additions & 1 deletion dev/reveal.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns reveal
(:require [vlaaad.reveal :as r]))
(:require
[vlaaad.reveal :as r]))

(comment (r/tap-log))
Loading
Loading