-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.ts
141 lines (121 loc) · 5.76 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as vscode from "vscode"
import { PlutoBackend } from "./backend"
import { PlutoEditor } from "./PlutoEditor"
import { start_empty_notebook_app, start_notebook_file_app } from "./app_engine"
import { TextDecoder, TextEncoder } from "util"
import { v4 as uuid } from "uuid"
// this is a commit on the vscode-webview-proxy branch, see https://github.com/fonsp/Pluto.jl/pull/1493
export const PLUTO_BRANCH_NAME = "376b08851906afc9e52e2c09e823de8d7e656675"
/*
HELLO
This file is the entry point of the extension. The important function here is `new_notebook`.
*/
export function activate(context: vscode.ExtensionContext) {
console.log("Activating extension pluto-vscode")
context.subscriptions.push(PlutoEditor.register(context))
context.subscriptions.push(
vscode.commands.registerCommand("pluto.editor.start", () => {
// THE ONLY WAY I WAS ABLE TO DO THIS IS
// ask the user for the file name IN ADVANCE, then write an empty notebook there, then open it
// ugh
vscode.window
.showSaveDialog({
// TODO: initialize with a cute filename
filters: {
Julia: [".jl"],
},
})
.then(async (path) => {
// TODO: generate a temporary file(?) if none was given by the user
// let path = path ?? vscode.Uri.parse("untitled:untitled-1.jl")
if (path) {
await vscode.workspace.fs.writeFile(path, new TextEncoder().encode(empty_notebook_contents()))
vscode.commands.executeCommand("vscode.openWith", path, "pluto.editor")
}
})
// OTHER ATTEMPS
// THIS ONE almost works, but when you do the first Ctrl+S, it does not automatically add the .jl extension
// const filename = vscode.Uri.parse("untitled:untitled-1.jl")
// vscode.workspace.fs.writeFile(filename, new TextEncoder().encode(empty_notebook_contents())).then(() => {
// vscode.commands.executeCommand("vscode.openWith", filename, "pluto.editor")
// })
// ALSO CLOSE and the most official, but it opens the window twice, once in Pluto, once in a text editor.
// vscode.workspace
// .openTextDocument({
// content: empty_notebook_contents(),
// language: "julia",
// })
// .then(async (document) => {
// const to_close = vscode.workspace.textDocuments.filter((d) => d === document)
// await vscode.commands.executeCommand("vscode.openWith", document.uri, "pluto.editor")
// // vs code already opens a regular .jl text editor, we should manually close that...
// // TODO: this gives a ...are you sure... popup :(((
// for (const doc of to_close) {
// console.error("closing!!!")
// await vscode.window.showTextDocument(doc)
// await vscode.commands.executeCommand("workbench.action.closeActiveEditor")
// }
// })
// ORIGINAL: opens a new notebook, but as a webview, not as an editor
// start_empty_notebook_app(context)
})
)
context.subscriptions.push(
vscode.commands.registerCommand("pluto.editor.openCurrentWith", (selectedDocumentURI) => {
vscode.commands.executeCommand("vscode.openWith", selectedDocumentURI, "pluto.editor")
})
)
context.subscriptions.push(
vscode.commands.registerCommand("pluto.appEngine.newNotebook", () => {
start_empty_notebook_app(context)
})
)
context.subscriptions.push(
vscode.commands.registerCommand("pluto.appEngine.openNotebook", async (documentURI, isolated_cell_ids = undefined) => {
start_notebook_file_app(context, {
notebook_file_contents: new TextDecoder().decode(await vscode.workspace.fs.readFile(documentURI)),
isolated_cell_ids: isolated_cell_ids,
})
})
)
/** This will be available as our `.exports` when this extension is used by another extension, see https://code.visualstudio.com/api/references/vscode-api#extensions. */
const api = {
version: 1,
runNotebookApp: (args: { notebook_file_contents: string; isolated_cell_ids?: string[]; disable_ui?: boolean;[_ignored: string]: any }) =>
start_notebook_file_app(context, args),
}
// let cool_nb =
// '### A Pluto.jl notebook ###\n# v0.17.1\n\nusing Markdown\nusing InteractiveUtils\n\n# ╔═╡ 3d0fb1de-8e96-4eed-8563-c91de4786001\n"show me!!!"\n\n# ╔═╡ 3d0fb1de-8e96-4eed-8563-c91de4786002\ndont\' show me\n\n# ╔═╡ Cell order:\n# ╠═3d0fb1de-8e96-4eed-8563-c91de4786001\n# ╠═3d0fb1de-8e96-4eed-8563-c91de4786002'
// let cool_nb_cell_id = "3d0fb1de-8e96-4eed-8563-c91de4786001"
// api.runNotebookApp({
// notebook_file_contents: cool_nb,
// isolated_cell_ids: [cool_nb_cell_id],
// disable_ui: true,
// })
return api
}
export const LOADING_HTML = `<!DOCTYPE html>
<html lang="en">
<head>
<title>Loading Pluto...</title>
</head>
<body style="overflow: hidden;">
<h1>Loading Pluto...</h1>
</body>
</html>
`
// this method is called when your extension is deactivated
export function deactivate() {
PlutoBackend.deactivate()
}
export const empty_notebook_contents = () => {
let id = uuid()
return `### A Pluto.jl notebook ###
# v0.17.1
using Markdown
using InteractiveUtils
# ╔═╡ ${id}
i'm new here!
# ╔═╡ Cell order:
# ╠═${id}`
}