-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathopenNewEditor.ts
More file actions
123 lines (106 loc) · 3.25 KB
/
Copy pathopenNewEditor.ts
File metadata and controls
123 lines (106 loc) · 3.25 KB
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
import * as vscode from "vscode";
import { getParseTreeApi } from "../getExtensionApi";
interface NewEditorOptions {
languageId?: string;
openBeside?: boolean;
}
export async function openNewEditor(
content: string,
{ languageId = "plaintext", openBeside = false }: NewEditorOptions = {},
): Promise<vscode.TextEditor> {
if (!openBeside) {
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
}
const document = await vscode.workspace.openTextDocument({
language: languageId,
content,
});
await (await getParseTreeApi()).loadLanguage(languageId);
const editor = await vscode.window.showTextDocument(
document,
openBeside ? vscode.ViewColumn.Beside : undefined,
);
const eol = content.includes("\r\n")
? vscode.EndOfLine.CRLF
: vscode.EndOfLine.LF;
if (eol !== editor.document.eol) {
await editor.edit((editBuilder) => editBuilder.setEndOfLine(eol));
}
// Many times running these tests opens the sidebar, which slows performance. Close it.
vscode.commands.executeCommand("workbench.action.closeSidebar");
return editor;
}
export async function reuseEditor(
editor: vscode.TextEditor,
content: string,
language: string = "plaintext",
) {
if (editor.document.languageId !== language) {
await vscode.languages.setTextDocumentLanguage(editor.document, language);
await (await getParseTreeApi()).loadLanguage(language);
}
await editor.edit((editBuilder) => {
editBuilder.replace(
new vscode.Range(
editor.document.lineAt(0).range.start,
editor.document.lineAt(editor.document.lineCount - 1).range.end,
),
content,
);
const eol = content.includes("\r\n")
? vscode.EndOfLine.CRLF
: vscode.EndOfLine.LF;
if (eol !== editor.document.eol) {
editBuilder.setEndOfLine(eol);
}
});
}
/**
* Open a new notebook editor with the given cells
* @param cellContents A list of strings each of which will become the contents
* of a cell in the notebook
* @param language The language id to use for all the cells in the notebook
* @returns notebook
*/
export async function openNewNotebookEditor(
cellContents: string[],
language: string = "plaintext",
) {
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
const document = await vscode.workspace.openNotebookDocument(
"jupyter-notebook",
new vscode.NotebookData(
cellContents.map(
(contents) =>
new vscode.NotebookCellData(
vscode.NotebookCellKind.Code,
contents,
language,
),
),
),
);
await (await getParseTreeApi()).loadLanguage(language);
// FIXME: There seems to be some timing issue when you create a notebook
// editor
await waitForEditorToOpen();
return document;
}
function waitForEditorToOpen() {
return new Promise<void>((resolve, reject) => {
let count = 0;
const interval = setInterval(() => {
if (vscode.window.activeTextEditor != null) {
clearInterval(interval);
// Give it a moment to settle
setTimeout(resolve, 100);
} else {
count++;
if (count === 20) {
clearInterval(interval);
reject("Timed out waiting for editor to open");
}
}
}, 100);
});
}