Skip to content

Commit c3d8582

Browse files
authored
Fix file sorting when previously matched (#261)
* use StringUtils match instead of split length * putAndMoveToLast and comment
1 parent 668102b commit c3d8582

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/main/java/com/cleanroommc/groovyscript/sandbox/SandboxData.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.cleanroommc.groovyscript.api.GroovyLog;
44
import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
5+
import org.apache.commons.lang3.StringUtils;
56
import org.apache.logging.log4j.Logger;
67
import org.jetbrains.annotations.ApiStatus;
78
import org.jetbrains.annotations.NotNull;
@@ -147,16 +148,19 @@ static Collection<File> getSortedFilesOf(File root, Collection<String> paths) {
147148
if (!rootFile.exists()) {
148149
continue;
149150
}
150-
int pathSize = path.split(separator).length;
151+
// if we are looking at a specific file, we don't want that to be overridden.
152+
// otherwise, we want to use the specificity based on the number of file separators.
153+
int pathSize = StringUtils.countMatches(path, separator);
151154
try (Stream<Path> stream = Files.walk(rootFile.toPath())) {
152155
stream.filter(path1 -> isGroovyFile(path1.toString()))
153156
.map(Path::toFile)
154157
//.filter(Preprocessor::validatePreprocessors)
155158
.sorted(Comparator.comparing(File::getPath))
156159
.forEach(file -> {
157160
if (files.containsKey(file)) {
161+
// if the file already exists, push the priority down if we are more specific than the already existing entry
158162
if (pathSize > files.getInt(file)) {
159-
files.put(file, pathSize);
163+
files.putAndMoveToLast(file, pathSize);
160164
}
161165
} else {
162166
files.put(file, pathSize);

0 commit comments

Comments
 (0)