Tighten the tooling package and document it

JsonText only reads now; splicing text is the host's, as the studio makes
every write. JsonPath steps are a sealed Key or Index. Absent values are
empty instead of -1 or blank: GraphNode.part, BlockPattern.pickAt and the
entries' group. GraphNode's child list, movability and additions are one
Edits record.

README and DESIGN describe the tooling package, CONTRACTS maps it, and the
docs no longer name the types the last batch removed.
This commit is contained in:
piexel committed 2026-11-06 06:09:12 +01:00
1 parent 2b6f96e5c2
commit cdd138d9f4
13 files changed
+213 -298

No files matched your search

@@ -1,14 +1,13 @@
package com.volmit.tooling;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* A JSON file that remembers where each value is written, so an edit rewrites only the value it changes and the rest
* of the file keeps the layout its author gave it. Comments are passed over, as the pack reader allows them.
* A JSON file that remembers where each value is written, so the studio can edit one value and leave the rest of the
* file as its author laid it out. Comments are passed over, as the pack reader allows them.
*/
public final class JsonText {
private final String text;
@@ -41,28 +40,6 @@ public final class JsonText {
}
}
/** {@code plain} as a JSON string literal. */
public static String quote(String plain) {
StringBuilder quoted = new StringBuilder("\"");
for (char character : plain.toCharArray()) {
switch (character) {
case '"' -> quoted.append("\\\"");
case '\\' -> quoted.append("\\\\");
case '\n' -> quoted.append("\\n");
case '\r' -> quoted.append("\\r");
case '\t' -> quoted.append("\\t");
default -> {
if (character < 0x20) {
quoted.append(String.format("\\u%04x", (int) character));
} else {
quoted.append(character);
}
}
}
}
return quoted.append('"').toString();
}
public String text() {
return text;
}
@@ -74,7 +51,7 @@ public final class JsonText {
/** The value at {@code path}; empty when the file has none there. */
public Optional<Value> at(JsonPath path) {
Value value = root;
for (Object step : path.steps()) {
for (JsonPath.Step step : path.steps()) {
Optional<Value> inside = stepInto(value, step);
if (inside.isEmpty()) {
return Optional.empty();
@@ -89,121 +66,16 @@ public final class JsonText {
return text.substring(value.start(), value.end());
}
/** The text of {@code value}, its later lines indented as if it started a line with no indent. */
public String detachedTextOf(Value value) {
return reindented(textOf(value), indentOfLine(value.start()), "");
}
public Splice replace(Value value, String literal) {
return new Splice(value.start(), value.end(), literal);
}
/** Sets the member, written after the last one in the object's own style when it is new. */
public Splice put(Obj object, String key, String literal) {
Optional<Member> existing = object.member(key);
if (existing.isPresent()) {
return replace(existing.get().value(), literal);
private static Optional<Value> stepInto(Value value, JsonPath.Step step) {
if (step instanceof JsonPath.Key(String name) && value instanceof Obj object) {
return object.get(name);
}
if (object.members().isEmpty()) {
return replace(object, "{" + quote(key) + ": " + reindented(literal, "", indentOfLine(object.start())) + "}");
}
Member last = object.members().getLast();
String indent = indentOfLine(last.keyStart());
String entry = quote(key) + ": " + reindented(literal, "", indent);
String separator = isMultiline(object) ? ",\n" + indent : ", ";
return new Splice(last.value().end(), last.value().end(), separator + entry);
}
/** @throws IllegalArgumentException when the object has no member {@code key} */
public Splice remove(Obj object, String key) {
List<Member> members = object.members();
int index = members.indexOf(object.member(key).orElseThrow(() -> new IllegalArgumentException("No " + key + " to remove")));
if (members.size() == 1) {
return replace(object, "{}");
}
if (index > 0) {
return new Splice(members.get(index - 1).value().end(), members.get(index).value().end(), "");
}
return new Splice(members.getFirst().keyStart(), members.get(1).keyStart(), "");
}
/** Adds an element after the last one, a multi-line one indented to match the array. */
public Splice append(Arr array, String literal) {
if (array.elements().isEmpty()) {
return replace(array, "[" + reindented(literal, "", indentOfLine(array.start())) + "]");
}
Value last = array.elements().getLast();
String indent = indentOfLine(last.start());
String separator = isMultiline(array) ? ",\n" + indent : ", ";
return new Splice(last.end(), last.end(), separator + reindented(literal, "", indent));
}
public Splice removeElement(Arr array, int index) {
List<Value> elements = array.elements();
Objects.checkIndex(index, elements.size());
if (elements.size() == 1) {
return replace(array, "[]");
}
if (index > 0) {
return new Splice(elements.get(index - 1).end(), elements.get(index).end(), "");
}
return new Splice(elements.getFirst().start(), elements.get(1).start(), "");
}
/**
* The text with {@code splices} made, each placed by where it was in this text.
*
* @throws IllegalArgumentException when two splices overlap
*/
public String apply(List<Splice> splices) {
List<Splice> fromLast = new ArrayList<>(splices);
fromLast.sort(Comparator.comparingInt(Splice::start).reversed());
StringBuilder edited = new StringBuilder(text);
int earliestStart = Integer.MAX_VALUE;
for (Splice splice : fromLast) {
if (splice.end() > earliestStart) {
throw new IllegalArgumentException("Overlapping edits");
}
edited.replace(splice.start(), splice.end(), splice.replacement());
earliestStart = splice.start();
}
return edited.toString();
}
private static Optional<Value> stepInto(Value value, Object step) {
if (step instanceof String key && value instanceof Obj object) {
return object.get(key);
}
if (step instanceof Integer index && value instanceof Arr array && index < array.elements().size()) {
return Optional.of(array.elements().get(index));
if (step instanceof JsonPath.Index(int position) && value instanceof Arr array && position < array.elements().size()) {
return Optional.of(array.elements().get(position));
}
return Optional.empty();
}
private boolean isMultiline(Value value) {
return textOf(value).indexOf('\n') >= 0;
}
// The blanks before the first character on the line holding position.
private String indentOfLine(int position) {
int lineStart = text.lastIndexOf('\n', position - 1) + 1;
int indentEnd = lineStart;
while (indentEnd < position && (text.charAt(indentEnd) == ' ' || text.charAt(indentEnd) == '\t')) {
indentEnd++;
}
return text.substring(lineStart, indentEnd);
}
private static String reindented(String literal, String fromIndent, String toIndent) {
String[] lines = literal.split("\n", -1);
StringBuilder result = new StringBuilder(lines[0]);
for (int index = 1; index < lines.length; index++) {
String line = lines[index].startsWith(fromIndent) ? lines[index].substring(fromIndent.length()) : lines[index].stripLeading();
result.append('\n').append(toIndent).append(line);
}
return result.toString();
}
/** A value of the file, from {@code start} up to {@code end} in its text. */
public sealed interface Value permits Obj, Arr, Literal {
int start();
@@ -242,16 +114,6 @@ public final class JsonText {
}
}
/** Replaces the text from {@code start} up to {@code end} with {@code replacement}. */
public record Splice(int start, int end, String replacement) {
public Splice {
Objects.requireNonNull(replacement, "replacement");
if (start < 0 || end < start) {
throw new IllegalArgumentException("A splice runs forwards from 0, not " + start + " to " + end);
}
}
}
private static final class Parser {
private final String text;
private int position;