-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathIRascalFileSystemServices.java
More file actions
684 lines (586 loc) · 24 KB
/
IRascalFileSystemServices.java
File metadata and controls
684 lines (586 loc) · 24 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
/*
* Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.rascalmpl.vscode.lsp;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.NotDirectoryException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.stream.Stream;
import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification;
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest;
import org.eclipse.lsp4j.jsonrpc.services.JsonSegment;
import org.eclipse.lsp4j.jsonrpc.validation.NonNull;
import org.rascalmpl.library.Prelude;
import org.rascalmpl.uri.ISourceLocationWatcher.ISourceLocationChangeType;
import org.rascalmpl.uri.ISourceLocationWatcher.ISourceLocationChanged;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.uri.UnsupportedSchemeException;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.vscode.lsp.util.NamedThreadPool;
import org.rascalmpl.vscode.lsp.util.locations.Locations;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IValueFactory;
@JsonSegment("rascal/filesystem")
public interface IRascalFileSystemServices {
static final URIResolverRegistry reg = URIResolverRegistry.getInstance();
static final Logger IRascalFileSystemServices__logger = LogManager.getLogger(IRascalFileSystemServices.class);
static final ExecutorService executor = NamedThreadPool.cachedDaemon("rascal-vfs");
@JsonRequest
default CompletableFuture<SourceLocation> resolveLocation(SourceLocation loc) {
return CompletableFuture.supplyAsync(() -> {
try {
ISourceLocation tmp = loc.toRascalLocation();
ISourceLocation resolved = Locations.toClientLocation(tmp);
if (resolved == null) {
return loc;
}
return SourceLocation.fromRascalLocation(resolved);
} catch (Exception e) {
IRascalFileSystemServices__logger.warn("Could not resolve location {}", loc, e);
return loc;
}
}, executor);
}
@JsonRequest
default CompletableFuture<Void> watch(WatchParameters params) {
return CompletableFuture.runAsync(() -> {
try {
ISourceLocation loc = params.getLocation();
URIResolverRegistry.getInstance().watch(loc, params.isRecursive(), changed -> {
try {
onDidChangeFile(convertChangeEvent(changed));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (IOException | URISyntaxException | RuntimeException e) {
throw new VSCodeFSError(e);
}
}, executor);
}
static FileChangeEvent convertChangeEvent(ISourceLocationChanged changed) throws IOException {
return new FileChangeEvent(convertFileChangeType(changed.getChangeType()),
Locations.toUri(changed.getLocation()).toASCIIString());
}
static FileChangeType convertFileChangeType(ISourceLocationChangeType changeType) throws IOException {
switch (changeType) {
case CREATED:
return FileChangeType.Created;
case DELETED:
return FileChangeType.Deleted;
case MODIFIED:
return FileChangeType.Changed;
default:
throw new IOException("unknown change type: " + changeType);
}
}
private static boolean readonly(ISourceLocation loc) throws IOException {
if (reg.getRegisteredOutputSchemes().contains(loc.getScheme())) {
return false;
}
if (reg.getRegisteredLogicalSchemes().contains(loc.getScheme())) {
var resolved = Locations.toClientLocation(loc);
if (resolved != null && resolved != loc) {
return readonly(resolved);
}
return true;
}
return true;
}
@JsonRequest
default CompletableFuture<FileStat> stat(URIParameter uri) {
return CompletableFuture.supplyAsync(() -> {
try {
ISourceLocation loc = uri.getLocation();
if (!reg.exists(loc)) {
throw new FileNotFoundException();
}
var created = reg.created(loc);
var lastModified = reg.lastModified(loc);
if (reg.isDirectory(loc)) {
return new FileStat(FileType.Directory, created, lastModified, 0, null);
}
long size = 0;
if (reg.supportsReadableFileChannel(loc)) {
try (var c = reg.getReadableFileChannel(loc)) {
size = c.size();
}
}
else {
size = Prelude.__getFileSize(IRascalValueFactory.getInstance(), loc).longValue();
}
return new FileStat(FileType.File, created, lastModified, size, readonly(loc) ? FilePermission.Readonly : null);
} catch (IOException | URISyntaxException | RuntimeException e) {
throw new VSCodeFSError(e);
}
}, executor);
}
@JsonRequest
default CompletableFuture<FileWithType[]> readDirectory(URIParameter uri) {
return CompletableFuture.supplyAsync(() -> {
try {
ISourceLocation loc = uri.getLocation();
if (!reg.isDirectory(loc)) {
throw VSCodeFSError.notADirectory(loc);
}
return Arrays.stream(reg.list(loc)).map(l -> new FileWithType(URIUtil.getLocationName(l),
reg.isDirectory(l) ? FileType.Directory : FileType.File)).toArray(FileWithType[]::new);
} catch (IOException | URISyntaxException | RuntimeException e) {
throw new VSCodeFSError(e);
}
}, executor);
}
@JsonRequest
default CompletableFuture<Void> createDirectory(URIParameter uri) {
return CompletableFuture.runAsync(() -> {
try {
reg.mkDirectory(uri.getLocation());
} catch (IOException | URISyntaxException | RuntimeException e) {
throw new VSCodeFSError(e);
}
}, executor);
}
@JsonRequest
default CompletableFuture<LocationContent> readFile(URIParameter uri) {
return CompletableFuture.supplyAsync(() -> {
try (InputStream source = new Base64InputStream(reg.getInputStream(uri.getLocation()), true)) {
return new LocationContent(new String(source.readAllBytes(), StandardCharsets.US_ASCII));
} catch (IOException | URISyntaxException | RuntimeException e) {
throw new VSCodeFSError(e);
}
}, executor);
}
@JsonRequest
default CompletableFuture<Void> writeFile(WriteFileParameters params) {
return CompletableFuture.runAsync(() -> {
try {
ISourceLocation loc = params.getLocation();
boolean fileExists = reg.exists(loc);
if (!fileExists && !params.isCreate()) {
throw new FileNotFoundException(loc.toString());
}
if (fileExists && reg.isDirectory(loc)) {
throw VSCodeFSError.isADirectory(loc);
}
ISourceLocation parentFolder = URIUtil.getParentLocation(loc);
if (!reg.exists(parentFolder) && params.isCreate()) {
throw new FileNotFoundException(parentFolder.toString());
}
if (fileExists && params.isCreate() && !params.isOverwrite()) {
throw new FileAlreadyExistsException(loc.toString());
}
try (OutputStream target = reg.getOutputStream(loc, false)) {
target.write(Base64.getDecoder().decode(params.getContent()));
}
} catch (IOException | URISyntaxException | RuntimeException e) {
throw new VSCodeFSError(e);
}
}, executor);
}
@JsonRequest
default CompletableFuture<Void> delete(DeleteParameters params) {
return CompletableFuture.runAsync(() -> {
try {
ISourceLocation loc = params.getLocation();
reg.remove(loc, params.isRecursive());
} catch (IOException | URISyntaxException e) {
throw new CompletionException(e);
}
}, executor);
}
@JsonRequest
default CompletableFuture<Void> rename(RenameParameters params) {
return CompletableFuture.runAsync(() -> {
try {
ISourceLocation oldLoc = params.getOldLocation();
ISourceLocation newLoc = params.getNewLocation();
reg.rename(oldLoc, newLoc, params.isOverwrite());
} catch (IOException | URISyntaxException e) {
throw new CompletionException(e);
}
}, executor);
}
@JsonRequest("schemes")
default CompletableFuture<String[]> fileSystemSchemes() {
Set<String> inputs = reg.getRegisteredInputSchemes();
Set<String> logicals = reg.getRegisteredLogicalSchemes();
return CompletableFuture
.completedFuture(Stream.concat(inputs.stream(), logicals.stream()).toArray(String[]::new));
}
@JsonNotification
default void onDidChangeFile(FileChangeEvent event) { }
public static class DeleteParameters {
private final String uri;
private final boolean recursive;
public DeleteParameters(String uri, boolean recursive) {
this.uri = uri;
this.recursive = recursive;
}
public ISourceLocation getLocation() throws URISyntaxException {
return new URIParameter(uri).getLocation();
}
public boolean isRecursive() {
return recursive;
}
}
public static class RenameParameters {
private final String oldUri;
private final String newUri;
private final boolean overwrite;
public RenameParameters(String oldUri, String newUri, boolean overwrite) {
this.oldUri = oldUri;
this.newUri = newUri;
this.overwrite = overwrite;
}
public ISourceLocation getOldLocation() throws URISyntaxException {
return new URIParameter(oldUri).getLocation();
}
public ISourceLocation getNewLocation() throws URISyntaxException {
return new URIParameter(newUri).getLocation();
}
public boolean isOverwrite() {
return overwrite;
}
}
public static class WatchParameters {
private final String uri;
private final boolean recursive;
private final String[] excludes;
public WatchParameters(String uri, boolean recursive, String[] excludes) {
this.uri = uri;
this.recursive = recursive;
this.excludes = excludes;
}
public ISourceLocation getLocation() throws URISyntaxException {
return new URIParameter(uri).getLocation();
}
public String[] getExcludes() {
return excludes;
}
public boolean isRecursive() {
return recursive;
}
}
public static class SourceLocation {
@NonNull private final String uri;
private final int @Nullable[] offsetLength;
private final int @Nullable[] beginLineColumn;
private final int @Nullable[] endLineColumn;
public static SourceLocation fromRascalLocation(ISourceLocation loc) {
if (loc.hasOffsetLength()) {
if (loc.hasLineColumn()) {
return new SourceLocation(Locations.toUri(loc).toString(), loc.getOffset(), loc.getLength(), loc.getBeginLine(), loc.getBeginColumn(), loc.getEndLine(), loc.getEndColumn());
}
else {
return new SourceLocation(Locations.toUri(loc).toString(), loc.getOffset(), loc.getLength());
}
}
else {
return new SourceLocation(Locations.toUri(loc).toString());
}
}
public ISourceLocation toRascalLocation() throws URISyntaxException {
final IValueFactory VF = IRascalValueFactory.getInstance();
ISourceLocation tmp = Locations.toCheckedLoc(uri);
if (hasOffsetLength()) {
if (hasLineColumn()) {
tmp = VF.sourceLocation(tmp,getOffset(), getLength(), getBeginLine(), getEndLine(), getBeginColumn(), getEndColumn());
}
else {
tmp = VF.sourceLocation(tmp, getOffset(), getLength());
}
}
return tmp;
}
private SourceLocation(String uri, int offset, int length, int beginLine, int beginColumn, int endLine, int endColumn) {
this.uri = uri;
this.offsetLength = new int[] {offset, length};
this.beginLineColumn = new int [] {beginLine, beginColumn};
this.endLineColumn = new int [] {endLine, endColumn};
}
private SourceLocation(String uri, int offset, int length) {
this.uri = uri;
this.offsetLength = new int[] {offset, length};
this.beginLineColumn = null;
this.endLineColumn = null;
}
private SourceLocation(String uri) {
this.uri = uri;
this.offsetLength = null;
this.beginLineColumn = null;
this.endLineColumn = null;
}
public String getUri() {
return uri;
}
@EnsuresNonNullIf(expression = "this.offsetLength", result = true)
public boolean hasOffsetLength() {
return offsetLength != null;
}
@EnsuresNonNullIf(expression = "this.endLineColumn", result = true)
@EnsuresNonNullIf(expression = "this.beginLineColumn", result = true)
public boolean hasLineColumn() {
return beginLineColumn != null && endLineColumn != null;
}
public int getOffset() {
if (!hasOffsetLength()) {
throw new IllegalStateException("This location has no offset");
}
return offsetLength[0];
}
public int getLength() {
if (!hasOffsetLength()) {
throw new IllegalStateException("This location has no length");
}
return offsetLength[1];
}
public int getBeginLine() {
if (!hasLineColumn()) {
throw new IllegalStateException("This location has no line and columns");
}
return beginLineColumn[0];
}
public int getBeginColumn() {
if (!hasLineColumn()) {
throw new IllegalStateException("This location has no line and columns");
}
return beginLineColumn[1];
}
public int getEndLine() {
if (!hasLineColumn()) {
throw new IllegalStateException("This location has no line and columns");
}
return endLineColumn[0];
}
public int getEndColumn() {
if (!hasLineColumn()) {
throw new IllegalStateException("This location has no line and columns");
}
return endLineColumn[1];
}
}
public static class FileChangeEvent {
@NonNull private final FileChangeType type;
@NonNull private final String uri;
public FileChangeEvent(FileChangeType type, @NonNull String uri) {
this.type = type;
this.uri = uri;
}
public FileChangeType getType() {
return type;
}
public ISourceLocation getLocation() throws URISyntaxException {
return new URIParameter(uri).getLocation();
}
}
public enum FileChangeType {
Changed(1), Created(2), Deleted(3);
private final int value;
private FileChangeType(int val) {
assert val == 1 || val == 2 || val == 3;
this.value = val;
}
public int getValue() {
return value;
}
}
// The fields of are only used on the TS side
@SuppressWarnings("unused")
public static class FileStat {
private final FileType type;
private final long ctime;
private final long mtime;
private final long size;
private @Nullable FilePermission permissions;
public FileStat(FileType type, long ctime, long mtime, long size, @Nullable FilePermission permissions) {
this.type = type;
this.ctime = ctime;
this.mtime = mtime;
this.size = size;
this.permissions = permissions;
}
}
public enum FileType {
Unknown(0), File(1), Directory(2), SymbolicLink(64);
private final int value;
private FileType(int val) {
assert val == 0 || val == 1 || val == 2 || val == 64;
this.value = val;
}
public int getValue() {
return value;
}
}
// this enum models the enum inside vscode, which in the future might become an enum flag
// in that case we have to solve that
public enum FilePermission {
Readonly(1);
private final int value;
private FilePermission(int val) {
assert val == 1;
this.value = val;
}
public int getValue() {
return value;
}
}
public static class FileWithType {
@NonNull private final String name;
@NonNull private final FileType type;
public FileWithType(@NonNull String name, @NonNull FileType type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public FileType getType() {
return type;
}
}
public static class LocationContent {
@NonNull private final String content;
public LocationContent(@NonNull String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
public static class URIParameter {
@NonNull private final String uri;
public URIParameter(@NonNull String uri) {
this.uri = uri;
}
public String getUri() {
return uri;
}
public ISourceLocation getLocation() throws URISyntaxException {
return Locations.toCheckedLoc(uri);
}
}
public static class WriteFileParameters {
@NonNull private final String uri;
@NonNull private final String content;
private final boolean create;
private final boolean overwrite;
public WriteFileParameters(@NonNull String uri, @NonNull String content, boolean create, boolean overwrite) {
this.uri = uri;
this.content = content;
this.create = create;
this.overwrite = overwrite;
}
public String getUri() {
return uri;
}
public ISourceLocation getLocation() throws URISyntaxException {
return new URIParameter(uri).getLocation();
}
public String getContent() {
return content;
}
public boolean isCreate() {
return create;
}
public boolean isOverwrite() {
return overwrite;
}
}
/** Maps common exceptions to FileSystemError in VS Code */
public static class VSCodeFSError extends ResponseErrorException {
public VSCodeFSError(Exception original) {
super(translate(original));
}
private static ResponseError fileExists(Object data) {
return new ResponseError(-1, "File exists", data);
}
private static ResponseError fileIsADirectory(Object data) {
return new ResponseError(-2, "File is a directory", data);
}
private static ResponseError fileNotADirectory(Object data) {
return new ResponseError(-3, "File is not a directory", data);
}
private static ResponseError fileNotFound(Object data) {
return new ResponseError(-4, "File is not found", data);
}
private static ResponseError noPermissions(Object data) {
return new ResponseError(-5, "No permissions", data);
}
@SuppressWarnings("unused")
private static ResponseError unavailable(Object data) {
return new ResponseError(-6, "Unavailable", data);
}
private static ResponseError generic(@Nullable String message, Object data) {
return new ResponseError(-99, message == null ? "no error message was provided" : message, data);
}
public static ResponseErrorException notADirectory(Object data) {
return new ResponseErrorException(fileNotADirectory(data));
}
public static ResponseErrorException isADirectory(Object data) {
return new ResponseErrorException(fileIsADirectory(data));
}
private static ResponseError translate(Exception original) {
if (original instanceof FileNotFoundException
|| original instanceof UnsupportedSchemeException
|| original instanceof URISyntaxException
) {
return fileNotFound(original);
}
else if (original instanceof FileAlreadyExistsException) {
return fileExists(original);
}
else if (original instanceof NotDirectoryException) {
return fileNotADirectory(original);
}
else if (original instanceof SecurityException) {
return noPermissions(original);
}
else if (original instanceof ResponseErrorException) {
return ((ResponseErrorException)original).getResponseError();
}
return generic(original.getMessage(), original);
}
}
}