Skip to content

Commit

Permalink
Cache the value of internaltext in a unit buffer when it is not alter…
Browse files Browse the repository at this point in the history
…ed. (#378)

Mark a buffer dirty on any operation.
  • Loading branch information
johanvos authored Nov 18, 2024
1 parent 5e43e95 commit 501ff69
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion rta/src/main/java/com/gluonhq/richtextarea/model/UnitBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class UnitBuffer {
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

private final List<Unit> unitList;
private volatile boolean dirty = true;
private String internalText = "";

public UnitBuffer() {
this(List.of());
Expand Down Expand Up @@ -93,9 +95,12 @@ public String getText() {
* @return a string with the internal text representation
*/
public String getInternalText() {
if (!dirty) return internalText;
final StringBuilder sb = new StringBuilder();
unitList.forEach(unit -> sb.append(unit.getInternalText()));
return sb.toString();
this.internalText = sb.toString();
this.dirty = false;
return this.internalText;
}

/**
Expand All @@ -112,6 +117,7 @@ public int length() {
*/
public void append(Unit unit) {
unitList.add(unit);
this.dirty = true;
}

/**
Expand All @@ -120,6 +126,7 @@ public void append(Unit unit) {
*/
public void append(List<Unit> units) {
unitList.addAll(units);
this.dirty = true;
}

/**
Expand Down Expand Up @@ -162,6 +169,7 @@ public void insert(Unit unit, int position) {
}
unitList.clear();
unitList.addAll(buffer);
this.dirty = true;
}

/**
Expand All @@ -179,6 +187,7 @@ public void remove(int start, int end) {
insert(unit, Math.max(start, end));
insert(unit, Math.min(start, end));
unitList.subList(unitList.indexOf(unit), unitList.lastIndexOf(unit) + 1).clear();
this.dirty = true;
}

/**
Expand Down

0 comments on commit 501ff69

Please sign in to comment.