Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ default void checkProject() {
Changes syncDependencyCompilationUnits();

Changes syncCompilationUnit(WFile changedFilePath);

default Changes syncProjectFiles() {
return Changes.empty();
}

Changes syncCompilationUnitContent(WFile filename, String contents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public void reportBuildDiagnostics(List<CompileError> diagnostics) {
// hashcode for each compilation unit content as string
private final Map<WFile, Integer> fileHashcodes = new HashMap<>();

// public ABI hash for each package
private final Map<String, String> packageAbiHashes = new HashMap<>();

// file for each compilation unit; also read by lookups from other threads
private final Map<CompilationUnit, WFile> compilationunitFile = Collections.synchronizedMap(new WeakHashMap<>());

Expand Down Expand Up @@ -125,6 +128,7 @@ public Changes removeCompilationUnit(WFile resource) {
.flatMap(cu -> cu.getPackages().stream())
.map(WPackage::getName)
.collect(Collectors.toSet());
removedPackages.forEach(packageAbiHashes::remove);
jassNamesRemoved = toRemove.stream().anyMatch(ModelManagerImpl::declaresJassNames);
if (!toRemove.isEmpty()) {
Set<CompilationUnit> dependents =
Expand Down Expand Up @@ -160,6 +164,7 @@ public Changes removeCompilationUnit(WFile resource) {
@Override
public void clean() {
fileHashcodes.clear();
packageAbiHashes.clear();
parseErrors.clear();
model = null;
synchronized (modelLock) {
Expand Down Expand Up @@ -488,6 +493,49 @@ public synchronized Changes syncDependencyCompilationUnits() {
return changes;
}

@Override
public synchronized Changes syncProjectFiles() {
if (model == null) {
buildProject();
return Changes.empty();
}
Changes changes = Changes.empty();
File wurstFolder = new File(projectPath, "wurst");
Set<WFile> expectedProjectFiles = new HashSet<>();
if (wurstFolder.exists()) {
collectWurstFiles(wurstFolder, expectedProjectFiles);
}

WurstModel model2 = model;
if (model2 != null) {
List<WFile> loadedProjectFiles = model2.stream()
.map(this::wFile)
.filter(this::isUnderWurstFolder)
.collect(Collectors.toList());

for (WFile loadedFile : loadedProjectFiles) {
if (!expectedProjectFiles.contains(loadedFile)) {
changes = changes.mergeWith(removeCompilationUnit(loadedFile));
}
}
}

for (WFile f : expectedProjectFiles) {
changes = changes.mergeWith(syncCompilationUnit(f));
}
return changes;
}

private void collectWurstFiles(File dir, Set<WFile> result) {
for (File f : getFiles(dir)) {
if (f.isDirectory()) {
collectWurstFiles(f, result);
} else if (f.getName().endsWith(".wurst") || f.getName().endsWith(".jurst") || f.getName().endsWith(".j")) {
result.add(WFile.create(f));
}
}
}

private String getCanonicalPath(File f) {
try {
return f.getCanonicalPath();
Expand Down Expand Up @@ -604,6 +652,7 @@ private void doTypeCheck(WurstGui gui) {
synchronized (modelLock) {
uncheckedUnits.clear();
}
rememberAbiHashes(model2);
}
} catch (CompileError e) {
gui.sendError(e);
Expand Down Expand Up @@ -1002,8 +1051,8 @@ public Changes syncCompilationUnit(WFile f) {
replaceCompilationUnit(f, contents, true);
WLogger.debug("replaced file " + f);
WurstGui gui = new WurstGuiLogger();
doTypeCheckPartial(gui, ImmutableList.of(f), oldPackages, oldJassNames);
return new Changes(io.vavr.collection.HashSet.of(f), oldPackages, oldJassNames);
Collection<CompilationUnit> affectedCus = doTypeCheckPartial(gui, ImmutableList.of(f), oldPackages, oldJassNames);
return new Changes(getfileNames(affectedCus), oldPackages, oldJassNames);
}

private @Nullable String readCompilationUnitContents(WFile filename, boolean preferOpenBuffer) throws IOException {
Expand Down Expand Up @@ -1219,20 +1268,21 @@ public void onCompilationResult(Consumer<PublishDiagnosticsParams> f) {
onCompilationResultListeners.add(f);
}

private void doTypeCheckPartial(WurstGui gui, List<WFile> toCheckFilenames, Set<String> oldPackages,
boolean oldJassNames) {
private Collection<CompilationUnit> doTypeCheckPartial(WurstGui gui, List<WFile> toCheckFilenames,
Set<String> oldPackages, boolean oldJassNames) {
WLogger.debug("do typecheck partial of " + toCheckFilenames);
WurstCompilerJassImpl comp = getCompiler(gui);
List<CompilationUnit> toCheck = getCompilationUnits(toCheckFilenames);

WurstModel model2 = model;
if (model2 == null) {
return;
return Collections.emptyList();
}

Collection<CompilationUnit> toCheckRec = calculateCUsToUpdate(toCheck, oldPackages, oldJassNames, model2);

partialTypecheck(model2, toCheckRec, gui, comp);
return toCheckRec;
}

@Override
Expand Down Expand Up @@ -1277,11 +1327,20 @@ private void partialTypecheck(WurstModel model2, Collection<CompilationUnit> toC
synchronized (modelLock) {
uncheckedUnits.removeAll(toCheck);
}
rememberAbiHashes(toCheck);
}
List<WFile> fileNames = getfileNames(toCheck);
reportErrorsForFiles(fileNames, gui);
}

private void rememberAbiHashes(Collection<CompilationUnit> checked) {
for (CompilationUnit cu : checked) {
for (WPackage p : cu.getPackages()) {
packageAbiHashes.put(p.getName(), de.peeeq.wurstscript.validation.PackageAbi.computeAbiHash(p));
}
}
}

/**
* The given units, which are unchecked from now on, and every other unit of the model which still is
* unchecked, because an earlier check did not get to validate it.
Expand Down Expand Up @@ -1321,17 +1380,45 @@ private Set<CompilationUnit> calculateCUsToUpdate(Collection<CompilationUnit> ch
return result;
}

// get packages provided by the changed CUs
Stream<String> providedPackages = changed.stream()
.flatMap(cu -> cu.getPackages().stream())
.map(WPackage::getName);
// get packages provided by the changed CUs whose ABI actually changed
Set<String> affectedPackagesWithChangedAbi = new HashSet<>();
for (CompilationUnit cu : changed) {
for (WPackage p : cu.getPackages()) {
String newAbiHash = de.peeeq.wurstscript.validation.PackageAbi.computeAbiHash(p);
String oldAbiHash = packageAbiHashes.get(p.getName());
if (oldAbiHash == null || !oldAbiHash.equals(newAbiHash)) {
affectedPackagesWithChangedAbi.add(p.getName());
}
}
}

// affected packages are new ones and old ones
Set<String> affectedPackages = Stream.concat(providedPackages, oldPackages.stream())
// packages that were previously provided but are now removed
Set<String> currentlyProvided = changed.stream()
.flatMap(cu -> cu.getPackages().stream())
.map(WPackage::getName)
.collect(Collectors.toSet());
affectedPackages.addAll(configRelatives(affectedPackages));
for (String oldPkg : oldPackages) {
if (!currentlyProvided.contains(oldPkg)) {
affectedPackagesWithChangedAbi.add(oldPkg);
packageAbiHashes.remove(oldPkg);
}
}
// a config package and the package it configures resolve to each other's definitions whatever the edit, so
// what uses either is affected even when no public signature changed
Set<String> touchedPackages = new HashSet<>(currentlyProvided);
touchedPackages.addAll(oldPackages);
Set<String> relatives = configRelatives(touchedPackages);
boolean relativeInModel = model.stream()
.flatMap(cu -> cu.getPackages().stream())
.anyMatch(p -> relatives.contains(p.getName()));
if (relativeInModel) {
affectedPackagesWithChangedAbi.addAll(touchedPackages);
affectedPackagesWithChangedAbi.addAll(relatives);
}

addPossiblyAffectedPackages(affectedPackages, model, result);
if (!affectedPackagesWithChangedAbi.isEmpty()) {
addPossiblyAffectedPackages(affectedPackagesWithChangedAbi, model, result);
}

return result;
}
Expand Down Expand Up @@ -1514,6 +1601,18 @@ private boolean isUnderDependenciesFolder(WFile file) {
}
}

private boolean isUnderWurstFolder(WFile file) {
try {
Path filePath = file.getPath().toAbsolutePath().normalize();
Path wurstRoot = Paths.get(projectPath.getAbsolutePath(), "wurst")
.toAbsolutePath()
.normalize();
return filePath.startsWith(wurstRoot) && Utils.isWurstFile(filePath.toString());
} catch (FileNotFoundException e) {
return false;
}
}


public File getProjectPath() {
return projectPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private static void printFirstNewline(Element e, StringBuilder sb, int indent) {
if (e.getParent().get(0).equals(e)) {
return;
}
if (sb.charAt(sb.length() - 1) == '\n' && sb.charAt(sb.length() - 2) == '\n') {
if (sb.length() >= 2 && sb.charAt(sb.length() - 1) == '\n' && sb.charAt(sb.length() - 2) == '\n') {
return;
}
sb.append("\n");
Expand Down
Loading
Loading