Skip to content
61 changes: 59 additions & 2 deletions src/org/rascalmpl/library/util/IDEServicesLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
package org.rascalmpl.library.util;

import java.io.IOException;
import java.io.StringWriter;
import java.util.function.Function;

import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.ideservices.IDEServices;
import org.rascalmpl.library.Prelude;
import org.rascalmpl.repl.http.REPLContentServer;
import org.rascalmpl.repl.http.REPLContentServerManager;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.values.functions.IFunction;
Expand Down Expand Up @@ -56,8 +59,63 @@ public void registerLanguage(IConstructor language) {
public void applyDocumentsEdits(IList edits) {
applyFileSystemEdits(edits);
}

public void applyFileSystemEdits(IList edits) {
services.applyFileSystemEdits(edits);
var registry = URIResolverRegistry.getInstance();
var vf = IRascalValueFactory.getInstance();
edits.stream().map(IConstructor.class::cast).forEach(c -> {
try {
switch (c.getName()) {
case "removed": {
var file = (ISourceLocation) c.get("file");
registry.remove(file.top(), false);
break;
}
case "created": {
var file = (ISourceLocation) c.get("file");
if (registry.exists(file)) {
registry.setLastModified(file, System.currentTimeMillis());
} else {
try (var out = registry.getCharacterWriter(file.top(), registry.detectCharset(file).name(), false)) {
out.write("");
}
}
break;
}
case "renamed": {
var from = (ISourceLocation) c.get("from");
var to = (ISourceLocation) c.get("to");
registry.rename(from.top(), to.top(), true);
break;
}
case "changed": {
var file = (ISourceLocation) c.get("file");
if (c.has("edits")) {
var textEdits = (IList) c.get("edits");
var charset = registry.detectCharset(file).name();
var contents = Prelude.readFile(vf, false, ((ISourceLocation) c.get("file")).top(), charset, false);
for (var e : textEdits.reverse()) {
var edit = (IConstructor) e;
var range = (ISourceLocation) edit.get("range");
var sw = new StringWriter();
contents.substring(0, range.getOffset()).write(sw);
((IString) edit.get("replacement")).write(sw);
contents.substring(range.getOffset() + range.getLength()).write(sw);
contents = vf.string(sw.toString());
};
try (var writer = registry.getCharacterWriter(file.top(), charset, false)) {
writer.write(contents.getValue());
}
} else {
registry.setLastModified(file, System.currentTimeMillis());
}
break;
}
}
} catch (IOException e) {
services.warning("Could not execute FileSystemChange due to " + e.getMessage(), URIUtil.rootLocation("unknown"));
}
});
}

public void showInteractiveContent(IConstructor provider, IString title, IInteger viewColumn) {
Expand Down Expand Up @@ -94,7 +152,6 @@ public void logMessage(IConstructor message) {
services.logMessage(message);
}


public void registerDiagnostics(IList messages) {
services.registerDiagnostics(messages);
}
Expand Down