package bigguy.texteditor.render; import noppes.npcs.client.gui.util.script.interpreter.token.TokenType; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.IdentityHashMap; import java.util.List; import java.util.Set; /** * Merges three independent mark sources — syntax, semantic, and error — into * a single non-overlapping list resolved by {@link TokenType#getPriority()}. * *
Thread safety: syntax marks are set from the UI thread while semantic marks * arrive from a background analysis thread. All mutators are synchronized; * {@link #merge()} produces a snapshot safe for the caller to iterate without * holding the lock. * *
A monotonically increasing {@link #getRevision() revision} counter lets
* consumers detect stale merge results cheaply.
*/
public class MarkLayer {
private List syntaxMarks = Collections.emptyList();
private List semanticMarks = Collections.emptyList();
private List errorMarks = Collections.emptyList();
private volatile int revision;
/**
* Replace the syntax mark layer (produced by tree-sitter highlight queries).
*
* @param marks new syntax marks; must not be null
*/
public synchronized void setSyntaxMarks(List marks) {
this.syntaxMarks = new ArrayList<>(marks);
revision++;
}
/**
* Replace the semantic mark layer (produced by type resolution / scope analysis).
* Typically called from a background thread.
*
* @param marks new semantic marks; must not be null
*/
public synchronized void setSemanticMarks(List marks) {
this.semanticMarks = new ArrayList<>(marks);
revision++;
}
/**
* Replace the error mark layer (diagnostic underlines).
* Error marks do not participate in color conflict resolution —
* they are kept in a separate channel and drawn as underlines.
*
* @param marks new error marks; must not be null
*/
public synchronized void setErrorMarks(List marks) {
this.errorMarks = new ArrayList<>(marks);
revision++;
}
/** @return the current syntax marks (defensive copy) */
public synchronized List getSyntaxMarks() {
return new ArrayList<>(syntaxMarks);
}
/** @return the current semantic marks (defensive copy) */
public synchronized List getSemanticMarks() {
return new ArrayList<>(semanticMarks);
}
/**
* Error marks are kept in their own channel and drawn as underlines
* rather than merged into the color layer.
*
* @return the current error marks (defensive copy)
*/
public synchronized List getErrorMarks() {
return new ArrayList<>(errorMarks);
}
/**
* Monotonically increasing counter that increments on every mutation.
* Compare against a cached value to detect stale merge results.
*
* @return current revision
*/
public int getRevision() {
return revision;
}
/**
* Merge syntax and semantic marks into a single non-overlapping list.
* Error marks are NOT included — they live in a separate rendering channel.
*
* Algorithm:
*
*
*
* @return merged, non-overlapping mark list (safe to iterate without synchronization)
*/
public List merge() {
List syntax;
List semantic;
synchronized (this) {
syntax = new ArrayList<>(syntaxMarks);
semantic = new ArrayList<>(semanticMarks);
}
if (syntax.isEmpty() && semantic.isEmpty()) {
return Collections.emptyList();
}
// Build identity set for O(1) semantic membership checks
Set semanticSet = Collections.newSetFromMap(new IdentityHashMap<>());
semanticSet.addAll(semantic);
// Collect all boundary points
List