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

WIP: Add webview output experience #2481

Draft
wants to merge 68 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
17ad745
Add repl output ns
bpringe Aug 20, 2023
c9b5334
Merge branch 'dev' into node-library_target_inject_vscode
bpringe Sep 3, 2023
514f9df
Add dev code
bpringe Sep 3, 2023
5871dfa
Add cljs code to get project root uri
bpringe Sep 3, 2023
6ec215d
Add function for creating repl output file (markdown)
bpringe Sep 3, 2023
3af8dfd
Add more markdown experimentation code
bpringe Nov 19, 2023
81303d5
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Nov 19, 2023
84f45e5
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Nov 20, 2023
26c6dad
Create webview-output ns - WIP
bpringe Nov 21, 2023
a193bcc
Use html file for webview html
bpringe Nov 21, 2023
756d366
Add highlight.js
bpringe Nov 21, 2023
56087e9
Begin setup for using reagent in output webview - WIP
bpringe Nov 22, 2023
94d73dc
Add reagent UI for repl output - WIP
bpringe Nov 24, 2023
f97c478
Make reagent UI work
bpringe Nov 25, 2023
0ad4652
Make posting results as messages work
bpringe Nov 25, 2023
06953ce
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Dec 4, 2023
45a629a
Add build to watch-cljs script
bpringe Dec 4, 2023
695afde
Open webview output on activation and print 10 messages
bpringe Dec 4, 2023
65f4229
Fix comment typo
bpringe Feb 26, 2024
2f85dff
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Feb 26, 2024
e005870
Update package-lock.json
bpringe Feb 26, 2024
d50b123
Remove comment form
bpringe Feb 26, 2024
5386de6
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Mar 31, 2024
11dfb42
Remove markdown output file
bpringe Mar 31, 2024
202329f
Fix project-root-uri-key
bpringe Mar 31, 2024
abe1a17
Get rid of react warning about every element needing a key
bpringe Mar 31, 2024
53a3718
Use create-root so that react 18 is used
bpringe Mar 31, 2024
18803b4
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Apr 6, 2024
f71343f
WIP: Update webview CSP
bpringe Apr 6, 2024
3d00c9a
Move hightlightAll call to cljs
bpringe Apr 6, 2024
4eb3975
Add comment
bpringe Apr 6, 2024
23022f6
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Apr 20, 2024
2c60af5
Add replicant
bpringe Apr 20, 2024
5be9a7d
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe May 5, 2024
8f9eca9
Add replicant and set up a simple multimethod command system
bpringe May 5, 2024
77e87f8
Fix typo
bpringe May 5, 2024
af177c1
Update deps-clj-version and deps.clj.jar
bpringe May 19, 2024
45ea2d3
Remove react and reagent deps, move snitch dep to dev alias, refactor…
bpringe May 19, 2024
32c27e7
Add multimethod for getting hiccup for output element
bpringe May 19, 2024
818303d
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Jan 29, 2025
1149007
Update deps.clj and package-lock
bpringe Jan 29, 2025
d517650
Remove empty line
bpringe Jan 29, 2025
6a7e355
Merge pull request #2717 from BetterThanTomorrow/test_branch_ignore
bpringe Jan 29, 2025
a0ca735
Add comment
bpringe Jan 29, 2025
bad6237
Merge pull request #2718 from BetterThanTomorrow/test_branch_2_ignore
bpringe Jan 29, 2025
015d564
Add docstring
bpringe Jan 29, 2025
ae5b6f0
Add docstring
bpringe Jan 29, 2025
450fc7d
Merge pull request #2719 from BetterThanTomorrow/test_branch_3_ignore
bpringe Jan 29, 2025
b9cfea1
Add webview option to outputDestinations in config
bpringe Jan 30, 2025
c161b59
Merge remote-tracking branch 'origin/add_new_output_experience' into …
bpringe Jan 30, 2025
80cc022
WIP: Make show/open output destination command work with webview
bpringe Jan 31, 2025
249b491
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Feb 1, 2025
b5931c9
Fix error caused by accessing `document` on `undefined`
bpringe Feb 1, 2025
73c31c8
Send stdout messages from evaluations to webview
bpringe Feb 2, 2025
62abbca
Show eval results in webview
bpringe Feb 4, 2025
76d6f3c
Show stderr in webview.
bpringe Feb 4, 2025
53f1b6c
Show otherError output in webview
bpringe Feb 4, 2025
5780f1c
Make webview show on repl connect when it's hidden.
bpringe Feb 4, 2025
a1ad0da
Print stacktraces to webview when they occur
bpringe Feb 5, 2025
618a354
Update printLastStacktrace command title
bpringe Feb 5, 2025
5ed14a2
Update printLastStacktrace command title
bpringe Feb 5, 2025
f543523
Refactor
bpringe Feb 6, 2025
6afa6be
Update package.json scripts and .vscodeignore to handle repl-output-u…
bpringe Feb 6, 2025
ecb221d
Print stacktraces to output channel when print last stacktrace comman…
bpringe Feb 6, 2025
80faaf0
Update print last stacktrace command to print to terminal
bpringe Feb 6, 2025
9eb393e
WIP: Add command to clear repl output webview
bpringe Feb 6, 2025
bfbf9f0
Add command to clear webview
bpringe Feb 6, 2025
7a6d830
Refactor for better reloadability
bpringe Feb 8, 2025
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/.clj-kondo
/site
/test-data
/repl-output-ui
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jspm_packages
lib/
cljs-out/
test-out/
repl-output-ui/

# This and that
.nrepl-port
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
**/.shadow-cljs/
**/out/
clojure.tmLanguage.json
/repl-output-ui
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
org.clojars.liverm0r/dartclojure {:mvn/version "0.2.22-SNAPSHOT"}
vvvvalvalval/supdate {:mvn/version "0.2.3"}
camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.3"}
reagent/reagent {:mvn/version "1.2.0"}
#_#_org.clojars.liverm0r/dartclojure {:local/root "../DartClojure"}}
:paths ["src/cljs-lib/src"
"src/cljs-lib/test"]}
"src/cljs-lib/test"]}
84 changes: 80 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3212,7 +3212,7 @@
"compile-ts": "npx tsc --project ./tsconfig.json",
"compile": "npm run compile-cljs && npm run compile-ts",
"watch-ts": "npx tsc --watch --project ./tsconfig.json",
"watch-cljs": "npx shadow-cljs -d cider/cider-nrepl:0.28.5 watch :calva-lib :test",
"watch-cljs": "npx shadow-cljs -d cider/cider-nrepl:0.28.5 watch :calva-lib :test :repl-output-ui",
"watch-ts-with-strict-nulls": "npx tsc --watch --project ./tsconfig.json --strictNullChecks",
"release-cljs": "npx shadow-cljs release :calva-lib :test",
"release": "webpack --mode production",
Expand Down Expand Up @@ -3254,6 +3254,7 @@
"open": "^6.3.0",
"parinfer": "^3.12.0",
"posthtml-parser": "^0.11.0",
"react-dom": "^18.2.0",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know if we need React for something in particular. If we do, then of course we should use it. Otherwise, it may be better to use something with less bloat, like Dumdom, or maybe Replicant.

Copy link
Member Author

@bpringe bpringe Apr 5, 2024

Choose a reason for hiding this comment

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

It's for reagent, which I consider very lightweight and should be used lightly for this. If you think there's any performance issue or other issues with it that would be significant for our use-case, we should discuss that, but switching it out later should be pretty simple.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm open to alternatives if there's a good reason to use something else.

Copy link
Member Author

@bpringe bpringe Apr 5, 2024

Choose a reason for hiding this comment

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

Dumdom looks interesting. Using a library that supports react gives us reach into the react ecosystem though, which could be nice in the future for more rich features and interactivity (making it less likely we'll need to recreate the wheel).

Copy link
Collaborator

Choose a reason for hiding this comment

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

My experience with React is that it changes all the time in breaking ways, making it high maintenance. With Dumdom we would avoid this, as the vdom library it uses is super tiny and stable. With Replicant we would get a pure ClojureScript vdom as well, and have even more of the familiar Clojure stability (though I am not sure if the Replicant API is marked as stable yet).

However, I am not opposed to React if we have a good reason to use it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I say we move forward with it for a first-release, and maybe be conscious of our usage of it (keeping it light), and if we come across issues we can switch it out later.

I'll continue to keep other options in mind and think about the React usage for now...

Copy link
Member Author

Choose a reason for hiding this comment

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

I say we move forward with it for a first-release

I may reconsider this. I need to look at Dumdom more closely.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Having had a closer look at Replicant, In think it's better to use that instead of Dumdom. They are quite similar, but Replicant drops the dependency on Snabbdom, as well as some legacy API requirements that Dumdom carries.

"semver": "^7.3.7",
"tree-kill": "^1.2.2",
"uuidv4": "6.2.12",
Expand Down
14 changes: 12 additions & 2 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@
:js2cljs calva.js2cljs.converter/convert-bridge
:dart2clj calva.dartclojure/convert-bridge
:readConfigEdn calva.read-config/config-edn->js-bridge
:html2hiccup calva.html2hiccup/html->hiccup-convert-bridge}
:html2hiccup calva.html2hiccup/html->hiccup-convert-bridge
:initializeCljs calva.util/initialize-cljs
:showReplOutputWebviewPanel calva.repl.webview.core/show-repl-output-webview-panel}
:output-to "out/cljs-lib/cljs-lib.js"}
:test
{:target :node-test
:output-to "out/cljs-lib/test/cljs-lib-tests.js"
:ns-regexp "-test$"
:autorun true}}}
:autorun true}
:repl-output-ui
{:target :browser
;; TODO: Do the asset-path and output-dir values make sense?
:asset-path "js"
:output-dir "repl-output-ui/js"
:modules {:main {:init-fn calva.repl.webview.ui/main}}
:devtools {:loader-mode :eval
:devtools-url "http://localhost:9630"}}}}
107 changes: 107 additions & 0 deletions src/cljs-lib/src/calva/repl/webview/core.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
(ns calva.repl.webview.core
(:require
[calva.util :as util]))

(defonce repl-output-webview-panel (atom nil))

(defn dispose-repl-output-webview-panel []
(println "Disposing repl-output-webview-panel")
(reset! repl-output-webview-panel nil))

;; TODO: See if there's a way to not have to use ^js in so many places without shadow-cljs warnings
(defn create-or-get-repl-output-webview-panel []
(or @repl-output-webview-panel
(let [webview-panel (.. ^js @util/vscode -window
(createWebviewPanel "calva:repl-output"
"REPL Output"
(.. ^js @util/vscode -ViewColumn -Two)
#js {:enableScripts true}))]
(.. ^js webview-panel (onDidDispose dispose-repl-output-webview-panel))
(reset! repl-output-webview-panel webview-panel))))

(defn get-webview-html
[js-src]
(str "
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\" />

<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />

<!-- TODO: Uncomment this and lock it down as much as possible. Remember to disable things that default-src does not. See bottomg of this section: https://web.dev/articles/csp#resource-options -->
<!-- <meta http-equiv=\"Content-Security-Policy\"
content=\"default-src 'none';
style-src https://cdnjs.cloudflare.com;
script-src https://cdnjs.cloudflare.com;\"> -->

<title>REPL Output</title>

<link
rel=\"stylesheet\"
href=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css\"
/>

<script src=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js\"></script>
<script src=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/clojure.min.js\"></script>

</head>
<body>
<pre><code class=\"language-clojure\">:hello-world</code></pre>

<!-- TODO: Disable inline scripts - see security section in webview docs -->
<div id=\"output\"></div>

<script src=\"" js-src "\"></script>
</body>
</html>"))

(defn post-message-to-webview [message]
(.. ^js @repl-output-webview-panel
-webview
(postMessage (clj->js (merge
{:id (str (random-uuid))} ;; Provide an id if one wasn't provided by the caller
message)))))

(defn show-repl-output-webview-panel []
(let [^js repl-output-webview-panel (create-or-get-repl-output-webview-panel)
js-path (.. ^js @util/vscode
-Uri
(joinPath (.. ^js @util/context -extensionUri) "repl-output-ui" "js" "main.js"))
js-src (.. repl-output-webview-panel -webview (asWebviewUri js-path))
webview-html (get-webview-html js-src)]
(set! (.. ^js repl-output-webview-panel -webview -html) webview-html)
(let [interval-id (js/setInterval post-message-to-webview
1000
{:command-name "show-result"
:result "Hello world!!!"})]
(js/setTimeout #(js/clearInterval interval-id)
11000))))

;; TODO: See if can send repl output to webview when it's hidden and see it once unhidden
;; "You cannot send messages to a hidden webview, even when retainContextWhenHidden is enabled."
;; https://code.visualstudio.com/api/extension-guides/webview#theming-webview-content

(comment
(show-repl-output-webview-panel)

;; TODO: Implement this interface for communicating with the webview
;; Message
{;; This message contains a command
:command {;; Command name
:name "show-result"
;; Command args
:args {:result "Hello world"}}
;; Message id
:id "1234"}

(post-message-to-webview {:command-name "show-result"
:result "send while hidden"})

(post-message-to-webview {:command "clear-output"})

@repl-output-webview-panel

;; TODO: Don't worry about scrolling yet. We know we can do that. Explore other important unknowns first.

:rcf)
Loading