forked from intersystems-community/vscode-objectscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemProvider.ts
More file actions
1098 lines (1054 loc) · 41.8 KB
/
FileSystemProvider.ts
File metadata and controls
1098 lines (1054 loc) · 41.8 KB
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as path from "path";
import * as vscode from "vscode";
import { isText } from "istextorbinary";
import { AtelierAPI } from "../../api";
import { fireOtherStudioAction, OtherStudioAction } from "../../commands/studio";
import { isfsConfig, projectContentsFromUri, studioOpenDialogFromURI } from "../../utils/FileProviderUtil";
import {
classNameRegex,
cspAppsForUri,
isClassDeployed,
notIsfs,
notNull,
outputChannel,
handleError,
redirectDotvscodeRoot,
stringifyError,
base64EncodeContent,
openLowCodeEditors,
compileErrorMsg,
isCompilable,
} from "../../utils";
import { FILESYSTEM_READONLY_SCHEMA, FILESYSTEM_SCHEMA, intLangId, macLangId } from "../../extension";
import { addIsfsFileToProject, modifyProject } from "../../commands/project";
import { DocumentContentProvider } from "../DocumentContentProvider";
import { Document, UserAction } from "../../api/atelier";
class File implements vscode.FileStat {
public type: vscode.FileType;
public ctime: number;
public mtime: number;
public size: number;
public permissions?: vscode.FilePermission;
public fileName: string;
public name: string;
public data?: Uint8Array;
public constructor(name: string, fileName: string, ts: string, size: number, data: string | Buffer) {
this.type = vscode.FileType.File;
this.ctime = Number(new Date(ts + "Z"));
this.mtime = this.ctime;
this.size = size;
this.fileName = fileName;
this.name = name;
this.data = typeof data === "string" ? Buffer.from(data) : data;
}
}
class Directory implements vscode.FileStat {
public name: string;
public fullName: string;
public type: vscode.FileType;
public ctime: number;
public mtime: number;
public size: number;
public entries: Map<string, File | Directory>;
public constructor(name: string, fullName: string) {
this.name = name;
this.fullName = fullName;
this.type = vscode.FileType.Directory;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.entries = new Map();
}
}
type Entry = File | Directory;
export function generateFileContent(
uri: vscode.Uri,
fileName: string,
sourceContent: Uint8Array
): { content: string[]; enc: boolean; eol: vscode.EndOfLine } {
const sourceLines = sourceContent.length ? new TextDecoder().decode(sourceContent).split("\n") : [];
// Detect eol style (a return value), and if CRLF then strip the \r character from end of source lines
let eol = vscode.EndOfLine.LF;
if (sourceLines.length && sourceLines[0].slice(-1) == "\r") {
eol = vscode.EndOfLine.CRLF;
for (let i = 0; i < sourceLines.length; i++) {
if (sourceLines[i].slice(-1) == "\r") {
sourceLines[i] = sourceLines[i].slice(0, -1);
}
}
}
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 (notIsfs(uri) && (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,
eol,
};
} else if (["int", "inc", "mac"].includes(fileExt) && !csp) {
if (sourceLines.length && notIsfs(uri) && (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,
eol,
};
} 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,
eol,
};
}
}
return {
content: base64EncodeContent(Buffer.from(sourceContent)),
enc: true,
eol,
};
}
/**
* 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();
/** Returns `true` if `uri` is a web application file */
export function isCSP(uri: vscode.Uri): boolean {
const { csp, project } = isfsConfig(uri);
if (project) {
// 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();
if (cspFilesInProjectFolder.has(parent) && cspFilesInProjectFolder.get(parent).includes(path.basename(uri.path))) {
return true;
}
// 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[] = vscode.workspace
.getConfiguration("objectscript.projects", uri)
.get("webAppFileExtensions");
return [
"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;
}
/** Get the document name of the file in `uri`. */
export function isfsDocumentName(uri: vscode.Uri, csp?: boolean, pkg = false): string {
const { project } = isfsConfig(uri);
if (pkg && project && ["", "/"].includes(uri.path)) {
// pkg is only true when opening a context server-side source control menu.
// When called on a project workspace root folder, show the menu for the project.
return `${project}.PRJ`;
}
if (csp == undefined) csp = isCSP(uri);
const doc = csp ? uri.path : uri.path.slice(1).replace(/\//g, ".");
// Add the .PKG extension to non-web folders if called from StudioActions
return pkg && !csp && !doc.split("/").pop().includes(".") ? `${doc}.PKG` : doc;
}
/**
* Validate that `uri`'s path is in "canonical form" for classes and routines.
* For example, the "canonical" uri path representing `%Library.CHUIScreen.cls`
* is `/%Library/CHUIScreen.cls`. Paths that will not be rejected include
* `/%CHUIScreen.cls` (short alias), `/%Library.CHUIScreen.cls` (dotted packages),
* and `/%Library/CHUIScreen.CLS` (extension has wrong case). This is needed to
* prevent the user from opening multiple copies of the same document. This
* function does not return a value; it throws a `vscode.FileSystemError.FileNotFound`
* error when `uri`'s path is not in "canonical form".
*/
function validateUriIsCanonical(uri: vscode.Uri): void {
const numDots = uri.path.split(".").length - 1;
const lastFour = uri.path.slice(-4);
if (
!isfsConfig(uri).csp &&
[".cls", ".mac", ".int", ".inc"].includes(lastFour.toLowerCase()) &&
// extension has wrong case
(![".cls", ".mac", ".int", ".inc"].includes(lastFour) ||
// short alias for %Library class
(uri.path.startsWith("/%") && lastFour == ".cls" && numDots == 1 && uri.path.split("/").length == 2) ||
// dotted packages
(numDots > 1 && !(numDots == 2 && /\.G?\d\.int$/.test(uri.path))))
) {
throw vscode.FileSystemError.FileNotFound(uri);
}
}
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.Timeout;
/**
* The stringified URIs of all `isfs` documents that may have had
* their contents changed during the last time they were saved
*/
private readonly _needsUpdate: Set<string> = new Set();
public constructor() {
this.onDidChangeFile = this._emitter.event;
}
/**
* Check if this `isfs` document stringified URI was changed during
* the last time it was saved. This is used to force VS Code to update
* the editor tab containing the file.
*/
public needsUpdate(uriString: string): boolean {
if (this._needsUpdate.has(uriString)) {
this._needsUpdate.delete(uriString);
return true;
}
return false;
}
// Used by import and compile to make sure we notice its changes
public fireFileChanged(uri: vscode.Uri): void {
this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
}
public async stat(uri: vscode.Uri): Promise<vscode.FileStat> {
const api = new AtelierAPI(uri);
if (!api.active) throw vscode.FileSystemError.Unavailable("Server connection is inactive");
validateUriIsCanonical(uri);
let entryPromise: Promise<Entry>;
let result: Entry;
const redirectedUri = redirectDotvscodeRoot(uri, vscode.FileSystemError.FileNotFound(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
entryPromise = this._lookup(redirectedUri, true);
} else {
entryPromise = this._lookup(uri);
}
// If this is our readonly variant there's no point checking server-side whether the file sould be marked readonly
if (uri.scheme === FILESYSTEM_READONLY_SCHEMA) {
return entryPromise;
}
if (entryPromise instanceof File) {
// previously resolved as a file
result = entryPromise;
} else if (entryPromise instanceof Promise && uri.path.split("/").pop()?.split(".").length > 1) {
// apparently a file, so resolve ahead of adding permissions
result = await entryPromise;
} else {
// otherwise return the promise
return entryPromise;
}
if (result instanceof File) {
const serverName = isfsDocumentName(uri);
if (serverName.slice(-4).toLowerCase() == ".cls") {
if (await isClassDeployed(serverName, api)) {
result.permissions |= vscode.FilePermission.Readonly;
return result;
}
}
// Does server-side source control report it as editable?
if (vscode.workspace.getConfiguration("objectscript.serverSourceControl", uri)?.get("respectEditableStatus")) {
const query = "select * from %Atelier_v1_Utils.Extension_GetStatus(?)";
const statusObj = await api.actionQuery(query, [serverName]);
const docStatus = statusObj.result?.content?.pop();
if (docStatus) {
result.permissions = docStatus.editable ? undefined : result.permissions | vscode.FilePermission.Readonly;
}
}
}
return result;
}
public async readDirectory(uri: vscode.Uri): Promise<[string, vscode.FileType][]> {
if (uri.path.includes(".vscode/") || uri.path.endsWith(".vscode")) {
throw new vscode.FileSystemError("Cannot read the /.vscode directory");
}
const parent = await this._lookupAsDirectory(uri);
const api = new AtelierAPI(uri);
if (!api.active) throw vscode.FileSystemError.Unavailable(uri);
const { csp, project } = isfsConfig(uri);
if (project) {
// 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(/\/$/, "").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 folder = !csp
? uri.path.replace(/\/$/, "").replace(/\//g, ".")
: uri.path === "/"
? ""
: uri.path.endsWith("/")
? uri.path
: uri.path + "/";
// Get all web apps that have a path (StudioOpenDialog returns all web apps)
const cspApps = csp ? cspAppsForUri(uri) : [];
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) {
if (error.errorText.includes(" #5540:")) {
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 ${api.config.ns.toUpperCase()} 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
}`;
handleError(message);
}
}
});
}
public createDirectory(uri: vscode.Uri): void | Thenable<void> {
uri = redirectDotvscodeRoot(uri, new vscode.FileSystemError("Server does not have a /_vscode web application"));
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> {
validateUriIsCanonical(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, true).then((file: File) => file.data);
}
public writeFile(
uri: vscode.Uri,
content: Uint8Array,
options: {
create: boolean;
overwrite: boolean;
}
): void | Thenable<void> {
const originalUriString = uri.toString();
const originalUri = vscode.Uri.parse(originalUriString);
this._needsUpdate.delete(originalUriString);
uri = redirectDotvscodeRoot(uri, new vscode.FileSystemError("Server does not have a /_vscode web application"));
if (uri.path.startsWith("/.")) {
throw new vscode.FileSystemError("dot-folders are not supported by server");
}
validateUriIsCanonical(uri);
const csp = isCSP(uri);
const fileName = isfsDocumentName(uri, csp);
if (fileName.startsWith(".")) {
return;
}
const api = new AtelierAPI(uri);
let created = false;
let update = false;
const isCls = !csp && fileName.split(".").pop().toLowerCase() == "cls";
// 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 (entry: File) => {
// Check cases for which we should fail the write and leave the document dirty if changed
if (isCls) {
// 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 vscode.FileSystemError("Cannot save a malformed class");
}
if (fileName.slice(0, -4) != clsname) {
throw new vscode.FileSystemError(
"Cannot save an isfs class where the class name and file name do not match"
);
}
if (openLowCodeEditors.has(uri.toString())) {
// This class is open in a low-code editor, so any
// updates to the class will be handled by that editor
return;
}
// Check if the class is deployed
if (await isClassDeployed(fileName, api)) {
throw new vscode.FileSystemError("Cannot overwrite a deployed class");
}
}
const contentBuffer = Buffer.from(content);
const putContent = isText(uri.path.split("/").pop(), contentBuffer)
? {
content: new TextDecoder().decode(content).split(/\r?\n/),
enc: false,
}
: {
content: base64EncodeContent(contentBuffer),
enc: true,
};
if (
csp &&
!putContent.enc &&
putContent.content.length > 1 &&
putContent.content[putContent.content.length - 1] == ""
) {
// Avoid appending a blank line on every save, which would cause a web app file to grow each time
putContent.content.pop();
}
// By the time we get here VS Code's built-in conflict resolution mechanism will already have interacted with the user.
// Therefore, it's safe to ignore any conflicts.
return api
.putDoc(
fileName,
{
...putContent,
mtime: -1,
},
true
)
.then((data) => {
update = isCls || data.result.content.length > 0;
return entry;
})
.catch((error) => {
// Throw all failures
const errorStr = stringifyError(error);
throw errorStr ? new vscode.FileSystemError(errorStr) : vscode.FileSystemError.Unavailable(uri);
});
},
(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
const errorStr = stringifyError(error);
throw errorStr ? new vscode.FileSystemError(errorStr) : vscode.FileSystemError.Unavailable(uri);
})
.then((data) => {
// New file has been written
if (data && data.result.ext && data.result.ext[0] && data.result.ext[1]) {
fireOtherStudioAction(OtherStudioAction.CreatedNewDocument, uri, data.result.ext[0]);
fireOtherStudioAction(OtherStudioAction.FirstTimeDocumentSave, uri, data.result.ext[1]);
}
const { project } = isfsConfig(uri);
if (project) {
// Add this document to the project if required
addIsfsFileToProject(project, fileName, api);
}
// Create an entry in our cache for the document
return this._lookupAsFile(uri).then((entry) => {
created = true;
update = isCls || data.result.content.length > 0;
this._fireSoon({ type: vscode.FileChangeType.Created, uri });
return entry;
});
});
}
)
.then((entry) => {
if (!entry) return; // entry is only empty when uri is open in a low-code editor
// Compile the document if required
if (
isCompilable(entry.fileName) &&
vscode.workspace.getConfiguration("objectscript", uri).get("compileOnSave")
) {
// Need to return the compile promise because technically the post-save compilation
// is part of the "write" operation from VS Code's point of view. This is required
// to prevent concurreny issues when VS Code refreshs its internal representaton of
// the file system while documents are being compiled.
return this.compile(originalUri, entry, update);
} else if (update) {
// The file's contents may have changed as a result of the save,
// so make sure VS Code updates the editor tab contents if needed
this._needsUpdate.add(originalUriString);
} else if (!created) {
this._fireSoon({ type: vscode.FileChangeType.Changed, uri: originalUri });
}
});
}
/** 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, <UserAction>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, vscode.FileSystemError.FileNotFound(uri));
validateUriIsCanonical(uri);
const { project } = isfsConfig(uri);
const csp = isCSP(uri);
const api = new AtelierAPI(uri);
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).then(
(data) => data.result.content
);
}
const toDelete: string[] = await toDeletePromise.then((data) =>
data
.map((entry) => {
if (options.recursive || project) {
return entry.Name;
} else if (entry.Name.includes(".")) {
const uriPath = uri.path.endsWith("/") ? uri.path : uri.path + "/";
return (csp ? uriPath : uriPath.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),
doc.name.includes("/"),
project.length > 0
);
} 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 the 'ObjectScript' Output channel for details.`
);
}
});
} else {
const fileName = isfsDocumentName(uri, csp);
if (fileName.startsWith(".")) return;
return api.deleteDoc(fileName).then(
(response) => {
this.processDeletedDoc(response.result, uri, csp, project.length > 0);
if (project) {
// Remove this document from the project if required
modifyProject(uri, "remove");
}
},
(error) => {
handleError(error);
throw new vscode.FileSystemError(
`Failed to delete file '${fileName}'. Check the 'ObjectScript' Output channel for details.`
);
}
);
}
}
public async rename(oldUri: vscode.Uri, newUri: vscode.Uri, options: { overwrite: boolean }): Promise<void> {
if (!oldUri.path.split("/").pop().includes(".")) {
throw new vscode.FileSystemError("Cannot rename a package/folder");
}
if (oldUri.path.split(".").pop().toLowerCase() != newUri.path.split(".").pop().toLowerCase()) {
throw new vscode.FileSystemError("Cannot change a file's extension during rename");
}
if (vscode.workspace.getWorkspaceFolder(oldUri) != vscode.workspace.getWorkspaceFolder(newUri)) {
throw new vscode.FileSystemError("Cannot rename a file across workspace folders");
}
validateUriIsCanonical(oldUri);
validateUriIsCanonical(newUri);
// 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);
} else if (newFileStat.permissions & vscode.FilePermission.Readonly) {
// If the file is read-only, throw an error
// This can happen if the target class is deployed,
// or the document is marked read-only by source control
throw vscode.FileSystemError.NoPermissions(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 newFileName = isfsDocumentName(newUri);
// Generate content for the new file
const newContent = generateFileContent(newUri, newFileName, 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
const errorStr = stringifyError(error);
throw errorStr ? new vscode.FileSystemError(errorStr) : vscode.FileSystemError.Unavailable(newUri);
})
.then(async (response) => {
// New file has been written
if (!newFileStat && 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]);
const { project } = isfsConfig(newUri);
if (project) {
// Add the new document to the project if required
await addIsfsFileToProject(project, newFileName, api);
}
}
// Sanity check that we find it there, then make client side update things
this._lookupAsFile(newUri).then(() => {
this._fireSoon({
type: newFileStat ? vscode.FileChangeType.Changed : vscode.FileChangeType.Created,
uri: newUri,
});
});
});
// Delete the old file
await vscode.workspace.fs.delete(oldUri);
}
/**
* If `uri` is a file, compile it.
* If `uri` is a directory, compile its contents.
* `file` and `update` are passed if called from `writeFile()`.
*/
public async compile(uri: vscode.Uri, file?: File, update?: boolean): Promise<void> {
if (!uri || uri.scheme != FILESYSTEM_SCHEMA) return;
const originalUriString = uri.toString();
const originalUri = vscode.Uri.parse(originalUriString);
this._needsUpdate.delete(originalUriString);
uri = redirectDotvscodeRoot(uri, new vscode.FileSystemError("Server does not have a /_vscode web application"));
const compileList: string[] = [];
try {
const entry = file || (await this._lookup(uri, true));
if (!entry) return;
if (entry instanceof Directory) {
// Get the list of files to compile
let compileListPromise: Promise<any>;
if (isfsConfig(uri).project) {
compileListPromise = projectContentsFromUri(uri, true);
} else {
compileListPromise = studioOpenDialogFromURI(uri, { flat: true }).then((data) => data.result.content);
}
compileList.push(...(await compileListPromise.then((data) => data.map((e) => e.Name))));
} else {
// Compile this file
compileList.push(isCSP(uri) ? uri.path : uri.path.slice(1).replace(/\//g, "."));
}
} catch (error) {
handleError(error, "Error determining documents to compile.");
return;
}
if (!compileList.length) return;
const api = new AtelierAPI(uri);
const conf = vscode.workspace.getConfiguration("objectscript");
const filesToUpdate: string[] = [];
// Compile the files
await vscode.window.withProgress(
{
cancellable: true,
location: vscode.ProgressLocation.Notification,
title: `Compiling: ${compileList.length == 1 ? compileList[0] : compileList.length + " files"}`,
},
(progress, token: vscode.CancellationToken) =>
api
.asyncCompile(compileList, token, conf.get("compileFlags"))
.then((data) => {
const info = compileList.length > 1 ? "" : `${compileList}: `;
if (data.status && data.status.errors && data.status.errors.length) {
throw new Error(`${info}Compile error`);
}
data.result.content.forEach((f) => filesToUpdate.push(f.name));
})
.catch((error) => compileErrorMsg(error))
);
if (file && (update || filesToUpdate.includes(file.fileName))) {
// This file was just written and the write may have changed its contents or the
// compilation changed its contents. Therefore, we must force VS Code to update it.
this._needsUpdate.add(originalUriString);
}
if (filesToUpdate.length) {
// Force VS Code to refresh the active editor tab's contents if the file was changed
// by compilation and is NOT the file that was saved if this is a post-save compilation
const activeDoc = vscode.window.activeTextEditor?.document;
if (activeDoc && !activeDoc.isDirty && !activeDoc.isClosed) {
const activeDocUriString = activeDoc.uri.toString();
if (
!(file && activeDocUriString == originalUriString) &&
filesToUpdate.some(
(f) =>
DocumentContentProvider.getUri(f, undefined, undefined, undefined, originalUri)?.toString() ==
activeDocUriString
)
) {
// Force VS Code to refresh the contents in the active editor tab
vscode.commands.executeCommand("workbench.action.files.revert");
}
}
}
// Fire file changed events for all files changed by compilation
this._fireSoon(
...filesToUpdate.map((f) => {
return {
type: vscode.FileChangeType.Changed,
uri: DocumentContentProvider.getUri(f, undefined, undefined, undefined, originalUri),
};
})
);
// Fire file changed events for the "other" documents related to
// the files that were compiled, or the files changed by compilation
this._fireSoon(
...(
await api
.actionIndex(Array.from(new Set(compileList.concat(filesToUpdate))))
.then((data) => data.result.content.flatMap((idx) => (!idx.status.length ? idx.others : [])))
.catch(() => {
// Index API returned an error. This should never happen.
return [];
})
).map((f: string) => {
return {
type: vscode.FileChangeType.Changed,
uri: DocumentContentProvider.getUri(f, undefined, undefined, undefined, originalUri),
};
})
);
}
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);
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) {