forked from intersystems-community/vscode-objectscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSystemProvider.ts
805 lines (773 loc) · 30.7 KB
/
FileSystemProvider.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
import * as path from "path";
import * as vscode from "vscode";
import { AtelierAPI } from "../../api";
import { Directory } from "./Directory";
import { File } from "./File";
import { fireOtherStudioAction, OtherStudioAction } from "../../commands/studio";
import { projectContentsFromUri, studioOpenDialogFromURI } from "../../utils/FileProviderUtil";
import {
classNameRegex,
isClassDeployed,
notNull,
outputChannel,
redirectDotvscodeRoot,
workspaceFolderOfUri,
} from "../../utils/index";
import { config, intLangId, macLangId, workspaceState } from "../../extension";
import { addIsfsFileToProject, modifyProject } from "../../commands/project";
import { DocumentContentProvider } from "../DocumentContentProvider";
import { Document } from "../../api/atelier";
declare function setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timeout;
export type Entry = File | Directory;
export function generateFileContent(
uri: vscode.Uri,
fileName: string,
sourceContent: Buffer
): { content: string[]; enc: boolean } {
const sourceLines = sourceContent.length ? new TextDecoder().decode(sourceContent).split("\n") : [];
const fileExt = fileName.split(".").pop().toLowerCase();
const csp = fileName.startsWith("/");
if (fileExt === "cls" && !csp) {
const className = fileName.split(".").slice(0, -1).join(".");
let content: string[] = [];
const preamble: string[] = [];
if (sourceLines.length) {
if (uri.scheme == "file" && (fileName.includes(path.sep) || fileName.includes(" "))) {
// We couldn't resolve a class name from the file path,
// so keep the source text unchanged.
content = sourceLines;
} else {
// Use all lines except for the Class x.y one.
// Replace that with one to match fileName.
while (sourceLines.length > 0) {
const nextLine = sourceLines.shift();
if (nextLine.toLowerCase().startsWith("class ")) {
const classLine = nextLine.split(" ");
classLine[0] = "Class";
classLine[1] = className;
content.push(...preamble, classLine.join(" "), ...sourceLines);
break;
}
preamble.push(nextLine);
}
if (!content.length) {
// Transfer sourceLines verbatim in cases where no class header line is found
content.push(...preamble);
}
}
} else {
content = [`Class ${className} Extends %RegisteredObject`, "{", "}"];
}
return {
content,
enc: false,
};
} else if (["int", "inc", "mac"].includes(fileExt) && !csp) {
if (sourceLines.length && uri.scheme == "file" && (fileName.includes(path.sep) || fileName.includes(" "))) {
// We couldn't resolve a routine name from the file path,
// so keep the source text unchanged.
return {
content: sourceLines,
enc: false,
};
} else {
sourceLines.shift();
const routineName = fileName.split(".").slice(0, -1).join(".");
const routineType = fileExt != "mac" ? `[Type=${fileExt.toUpperCase()}]` : "";
if (sourceLines.length === 0 && fileExt !== "inc") {
const languageId = fileExt === "mac" ? macLangId : intLangId;
// Labels cannot contain dots
const firstLabel = routineName.replaceAll(".", "");
// Be smart about whether to use a Tab or a space between label and comment.
// Doing this will help autodetect to do the right thing.
const lineStart = vscode.workspace.getConfiguration("editor", { languageId, uri }).get("insertSpaces")
? " "
: "\t";
sourceLines.push(`${firstLabel}${lineStart};`);
}
return {
content: [`ROUTINE ${routineName} ${routineType}`, ...sourceLines],
enc: false,
};
}
}
return {
content: [sourceContent.toString("base64")],
enc: true,
};
}
/**
* This map contains all csp files contained in a directory
* within a workspace folder that has a `project` query parameter.
* The key is the URI for the folder. The value is an array of names of
* csp files contained within the folder.
* @example
* cspFilesInProjectFolder.get(`isfs://iris:user/csp/user/?project=test`) = ["menu.csp"]
*/
const cspFilesInProjectFolder: Map<string, string[]> = new Map();
/**
* Check if this file is a web application file.
*/
export function isCSPFile(uri: vscode.Uri): boolean {
const params = new URLSearchParams(uri.query);
let csp = params.has("csp") && ["", "1"].includes(params.get("csp"));
if (params.has("project") && params.get("project").length) {
// Projects can contain both CSP and non-CSP files
// Read the cache of found CSP files to determine if this is one
const parent = uri
.with({
path: path.dirname(uri.path),
})
.toString();
csp = cspFilesInProjectFolder.has(parent) && cspFilesInProjectFolder.get(parent).includes(path.basename(uri.path));
if (!csp) {
// Read the parent directory and file is not CSP OR haven't read the parent directory yet
// Use the file extension to guess if it's a web app file
const additionalExts: string[] = config("projects.webAppFileExtensions", workspaceFolderOfUri(uri));
csp = [
"csp",
"csr",
"ts",
"js",
"css",
"scss",
"sass",
"less",
"html",
"json",
"md",
"markdown",
"png",
"svg",
"jpeg",
"jpg",
"ico",
"xml",
"txt",
...additionalExts,
].includes(uri.path.split(".").pop().toLowerCase());
}
}
return csp;
}
export class FileSystemProvider implements vscode.FileSystemProvider {
private superRoot = new Directory("", "");
public readonly onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]>;
private _emitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>();
private _bufferedEvents: vscode.FileChangeEvent[] = [];
private _fireSoonHandle?: NodeJS.Timer;
public constructor() {
this.onDidChangeFile = this._emitter.event;
}
// Used by import and compile to make sure we notice its changes
public fireFileChanged(uri: vscode.Uri): void {
// Remove entry from our cache
this._lookupParentDirectory(uri).then((parent) => {
const name = path.basename(uri.path);
parent.entries.delete(name);
});
// Queue the event
this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
}
public stat(uri: vscode.Uri): Promise<vscode.FileStat> {
const redirectedUri = redirectDotvscodeRoot(uri);
if (redirectedUri.path !== uri.path) {
// When redirecting the /.vscode subtree we must fill in as-yet-unvisited folders to fix https://github.com/intersystems-community/vscode-objectscript/issues/1143
return this._lookup(redirectedUri, true);
}
return this._lookup(uri);
}
public async readDirectory(uri: vscode.Uri): Promise<[string, vscode.FileType][]> {
uri = redirectDotvscodeRoot(uri);
const parent = await this._lookupAsDirectory(uri);
const api = new AtelierAPI(uri);
if (!api.active) {
throw vscode.FileSystemError.Unavailable(`${uri.toString()} is unavailable`);
}
const params = new URLSearchParams(uri.query);
if (params.has("project") && params.get("project").length) {
// Get all items in the project
return projectContentsFromUri(uri).then((entries) =>
entries.map((entry) => {
const csp = ["CSP", "DIR"].includes(entry.Type);
if (!entry.Name.includes(".")) {
if (!parent.entries.has(entry.Name)) {
const folder = !csp
? uri.path.replace(/\//g, ".")
: uri.path === "/"
? ""
: uri.path.endsWith("/")
? uri.path
: uri.path + "/";
const fullName = folder === "" ? entry.Name : csp ? folder + entry.Name : folder + "/" + entry.Name;
parent.entries.set(entry.Name, new Directory(entry.Name, fullName));
}
return [entry.Name, vscode.FileType.Directory];
} else {
if (csp) {
// Projects can contain both CSP and non-CSP files
// Update the cache of found CSP files to include this file
const mapkey = uri.toString();
let mapvalue: string[] = [];
if (cspFilesInProjectFolder.has(mapkey)) {
mapvalue = cspFilesInProjectFolder.get(mapkey);
}
mapvalue.push(entry.Name);
cspFilesInProjectFolder.set(mapkey, mapvalue);
}
return [entry.Name, vscode.FileType.File];
}
})
);
}
const csp = params.has("csp") && ["", "1"].includes(params.get("csp"));
const folder = !csp
? uri.path.replace(/\//g, ".")
: uri.path === "/"
? ""
: uri.path.endsWith("/")
? uri.path
: uri.path + "/";
// get all web apps that have a filepath (Studio dialog used below returns REST ones too)
const cspApps = csp ? await api.getCSPApps().then((data) => data.result.content || []) : [];
const cspSubfolderMap = new Map<string, vscode.FileType>();
const prefix = folder === "" ? "/" : folder;
for (const app of cspApps) {
if ((app + "/").startsWith(prefix)) {
const subfolder = app.slice(prefix.length).split("/")[0];
if (subfolder) {
cspSubfolderMap.set(subfolder, vscode.FileType.Directory);
}
}
}
const cspSubfolders = Array.from(cspSubfolderMap.entries());
return studioOpenDialogFromURI(uri)
.then((data) => data.result.content || [])
.then((data) => {
const results = data
.filter((item: { Name: string; Type: number }) =>
item.Type == 10
? csp && !item.Name.includes("/") // ignore web apps here because there may be REST ones
: item.Type == 9 // class package
? !csp
: csp
? item.Type == 5 // web app file
: true
)
.map((item: { Name: string; Type: number }) => {
const name = item.Name;
if (item.Type == 10 || item.Type == 9) {
if (!parent.entries.has(name)) {
const fullName = folder === "" ? name : csp ? folder + name : folder + "/" + name;
parent.entries.set(name, new Directory(name, fullName));
}
return [name, vscode.FileType.Directory];
} else {
return [name, vscode.FileType.File];
}
});
if (!csp) {
return results;
}
cspSubfolders.forEach((value) => {
const name = value[0];
if (!parent.entries.has(name)) {
const fullName = folder + name;
parent.entries.set(name, new Directory(name, fullName));
}
results.push(value);
});
return results;
})
.catch((error) => {
if (error) {
console.log(error);
if (error.errorText.includes(" #5540:")) {
const nsUpper = api.config.ns.toUpperCase();
const message = `User '${api.config.username}' cannot list ${
csp ? `web application '${uri.path}'` : "namespace"
} contents. If they do not have READ permission on the default code database of the ${nsUpper} namespace then grant it and retry. If the problem remains then execute the following SQL in that namespace:\n\t GRANT EXECUTE ON %Library.RoutineMgr_StudioOpenDialog TO ${
api.config.username
}`;
outputChannel.appendError(message);
}
}
});
}
public createDirectory(uri: vscode.Uri): void | Thenable<void> {
uri = redirectDotvscodeRoot(uri);
const basename = path.posix.basename(uri.path);
const dirname = uri.with({ path: path.posix.dirname(uri.path) });
return this._lookupAsDirectory(dirname).then((parent) => {
const entry = new Directory(basename, uri.path);
parent.entries.set(entry.name, entry);
parent.mtime = Date.now();
parent.size += 1;
this._fireSoon(
{ type: vscode.FileChangeType.Changed, uri: dirname },
{ type: vscode.FileChangeType.Created, uri }
);
});
}
public async readFile(uri: vscode.Uri): Promise<Uint8Array> {
// Use _lookup() instead of _lookupAsFile() so we send
// our cached mtime with the GET /doc request if we have it
return this._lookup(uri, true).then((file: File) => {
// Update cache entry
const uniqueId = `${workspaceFolderOfUri(uri)}:${file.fileName}`;
workspaceState.update(`${uniqueId}:mtime`, file.mtime);
return file.data;
});
}
public writeFile(
uri: vscode.Uri,
content: Buffer,
options: {
create: boolean;
overwrite: boolean;
}
): void | Thenable<void> {
uri = redirectDotvscodeRoot(uri);
if (uri.path.startsWith("/.")) {
throw vscode.FileSystemError.NoPermissions("dot-folders not supported by server");
}
const csp = isCSPFile(uri);
const fileName = csp ? uri.path : uri.path.slice(1).replace(/\//g, ".");
if (fileName.startsWith(".")) {
return;
}
const api = new AtelierAPI(uri);
// Use _lookup() instead of _lookupAsFile() so we send
// our cached mtime with the GET /doc request if we have it
return this._lookup(uri).then(
async () => {
// Weirdly, if the file exists on the server we don't actually write its content here.
// Instead we simply return as though we wrote it successfully.
// The actual writing is done by our workspace.onDidSaveTextDocument handler.
// But first check cases for which we should fail the write and leave the document dirty if changed.
if (!csp && fileName.split(".").pop().toLowerCase() === "cls") {
// Check if the class is deployed
if (await isClassDeployed(fileName, api)) {
throw new Error("Cannot overwrite a deployed class");
}
// Check if the class name and file name match
let clsname = "";
const match = new TextDecoder().decode(content).match(classNameRegex);
if (match) {
[, clsname] = match;
}
if (clsname === "") {
throw new Error("Cannot save a malformed class");
}
if (fileName.slice(0, -4) !== clsname) {
throw new Error("Cannot save an isfs class where the class name and file name do not match");
}
}
// Set a -1 mtime cache entry so the actual write by the workspace.onDidSaveTextDocument handler always overwrites.
// By the time we get here VS Code's built-in conflict resolution mechanism will already have interacted with the user.
const uniqueId = `${workspaceFolderOfUri(uri)}:${fileName}`;
workspaceState.update(`${uniqueId}:mtime`, -1);
return;
},
(error) => {
if (error.code !== "FileNotFound" || !options.create) {
return Promise.reject();
}
// File doesn't exist on the server, and we are allowed to create it.
// Create content (typically a stub, unless the write-phase of a copy operation).
const newContent = generateFileContent(uri, fileName, content);
// Write it to the server
return api
.putDoc(
fileName,
{
...newContent,
mtime: Date.now(),
},
false
)
.catch((error) => {
// Throw all failures
if (error && error.errorText && error.errorText !== "") {
throw vscode.FileSystemError.Unavailable(error.errorText);
}
throw vscode.FileSystemError.Unavailable(uri);
})
.then(async (response) => {
// New file has been written
if (response && response.result.ext && response.result.ext[0] && response.result.ext[1]) {
fireOtherStudioAction(OtherStudioAction.CreatedNewDocument, uri, response.result.ext[0]);
fireOtherStudioAction(OtherStudioAction.FirstTimeDocumentSave, uri, response.result.ext[1]);
}
const params = new URLSearchParams(uri.query);
if (params.has("project") && params.get("project").length) {
// Add this document to the project if required
await addIsfsFileToProject(params.get("project"), uri, fileName, csp, api);
}
// Sanity check that we find it there, then make client side update things
this._lookupAsFile(uri).then(() => {
this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
});
// Ask to put cursor at start of 3rd line.
// For CLS stub this will be where properties and methods need to be inserted.
// For MAC and INT stubs there is no 3rd line, so cursor goes to the end of the 2nd, which is 1st of actual routine and is ready for comment text.
const editor = await vscode.window.showTextDocument(uri);
editor.selection = new vscode.Selection(2, 0, 2, 0);
});
}
);
}
/** Process a Document object that was successfully deleted. */
private async processDeletedDoc(doc: Document, uri: vscode.Uri, csp: boolean, project: boolean): Promise<void> {
const events: vscode.FileChangeEvent[] = [];
try {
if (doc.ext) {
fireOtherStudioAction(OtherStudioAction.DeletedDocument, uri, doc.ext);
}
// Remove entry from our cache, plus any now-empty ancestor entries
let thisUri = vscode.Uri.parse(uri.toString(), true);
while (thisUri.path !== "/") {
events.push({ type: vscode.FileChangeType.Deleted, uri: thisUri });
const parentDir = await this._lookupParentDirectory(thisUri);
const name = path.basename(thisUri.path);
parentDir.entries.delete(name);
if (!csp && parentDir.entries.size === 0) {
thisUri = thisUri.with({ path: path.posix.dirname(thisUri.path) });
} else {
break;
}
}
if (csp && project) {
// Remove this file from our CSP files cache
const parentUriStr = uri
.with({
path: path.dirname(uri.path),
})
.toString();
const mapvalue = cspFilesInProjectFolder.get(parentUriStr);
const idx = mapvalue.indexOf(path.basename(uri.path));
if (idx != -1) {
mapvalue.splice(idx, 1);
if (mapvalue.length) {
cspFilesInProjectFolder.set(parentUriStr, mapvalue);
} else {
cspFilesInProjectFolder.delete(parentUriStr);
}
}
}
} catch {
// Swallow all errors
} finally {
if (events.length) {
this._fireSoon(...events);
}
}
}
public async delete(uri: vscode.Uri, options: { recursive: boolean }): Promise<void> {
uri = redirectDotvscodeRoot(uri);
const csp = isCSPFile(uri);
const fileName = csp ? uri.path : uri.path.slice(1).replace(/\//g, ".");
const params = new URLSearchParams(uri.query);
const project = params.has("project") && params.get("project").length > 0;
const api = new AtelierAPI(uri);
if (fileName.startsWith(".")) {
return;
}
if (await this._lookup(uri, true).then((entry) => entry instanceof Directory)) {
// Get the list of documents to delete
let toDeletePromise: Promise<any>;
if (project) {
// Ignore the recursive flag for project folders
toDeletePromise = projectContentsFromUri(uri, true);
} else {
toDeletePromise = studioOpenDialogFromURI(uri, options.recursive ? { flat: true } : undefined);
}
const toDelete: string[] = await toDeletePromise.then((data) =>
data.result.content
.map((entry) => {
if (options.recursive || project) {
return entry.Name;
} else if (entry.Name.includes(".")) {
return csp ? uri.path + entry.Name : uri.path.slice(1).replace(/\//g, ".") + entry.Name;
}
return null;
})
.filter(notNull)
);
if (toDelete.length == 0) {
// Nothing to delete
return;
}
// Delete the documents
return api.deleteDocs(toDelete).then((data) => {
let failed = 0;
for (const doc of data.result) {
if (doc.status == "") {
this.processDeletedDoc(
doc,
DocumentContentProvider.getUri(doc.name, undefined, undefined, true, uri),
csp,
project
);
} else {
// The document was not deleted, so log the error
failed++;
outputChannel.appendLine(`${failed == 1 ? "\n" : ""}${doc.status}`);
}
}
if (project) {
// Remove everything in this folder from the project if required
modifyProject(uri, "remove");
}
if (failed > 0) {
outputChannel.show(true);
throw new vscode.FileSystemError(
`Failed to delete ${failed} document${
failed > 1 ? "s" : ""
}. Check 'ObjectScript' Output channel for details.`
);
}
});
}
return api.deleteDoc(fileName).then(
(response) => {
this.processDeletedDoc(response.result, uri, csp, project);
if (project) {
// Remove this document from the project if required
modifyProject(uri, "remove");
}
},
(error) => {
let message = `Failed to delete file '${fileName}'.`;
if (error && error.errorText && error.errorText !== "") {
outputChannel.appendLine("\n" + error.errorText);
outputChannel.show(true);
message += " Check 'ObjectScript' Output channel for details.";
}
throw new vscode.FileSystemError(message);
}
);
}
public async rename(oldUri: vscode.Uri, newUri: vscode.Uri, options: { overwrite: boolean }): Promise<void> {
if (!oldUri.path.includes(".")) {
throw vscode.FileSystemError.NoPermissions("Cannot rename a package/folder");
}
if (oldUri.path.split(".").pop().toLowerCase() != newUri.path.split(".").pop().toLowerCase()) {
throw vscode.FileSystemError.NoPermissions("Cannot change a file's extension during rename");
}
if (vscode.workspace.getWorkspaceFolder(oldUri) != vscode.workspace.getWorkspaceFolder(newUri)) {
throw vscode.FileSystemError.NoPermissions("Cannot rename a file across workspace folders");
}
// Check if the destination exists
let newFileStat: vscode.FileStat;
try {
newFileStat = await vscode.workspace.fs.stat(newUri);
if (!options.overwrite) {
// If it does and we can't overwrite it, throw an error
throw vscode.FileSystemError.FileExists(newUri);
}
} catch (error) {
if (error instanceof vscode.FileSystemError && error.code == "FileExists") {
// Re-throw the FileExists error
throw error;
}
}
// Get the name of the new file
const newParams = new URLSearchParams(newUri.query);
const newCsp = newParams.has("csp") && ["", "1"].includes(newParams.get("csp"));
const newFileName = newCsp ? newUri.path : newUri.path.slice(1).replace(/\//g, ".");
// Generate content for the new file
const newContent = generateFileContent(
newUri,
newFileName,
Buffer.from(await vscode.workspace.fs.readFile(oldUri))
);
if (newFileStat) {
// We're overwriting an existing file so prompt the user to check it out
await fireOtherStudioAction(OtherStudioAction.AttemptedEdit, newUri);
}
// Write the new file
// This is going to attempt the write regardless of the user's response to the check out prompt
const api = new AtelierAPI(newUri);
await api
.putDoc(
newFileName,
{
...newContent,
mtime: Date.now(),
},
true
)
.catch((error) => {
// Throw all failures
if (error && error.errorText && error.errorText !== "") {
throw vscode.FileSystemError.Unavailable(error.errorText);
}
throw vscode.FileSystemError.Unavailable(error.message);
})
.then(async (response) => {
// New file has been written
if (newFileStat != undefined && response && response.result.ext && response.result.ext[0]) {
// We created a file
fireOtherStudioAction(OtherStudioAction.CreatedNewDocument, newUri, response.result.ext[0]);
fireOtherStudioAction(OtherStudioAction.FirstTimeDocumentSave, newUri, response.result.ext[1]);
if (newParams.has("project") && newParams.get("project").length) {
// Add the new document to the project if required
await modifyProject(newUri, "add");
}
}
// Sanity check that we find it there, then make client side update things
this._lookupAsFile(newUri).then(() => {
this._fireSoon({ type: vscode.FileChangeType.Changed, uri: newUri });
});
});
// Delete the old file
await vscode.workspace.fs.delete(oldUri);
}
public watch(uri: vscode.Uri): vscode.Disposable {
return new vscode.Disposable(() => {
return;
});
}
// Fetch entry (a file or directory) from cache, else from server
private async _lookup(uri: vscode.Uri, fillInPath?: boolean): Promise<Entry> {
const api = new AtelierAPI(uri);
if (uri.path === "/") {
await api
.serverInfo()
.then()
.catch((error) => {
if (error && error.errorText && error.errorText !== "") {
throw vscode.FileSystemError.Unavailable(error.errorText);
}
throw vscode.FileSystemError.Unavailable(uri);
});
}
const config = api.config;
const rootName = `${config.username}@${config.host}:${config.port}${config.pathPrefix}/${config.ns.toUpperCase()}`;
let entry: Entry = this.superRoot.entries.get(rootName);
if (!entry) {
entry = new Directory(rootName, "");
this.superRoot.entries.set(rootName, entry);
}
const parts = uri.path.split("/");
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (!part) {
continue;
}
let child: Entry | undefined;
if (entry instanceof Directory) {
child = entry.entries.get(part);
// If the last element of path is dotted and is one we haven't already cached as a directory
// then it is assumed to be a file. Treat all other cases as a directory we haven't yet explored.
if (!child && (!part.includes(".") || i + 1 < parts.length)) {
if (!fillInPath) {
throw vscode.FileSystemError.FileNotFound(uri);
}
// Caller granted us permission to create structures for intermediate directories not yet seen.
// This arises when ObjectScript Explorer uses isfs to enable server-side editing, and when reloading a workspace
// in which isfs documents were previously open.
// See https://github.com/intersystems-community/vscode-objectscript/issues/879
const fullName = entry.name === "" ? part : entry.fullName + "/" + part;
child = new Directory(part, fullName);
entry.entries.set(part, child);
}
}
if (!child) {
if (part.includes(".")) {
return this._lookupAsFile(uri);
} else {
throw vscode.FileSystemError.FileNotFound(uri);
}
} else if (child instanceof File) {
// Return cached copy unless changed, in which case return updated one
return this._lookupAsFile(uri, child);
} else {
entry = child;
}
}
return entry;
}
private async _lookupAsDirectory(uri: vscode.Uri): Promise<Directory> {
// Reject attempt to access /node_modules
if (uri.path.startsWith("/node_modules")) {
throw vscode.FileSystemError.FileNotADirectory(uri);
}
const entry = await this._lookup(uri, true);
if (entry instanceof Directory) {
return entry;
}
throw vscode.FileSystemError.FileNotADirectory(uri);
}
// Fetch from server and cache it, optionally the passed cached copy if unchanged on server
private async _lookupAsFile(uri: vscode.Uri, cachedFile?: File): Promise<File> {
uri = redirectDotvscodeRoot(uri);
if (uri.path.startsWith("/.")) {
throw vscode.FileSystemError.NoPermissions("dot-folders not supported by server");
}
const csp = isCSPFile(uri);
const name = path.basename(uri.path);
const fileName = csp ? uri.path : uri.path.slice(1).replace(/\//g, ".");
const api = new AtelierAPI(uri);
return api
.getDoc(fileName, undefined, cachedFile?.mtime)
.then((data) => data.result)
.then((result) => {
const fileSplit = fileName.split(".");
const fileType = fileSplit[fileSplit.length - 1];
if (!csp && ["bpl", "dtl"].includes(fileType)) {
const partialUri = Array.isArray(result.content) ? result.content[0] : String(result.content).split("\n")[0];
const strippedUri = partialUri.split("&STUDIO=")[0];
const { https, host, port, pathPrefix } = api.config;
result.content = [
`${https ? "https" : "http"}://${host}:${port}${pathPrefix}${strippedUri}`,
"Use the link above to launch the external editor in your web browser.",
"Do not edit this document here. It cannot be saved to the server.",
];
}
return result;
})
.then(
({ ts, content }) =>
new File(
name,
fileName,
ts,
Array.isArray(content) ? content.join("\n").length : content.length,
Array.isArray(content) ? content.join("\n") : content
)
)
.then((entry) =>
this._lookupParentDirectory(uri).then((parent) => {
// Store in parent directory's cache
parent.entries.set(name, entry);
return entry;
})
)
.catch((error) => {
if (error?.statusCode === 304 && cachedFile) {
return cachedFile;
}
if (error && error.errorText && error.errorText !== "") {
throw vscode.FileSystemError.FileNotFound(error.errorText);
}
throw vscode.FileSystemError.FileNotFound(uri);
});
}
private async _lookupParentDirectory(uri: vscode.Uri): Promise<Directory> {
uri = redirectDotvscodeRoot(uri);
const dirname = uri.with({ path: path.posix.dirname(uri.path) });
return await this._lookupAsDirectory(dirname);
}
private _fireSoon(...events: vscode.FileChangeEvent[]): void {
this._bufferedEvents.push(...events);
if (this._fireSoonHandle) {
clearTimeout(this._fireSoonHandle);
}
this._fireSoonHandle = setTimeout(() => {
this._emitter.fire(this._bufferedEvents);
this._bufferedEvents = [];
}, 5);
}
}