Reduces the complexity of convertYAMLMappingsToComments

This commit is contained in:
2023-04-21 14:53:23 +02:00
parent 05fadfa558
commit e07adacf73
2 changed files with 79 additions and 38 deletions

View File

@@ -459,11 +459,13 @@ public final class StargateConfig {
* @param currentConfiguration <p>The current config to back up</p>
*/
private void migrateConfig(FileConfiguration currentConfiguration) {
String debugPath = "StargateConfig::migrateConfig";
//Save the old config just in case something goes wrong
try {
currentConfiguration.save(new File(dataFolderPath, "config.yml.old"));
} catch (IOException e) {
Stargate.debug("StargateConfig::migrateConfig", "Unable to save old backup and do migration");
Stargate.debug(debugPath, "Unable to save old backup and do migration");
return;
}
@@ -481,7 +483,7 @@ public final class StargateConfig {
FileHelper.getInputStreamForInternalFile("/config-migrations.txt")), "=",
ColorConversion.NORMAL);
} catch (IOException e) {
Stargate.debug("StargateConfig::migrateConfig", "Unable to load config migration file");
Stargate.debug(debugPath, "Unable to load config migration file");
return;
}
@@ -502,7 +504,7 @@ public final class StargateConfig {
if (oldConfiguration.get(key) instanceof MemorySection) {
continue;
}
Stargate.debug("StargateConfig::migrateConfig", "Setting " + key + " to " +
Stargate.debug(debugPath, "Setting " + key + " to " +
oldConfiguration.get(key));
newConfiguration.set(key, oldConfiguration.get(key));
}
@@ -510,7 +512,7 @@ public final class StargateConfig {
try {
newConfiguration.save(new File(dataFolderPath, "config.yml"));
} catch (IOException exception) {
Stargate.debug("StargateConfig::migrateConfig", "Unable to save migrated config");
Stargate.debug(debugPath, "Unable to save migrated config");
}
Stargate.getInstance().reloadConfig();

View File

@@ -60,26 +60,52 @@ public class StargateYamlConfiguration extends YamlConfiguration {
previousIndentation = getIndentation(line);
}
//Temporarily store the comment line
if (trimmed.startsWith("# ")) {
currentComment.add(trimmed.replaceFirst("# ", START_OF_COMMENT_LINE));
addComment(currentComment, trimmed);
} else {
currentComment.add(trimmed.replaceFirst("#", START_OF_COMMENT_LINE));
addYamlString(yamlBuilder, currentComment, line, previousIndentation, commentId);
commentId++;
previousIndentation = 0;
}
} else {
}
return yamlBuilder.toString();
}
/**
* Adds a YAML string to the given string builder
*
* @param yamlBuilder <p>The string builder used for building YAML</p>
* @param currentComment <p>The comment to add as a YAML string</p>
* @param line <p>The current line</p>
* @param previousIndentation <p>The indentation of the current block comment</p>
* @param commentId <p>The id of the comment</p>
*/
private void addYamlString(StringBuilder yamlBuilder, List<String> currentComment, String line,
int previousIndentation, int commentId) {
String trimmed = line.trim();
//Write the full formatted comment to the StringBuilder
if (!currentComment.isEmpty()) {
int indentation = trimmed.isEmpty() ? previousIndentation : getIndentation(line);
generateCommentYAML(yamlBuilder, currentComment, commentId++, indentation);
generateCommentYAML(yamlBuilder, currentComment, commentId, indentation);
currentComment.clear();
previousIndentation = 0;
}
//Add the non-comment line assuming it isn't empty
if (!trimmed.isEmpty()) {
yamlBuilder.append(line).append("\n");
}
}
/**
* Adds the given comment to the given list
*
* @param commentParts <p>The list to add to</p>
* @param comment <p>The comment to add</p>
*/
private void addComment(List<String> commentParts, String comment) {
if (comment.startsWith("# ")) {
commentParts.add(comment.replaceFirst("# ", START_OF_COMMENT_LINE));
} else {
commentParts.add(comment.replaceFirst("#", START_OF_COMMENT_LINE));
}
return yamlBuilder.toString();
}
/**
@@ -114,36 +140,49 @@ public class StargateYamlConfiguration extends YamlConfiguration {
*/
private String convertYAMLMappingsToComments(String yamlString) {
StringBuilder finalText = new StringBuilder();
boolean isReadingCommentBlock = false;
int commentIndentation = 0;
for (String line : yamlString.split("\n")) {
String[] lines = yamlString.split("\n");
for (int currentIndex = 0; currentIndex < lines.length; currentIndex++) {
String line = lines[currentIndex];
String possibleComment = line.trim();
if (isReadingCommentBlock && line.contains(END_OF_COMMENT)) {
//Skip the line signifying the end of a comment
isReadingCommentBlock = false;
} else if (possibleComment.startsWith(START_OF_COMMENT)) {
//Skip the comment start line, and start comment parsing
isReadingCommentBlock = true;
//Get the indentation to use for the comment block
commentIndentation = getIndentation(line);
if (possibleComment.startsWith(START_OF_COMMENT)) {
//Add an empty line before every comment block
finalText.append("\n");
} else if (line.isEmpty() && !isReadingCommentBlock) {
//Output the empty line as-is, as it's not part of a comment
finalText.append("\n");
} else if (isReadingCommentBlock) {
possibleComment = possibleComment.replace(START_OF_COMMENT_LINE, "");
//Output the comment with correct indentation
finalText.append(addIndentation(commentIndentation)).append("# ").append(possibleComment).append("\n");
currentIndex = readComment(finalText, lines, currentIndex + 1, getIndentation(line));
} else {
//Output the configuration key
finalText.append(line).append("\n");
}
}
return finalText.toString().trim();
}
/**
* Fully reads a comment
*
* @param builder <p>The string builder to write to</p>
* @param lines <p>The lines to read from</p>
* @param startIndex <p>The index to start reading from</p>
* @param commentIndentation <p>The indentation of the read comment</p>
* @return <p>The index containing the next non-comment line</p>
*/
private int readComment(StringBuilder builder, String[] lines, int startIndex, int commentIndentation) {
for (int currentIndex = startIndex; currentIndex < lines.length; currentIndex++) {
String line = lines[currentIndex];
String possibleComment = line.trim();
if (!line.contains(END_OF_COMMENT)) {
possibleComment = possibleComment.replace(START_OF_COMMENT_LINE, "");
builder.append(addIndentation(commentIndentation)).append("# ").append(possibleComment).append("\n");
} else {
return currentIndex;
}
}
return startIndex;
}
/**
* Gets a string containing the given indentation
*