-
Notifications
You must be signed in to change notification settings - Fork 50
/
context.ts
198 lines (171 loc) · 7.98 KB
/
context.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
194
195
196
197
198
'use strict';
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { DiagnosticBatch } from './typenames';
export class Context {
private context: vscode.ExtensionContext;
private config: vscode.WorkspaceConfiguration;
private diagnosticCollection: vscode.DiagnosticCollection;
private collectedDiagnostics: { [id: string]: vscode.Diagnostic[]; } = {};
public activeEditor: vscode.TextEditor | undefined;
constructor(context: vscode.ExtensionContext, config: vscode.WorkspaceConfiguration) {
this.context = context;
this.config = config;
this.diagnosticCollection = vscode.languages.createDiagnosticCollection('shader-toy.errors');
this.activeEditor = vscode.window.activeTextEditor;
}
public makeUri(file: string): vscode.Uri {
return vscode.Uri.file(file);
}
public getResourceUri(file: string): vscode.Uri {
return this.makeUri(
path.join(this.context.extensionPath, 'resources', file)
);
}
public makeWebviewResource(webview: vscode.Webview, resourceUri: vscode.Uri): vscode.Uri {
return webview.asWebviewUri(resourceUri);
}
public getWebviewResourcePath(webview: vscode.Webview, file: string): string {
const resourceUri = this.getResourceUri(file);
const webviewResourceUri = this.makeWebviewResource(webview, resourceUri);
return webviewResourceUri.toString();
}
public async mapUserPath(userPath: string, sourcePath: string): Promise<{ file: string, userPath: string }> {
userPath = userPath.replace(/\\/g, '/');
sourcePath = path.dirname(sourcePath);
let file = await (async (file: string) => {
const fileCandidates: string[] = [];
const exists = async (file: string) => {
const singleFileExists = async (file: string) => {
try {
await fs.promises.access(file);
return true;
}
catch
{
return false;
}
};
if (file.indexOf('{}') < 0 && file.indexOf('*') < 0) {
return await singleFileExists(file);
}
else {
const existsWithEachPrefix = async (pattern: string, prefixes: [string, string, string, string, string, string]) => {
for (const dir of prefixes) {
const directionFile = pattern.replace('{}', dir);
if (!await singleFileExists(directionFile)) {
return false;
}
}
return true;
};
const possiblePrefixes: [string, string, string, string, string, string][] = [
['e', 'w', 'u', 'd', 'n', 's'],
['east', 'west', 'up', 'down', 'north', 'south'],
['px', 'nx', 'py', 'ny', 'pz', 'nz'],
['posx', 'negx', 'posy', 'negy', 'posz', 'negz']
];
const pattern = file.replace('*', '{}');
for (const prefixes of possiblePrefixes) {
if (await existsWithEachPrefix(pattern, prefixes)) {
return true;
}
}
return false;
}
};
// Highest priority are absolute paths
fileCandidates.push(file);
if (await exists(file)) {
return file;
}
// Second priority are relative to sourcePath
{
const fileCandidate = [sourcePath, file].join('/');
fileCandidates.push(fileCandidate);
if (await exists(fileCandidate)) {
return fileCandidate;
}
}
// Last priority are relative to workspace folders
if (vscode.workspace.workspaceFolders !== undefined) {
const workspaceFileCandidates: string[] = [];
for (const worspaceFolder of vscode.workspace.workspaceFolders) {
let workspacePath = worspaceFolder.uri.fsPath;
workspacePath = workspacePath.replace(/\\/g, '/');
workspacePath = workspacePath.replace(/\.\//g, '');
const fileCandidate = [workspacePath, file].join('/');
if (await exists(fileCandidate)) {
workspaceFileCandidates.push(fileCandidate);
}
fileCandidates.push(fileCandidate);
}
if (workspaceFileCandidates.length > 1) {
vscode.window.showErrorMessage(`Multiple candidates for file '${userPath}' were found in your workspace folders, first option was picked: '${workspaceFileCandidates[0]}'`);
}
return workspaceFileCandidates[0];
}
vscode.window.showErrorMessage(`File '${userPath}' was not found, paths that were tried were\n\t${fileCandidates.join('\n\t')}`);
return file;
})(userPath);
file = path.normalize(file);
file = file.replace(/\\/g, '/');
return { file, userPath };
}
public showErrorMessage(message: string) {
vscode.window.showErrorMessage(message);
}
public clearDiagnostics() {
this.collectedDiagnostics = {};
this.diagnosticCollection.clear();
}
public showDiagnostics(diagnosticBatch: DiagnosticBatch, severity: vscode.DiagnosticSeverity) {
const file = diagnosticBatch.filename;
const newDocument = vscode.workspace.openTextDocument(file);
newDocument.then((document: vscode.TextDocument) => {
if (this.collectedDiagnostics[file] === undefined) {
this.collectedDiagnostics[file] = [];
}
for (const diagnostic of diagnosticBatch.diagnostics) {
const line = Math.min(Math.max(1, diagnostic.line), document.lineCount) - 1;
const range = document.lineAt(line).range;
this.collectedDiagnostics[file].push(new vscode.Diagnostic(range, diagnostic.message, severity));
}
this.diagnosticCollection.set(document.uri, this.collectedDiagnostics[file]);
}, (reason) => {
vscode.window.showErrorMessage(`Could not open ${file} because ${reason}`);
});
}
public revealLine(file: string, line: number) {
const highlightLine = (document: vscode.TextDocument, line: number) => {
line = Math.min(Math.max(1, line), document.lineCount) - 1;
const range = document.lineAt(line).range;
vscode.window.showTextDocument(document, vscode.ViewColumn.One, true)
.then((editor: vscode.TextEditor) => {
editor.selection = new vscode.Selection(range.start, range.end);
editor.revealRange(range, vscode.TextEditorRevealType.InCenterIfOutsideViewport);
});
};
if (this.activeEditor) {
let currentFile = this.activeEditor.document.fileName;
currentFile = currentFile.replace(/\\/g, '/');
if (currentFile === file) {
highlightLine(this.activeEditor.document, line);
return;
}
}
const newDocument = vscode.workspace.openTextDocument(file);
newDocument.then((document: vscode.TextDocument) => {
highlightLine(document, line);
}, (reason) => {
vscode.window.showErrorMessage(`Could not open ${file} because ${reason}`);
});
}
public getConfig<T>(section: string): T | undefined {
return this.config.get<T>(section);
}
public getVscodeExtensionContext(): vscode.ExtensionContext {
return this.context;
}
}