-
Notifications
You must be signed in to change notification settings - Fork 3
/
InteractiveLatexDocumentManager.ts
190 lines (156 loc) · 7.29 KB
/
InteractiveLatexDocumentManager.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import * as vscode from "vscode";
import * as path from "path";
import { InteractiveLatexDocument, InteractiveLatexDocumentOptions } from "./InteractiveLatexDocument";
function createWebview(title: string): vscode.WebviewPanel {
return vscode.window.createWebviewPanel(
"ilatex",
title,
vscode.ViewColumn.Two,
{
enableScripts: true,
retainContextWhenHidden: true
}
);
}
export class InteractiveLatexDocumentManager implements vscode.Disposable {
// Map from the paths of main LaTeX files paths to their interactive LaTeX document instance
// This data structure encodes the fact there must be at most one iLaTeX instance per path
private mainLatexFilePathsToInteractiveLatexDocuments: Map<string, InteractiveLatexDocument>;
readonly nbInteractiveLatexDocumentsChangeEventEmitter: vscode.EventEmitter<void>;
constructor() {
this.mainLatexFilePathsToInteractiveLatexDocuments = new Map();
this.nbInteractiveLatexDocumentsChangeEventEmitter = new vscode.EventEmitter();
}
get nbILatexDocuments(): number {
return this.mainLatexFilePathsToInteractiveLatexDocuments.size;
}
get mainLatexFilePaths(): string[] {
return [...this.mainLatexFilePathsToInteractiveLatexDocuments.keys()];
}
get ilatexDocuments(): InteractiveLatexDocument[] {
return [...this.mainLatexFilePathsToInteractiveLatexDocuments.values()];
}
hasDocument(ilatexDocument: InteractiveLatexDocument): boolean {
return this.ilatexDocuments.includes(ilatexDocument);
}
hasDocumentWithMainFilePath(path: string): boolean {
return this.mainLatexFilePathsToInteractiveLatexDocuments.has(path);
}
getDocumentWithMainFilePath(path: string): InteractiveLatexDocument | undefined {
return this.mainLatexFilePathsToInteractiveLatexDocuments.get(path);
}
private createNewILatexDocumentFromMainFileAt(
uri: vscode.Uri,
options: InteractiveLatexDocumentOptions
): Promise<InteractiveLatexDocument> {
// Create and show a new webview panel
const fileName = path.basename(uri.path);
const webviewPanel = createWebview(`i-LaTeX — ${fileName}`);
// Create and return a new instance of iLaTeX
// The editor is mapped to the instance of iLateX until it is destroyed
return InteractiveLatexDocument.fromMainLatexFileAt(uri, webviewPanel, options)
.then(ilatexDocument => {
webviewPanel.onDidDispose(() => {
this.destroyILatexDocument(ilatexDocument);
});
return ilatexDocument;
});
}
createOrShowILatexDocumentFromMainFileAt(
uri: vscode.Uri,
options: InteractiveLatexDocumentOptions
): void {
const path = uri.path;
// If there already is an interactive LaTeX document
// for the given main file, simply reveal its webview
if (this.mainLatexFilePathsToInteractiveLatexDocuments.has(path)) {
const ilatexDocument = this.mainLatexFilePathsToInteractiveLatexDocuments.get(path);
ilatexDocument?.webviewManager.revealWebviewPanel();
return;
}
// Otherwise, attempt to create a new iLatex instance
try {
this.createNewILatexDocumentFromMainFileAt(uri, options)
.then(newIlatexDocument => {
// Once the interactive LaTeX document is created and initialised, do some bookkeeping
this.mainLatexFilePathsToInteractiveLatexDocuments.set(path, newIlatexDocument);
vscode.commands.executeCommand("setContext", "ilatex:hasActiveInstances", true);
this.nbInteractiveLatexDocumentsChangeEventEmitter.fire();
});
}
catch (error) {
vscode.window.showInformationMessage(`An unexpected error occured while creating a new iLaTeX document.`);
console.error("An unexpected error occured while creating a new iLaTeX instance: ", error);
}
}
createOrShowILatexDocumentFromActiveEditor(options: InteractiveLatexDocumentOptions): void {
// If there is no active editor,
// display an error message and abort
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
vscode.window.showErrorMessage("There is no active editor to init. an iLaTeX document from");
return;
}
// If the active editor has no (document,
// display an message which explain the problem and abort
const activeEditorDocument = activeEditor.document;
if (!activeEditorDocument) {
vscode.window.showErrorMessage("There is no document in the active editor to init. an iLaTeX document from.");
return;
}
this.createOrShowILatexDocumentFromMainFileAt(activeEditorDocument.uri, options);
}
recompileAllILatexDocumentsUsingActiveEditor(): void {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor || !activeEditor.document) {
return;
}
for (let ilatexDocument of this.mainLatexFilePathsToInteractiveLatexDocuments.values()) {
const activeEditorContainsFileFromCurrentInstance = ilatexDocument.sourceFileManager.sourceFiles.some(file => {
return file.isRepresentedByDocument(activeEditor.document);
});
if (activeEditorContainsFileFromCurrentInstance) {
ilatexDocument.recompileAndUpdate();
}
}
}
destroyILatexDocument(ilatexDocument: InteractiveLatexDocument): void {
const mainLatexFilePath = ilatexDocument.mainSourceFileUri.path;
const mainLatexFileName = path.basename(mainLatexFilePath);
try {
ilatexDocument.dispose();
}
catch (error) {
console.error(`An error occured while trying to dispose of an interactive LaTeX document (${mainLatexFileName}):`, error);
}
// Do some bookkeeping
this.mainLatexFilePathsToInteractiveLatexDocuments.delete(mainLatexFilePath);
vscode.commands.executeCommand(
"setContext",
"ilatex:hasActiveInstances",
this.mainLatexFilePathsToInteractiveLatexDocuments.size > 0
);
this.nbInteractiveLatexDocumentsChangeEventEmitter.fire();
console.info(`An interactive LaTeX document (${mainLatexFileName}) has been destroyed.`);
}
destroyILatexDocumentWithMainFilePath(path: string): void {
const ilatexDocument = this.mainLatexFilePathsToInteractiveLatexDocuments.get(path);
if (ilatexDocument) {
this.destroyILatexDocument(ilatexDocument);
}
}
destroyAllILatexDocuments(): void {
const nbInteractiveLatexDocuments = this.mainLatexFilePathsToInteractiveLatexDocuments.size;
if (nbInteractiveLatexDocuments === 0) {
return;
}
// Dispose of any remaining iLaTeX instance
console.warn(`The ${nbInteractiveLatexDocuments} remaining interactive LaTeX documenta are about to be destroyed...`);
for (let ilatexDocument of this.mainLatexFilePathsToInteractiveLatexDocuments.values()) {
this.destroyILatexDocument(ilatexDocument);
}
}
dispose(): void {
this.destroyAllILatexDocuments();
}
}