-
Notifications
You must be signed in to change notification settings - Fork 3
/
SourceFileManager.ts
176 lines (140 loc) · 7.22 KB
/
SourceFileManager.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
import * as vscode from "vscode";
import { CodeMapping } from "../code-mappings/CodeMapping";
import { InteractiveLatexDocument } from "../InteractiveLatexDocument";
import { SourceFile } from "./SourceFile";
import { SourceFileChange } from "./SourceFileChange";
import { SourceFilePosition } from "./SourceFilePosition";
import { SourceFileRange } from "./SourceFileRange";
export class SourceFileManager {
private ilatexDocument: InteractiveLatexDocument;
private files: SourceFile[];
readonly sourceFileChangeEventEmitter: vscode.EventEmitter<SourceFile>;
readonly sourceFileSaveEventEmitter: vscode.EventEmitter<SourceFile>;
private sourceFilesToParsingErrorObserverDisposables: Map<SourceFile, vscode.Disposable>;
private textDocumentChangeObserverDisposable: vscode.Disposable;
private textDocumentSaveObserverDisposable: vscode.Disposable;
constructor(ilatexDocument: InteractiveLatexDocument) {
this.ilatexDocument = ilatexDocument;
this.files = [];
this.sourceFileChangeEventEmitter = new vscode.EventEmitter();
this.sourceFileSaveEventEmitter = new vscode.EventEmitter();
this.sourceFilesToParsingErrorObserverDisposables = new Map();
this.textDocumentChangeObserverDisposable = vscode.workspace.onDidChangeTextDocument(
async (event) => await this.processTextDocumentChange(event)
);
this.textDocumentSaveObserverDisposable = vscode.workspace.onDidSaveTextDocument(
async (document) => await this.processTextDocumentSave(document)
);
}
get sourceFiles(): SourceFile[] {
return this.files;
}
get hasDirtySourceFile(): boolean {
return this.sourceFiles.some(sourceFile => sourceFile.isDirty);
}
hasSourceFileWithPath(absolutePath: string): boolean {
return this.sourceFiles.some(sourceFile => sourceFile.uri.path === absolutePath);
}
getSourceFileWithPath(absolutePath: string): SourceFile | undefined {
return this.sourceFiles.find(sourceFile => sourceFile.uri.path === absolutePath);
}
getSourceFileOfCodeMapping(codeMapping: CodeMapping): SourceFile | undefined {
return this.getSourceFileWithPath(codeMapping.absolutePath);
}
dispose() {
for (let disposable of this.sourceFilesToParsingErrorObserverDisposables.values()) {
disposable.dispose();
}
this.textDocumentChangeObserverDisposable.dispose();
this.textDocumentSaveObserverDisposable.dispose();
for (let sourceFile of this.sourceFiles) {
sourceFile.dispose();
}
}
async saveAllSourceFiles(): Promise<void> {
await Promise.all(
this.sourceFiles.map(sourceFile => sourceFile.save())
);
}
async updateSourceFilesFromCodeMappings(): Promise<void> {
const absolutePathsOfCurrentSourceFiles = this.files.map(file => file.uri.path);
const absolutePathsOfCodeMappings = new Set(
this.ilatexDocument.codeMappingManager.codeMappings.map(codeMapping => codeMapping.absolutePath)
);
// Remove source files whose paths do not appear in the new set of code mappings' paths
// Stop observing parsing errors in those files
const sourceFilesToRemove = this.files.filter(sourceFile => !absolutePathsOfCodeMappings.has(sourceFile.uri.path));
for (let sourceFile of sourceFilesToRemove) {
this.sourceFilesToParsingErrorObserverDisposables.delete(sourceFile);
}
this.files = this.files.filter(sourceFile => absolutePathsOfCodeMappings.has(sourceFile.uri.path));
// Update source files whose path appear in both the new set of code mappings' paths
// and in the paths of the current source files, i.e. those that have not been deleted just above
for (let sourceFile of this.sourceFiles) {
await sourceFile.parseNewAST();
}
// Create new source files from paths that appear in the new set of code mappings' paths
// but not in the paths of the current source files
for (let path of absolutePathsOfCodeMappings) {
if (absolutePathsOfCurrentSourceFiles.includes(path)) {
continue;
}
const newSourceFile = await SourceFile.fromAbsolutePath(path);
this.files.push(newSourceFile);
// Start observing parsing errors in these files
this.sourceFilesToParsingErrorObserverDisposables.set(
newSourceFile,
newSourceFile.astNodeParsingErrorEventEmitter.event(parsingError => {
this.ilatexDocument.logFileManager.logError({
event: "parsing-error",
fileName: newSourceFile.name
});
})
);
}
// console.log("New list of source files: ", this.files);
}
private async processTextDocumentChange(event: vscode.TextDocumentChangeEvent): Promise<void> {
const sourceFileInChangedDocument = this.sourceFiles.find(
sourceFile => sourceFile.isRepresentedByDocument(event.document)
);
if (sourceFileInChangedDocument) {
for (let contentChange of event.contentChanges) {
const sourceFileChange = new SourceFileChange(contentChange);
const changeProcessingResult = await sourceFileInChangedDocument.processChange(sourceFileChange);
if (changeProcessingResult.changeIsLoggable) {
const editedTransitionalModel = this.ilatexDocument.transitionalModelManager.findModelContainingRange(new SourceFileRange(
SourceFilePosition.fromVscodePosition(sourceFileChange.start),
SourceFilePosition.fromVscodePosition(sourceFileChange.end)
)
);
let editedTransitionalDataToLog = {};
if (editedTransitionalModel) {
editedTransitionalDataToLog = {
transitionalName: editedTransitionalModel.transitionalName,
transitionalUid: editedTransitionalModel.uid,
transitionalCodeMappingId: editedTransitionalModel.codeMapping.id,
};
}
this.ilatexDocument.logFileManager.logUserEditEvent({
fileName: sourceFileInChangedDocument.name,
event: "text-edit",
editKind: sourceFileChange.kind.toLowerCase(),
editSize: sourceFileChange.size,
...editedTransitionalDataToLog
});
}
}
this.sourceFileChangeEventEmitter.fire(sourceFileInChangedDocument);
}
}
private async processTextDocumentSave(document: vscode.TextDocument): Promise<void> {
const sourceFileInSavedDocument = this.sourceFiles.find(
sourceFile => sourceFile.isRepresentedByDocument(document)
);
if (sourceFileInSavedDocument) {
await sourceFileInSavedDocument.processSave();
this.sourceFileSaveEventEmitter.fire(sourceFileInSavedDocument);
}
}
}