-
Notifications
You must be signed in to change notification settings - Fork 3
/
SourceFile.ts
193 lines (155 loc) · 5.98 KB
/
SourceFile.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
191
192
193
import * as vscode from "vscode";
import * as path from "path";
import { LatexAST } from "../ast/LatexAST";
import { SourceFileChange } from "./SourceFileChange";
import { SourceFileRange } from "../source-files/SourceFileRange";
import { EditableSection, TransientSourceFileEditor } from "./TransientSourceFileEditor";
import { SourceFileEditor, SourceFileEdit, SourceFileEditProvider } from "./SourceFileEditor";
import { ASTParsingError } from "../ast/ASTParser";
export class NotInitialisedError {}
export type SourceFileChangeProcessingResult = {
processedByAst: boolean;
changeIsLoggable: boolean;
};
export class SourceFile {
readonly uri: vscode.Uri;
readonly name: string;
private latexAst: LatexAST | null;
private hasUnsavedChanges: boolean;
// Note: if changes are ignored, they will not be logged either!
ignoreChanges: boolean;
skipChangeLogging: boolean;
astNodeParsingErrorEventEmitter: vscode.EventEmitter<ASTParsingError>;
astNodeParsingErrorEventObserverDisposable: vscode.Disposable | null;
private constructor(absolutePath: string) {
this.uri = vscode.Uri.file(absolutePath);
this.name = path.basename(absolutePath);
this.latexAst = null;
this.hasUnsavedChanges = false;
this.ignoreChanges = false;
this.skipChangeLogging = false;
this.astNodeParsingErrorEventEmitter = new vscode.EventEmitter();
this.astNodeParsingErrorEventObserverDisposable = null;
}
private async init(): Promise<void> {
// Preload the document in VS Code
await vscode.workspace.openTextDocument(this.uri.path);
// Create and initialise the AST
await this.parseNewAST();
}
get isDirty(): boolean {
return this.hasUnsavedChanges;
}
get ast(): LatexAST {
if (!this.latexAst) {
throw new NotInitialisedError();
}
return this.latexAst;
}
get document(): Promise<vscode.TextDocument> {
return new Promise((resolve, reject) => {
vscode.workspace
.openTextDocument(this.uri.path)
.then(document => resolve(document));
});
}
get isOpenInVisibleEditor(): boolean {
return vscode.window.visibleTextEditors.some(editor => {
return editor.document.uri.path === this.uri.path;
});
}
isOpenInEditor(editor: vscode.TextEditor): boolean {
return this.isRepresentedByDocument(editor.document);
}
isRepresentedByDocument(document: vscode.TextDocument): boolean {
return this.uri.path === document.uri.path;
}
async getOrOpenInEditor(focus: boolean = false): Promise<vscode.TextEditor> {
return vscode.window.showTextDocument(
await this.document,
vscode.ViewColumn.One,
!focus
);
}
async selectRangeInEditor(
range: SourceFileRange,
openIfNotVisible: boolean = false,
scrollIfNotVisible: boolean = true
): Promise<void> {
if (!openIfNotVisible && !this.isOpenInVisibleEditor) {
return;
}
const editor = await this.getOrOpenInEditor();
// If the selected range is not visible, possibly scroll to the selection
editor.selections = [range.asVscodeSelection];
editor.revealRange(
range.asVscodeRange,
scrollIfNotVisible
? vscode.TextEditorRevealType.InCenterIfOutsideViewport
: undefined
);
}
async getContent(range?: SourceFileRange): Promise<string> {
const document = await this.document;
return document.getText(range?.asVscodeRange);
}
async applyEdits(...edits: SourceFileEdit[]): Promise<void> {
const editor = this.createEditor();
editor.addEdits(...edits);
await editor.apply();
}
createEditor(editProviders: SourceFileEditProvider[] = []): SourceFileEditor {
return new SourceFileEditor(this, editProviders);
}
createTransientEditor(editableSections: EditableSection[]): TransientSourceFileEditor {
return new TransientSourceFileEditor(this, editableSections);
}
async parseNewAST(): Promise<void> {
this.astNodeParsingErrorEventObserverDisposable?.dispose();
this.latexAst = new LatexAST(this);
this.astNodeParsingErrorEventObserverDisposable =
this.latexAst.parsingErrorEventEmitter.event(parsingError => {
this.astNodeParsingErrorEventEmitter.fire(parsingError);
vscode.window.showWarningMessage(
`File '${this.name}' could not be parsed by iLaTeX\n(transitionals will not be available in this file).`
);
});
await this.latexAst.init();
}
async save(): Promise<void> {
const document = await this.document;
if (!document.isDirty) {
return;
}
const success = await document.save();
if (!success) {
console.error(`An error occured when trying to save a source file (${this.uri.path}).`);
}
}
async processChange(change: SourceFileChange): Promise<SourceFileChangeProcessingResult> {
this.hasUnsavedChanges = true;
if (this.latexAst && !this.ignoreChanges) {
await this.latexAst.processSourceFileChange(change);
return {
processedByAst: true,
changeIsLoggable: !this.skipChangeLogging
};
}
return {
processedByAst: false,
changeIsLoggable: false
};
}
async processSave(): Promise<void> {
await this.parseNewAST();
this.hasUnsavedChanges = false;
}
dispose(): void {
this.astNodeParsingErrorEventObserverDisposable?.dispose();
}
static async fromAbsolutePath(absolutePath: string): Promise<SourceFile> {
const newSourceFile = new SourceFile(absolutePath);
await newSourceFile.init();
return newSourceFile;
}
}