-
Notifications
You must be signed in to change notification settings - Fork 23
/
newdoc.ts
116 lines (105 loc) · 2.91 KB
/
newdoc.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
/*
* newdoc.ts
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
import {
workspace,
window,
commands,
NotebookData,
NotebookCellData,
NotebookCellKind,
WorkspaceEdit,
ViewColumn,
} from "vscode";
import { Command } from "../core/command";
import { getWholeRange, kQuartoLanguageId } from "../core/doc";
export function newDocumentCommands(): Command[] {
return [
new NewDocumentCommand("quarto.newDocument"),
new NewDocumentCommand("quarto.fileNewDocument"),
new NewPresentationCommand("quarto.newPresentation"),
new NewNotebookCommand("quarto.newNotebook"),
];
}
class NewNotebookCommand implements Command {
public readonly id: string;
constructor(cmdId: string) {
this.id = cmdId;
}
async execute(): Promise<void> {
const cells: NotebookCellData[] = [];
cells.push(
new NotebookCellData(
NotebookCellKind.Code,
kUntitledHtml.trimEnd(),
"raw"
)
);
cells.push(new NotebookCellData(NotebookCellKind.Code, "1 + 1", "python"));
const nbData = new NotebookData(cells);
let notebook = await workspace.openNotebookDocument(
"jupyter-notebook",
nbData
);
await commands.executeCommand(
"vscode.openWith",
notebook.uri,
"jupyter-notebook"
);
const cell = notebook.cellAt(1);
const edit = new WorkspaceEdit();
edit.replace(cell.document.uri, getWholeRange(cell.document), "");
await workspace.applyEdit(edit);
}
}
abstract class NewFileCommand implements Command {
public readonly id: string;
constructor(cmdId: string, private readonly viewColumn_?: ViewColumn) {
this.id = cmdId;
}
async execute(): Promise<void> {
const doc = await workspace.openTextDocument({
language: kQuartoLanguageId,
content: this.scaffold(),
});
await window.showTextDocument(doc, this.viewColumn_, false);
await commands.executeCommand("cursorMove", { to: "viewPortBottom" });
}
protected abstract scaffold(): string;
}
class NewDocumentCommand extends NewFileCommand {
constructor(cmdId: string) {
super(cmdId);
}
protected scaffold(): string {
return kUntitledHtml;
}
}
class NewPresentationCommand extends NewFileCommand {
constructor(cmdId: string) {
super(cmdId);
}
protected scaffold(): string {
return `---
title: "Untitled"
format: revealjs
---
`;
}
}
const kUntitledHtml = `---
title: "Untitled"
format: html
---
`;