Skip to content

Commit

Permalink
Merge pull request #3120 from mx990/optimizeTextReplacerContext
Browse files Browse the repository at this point in the history
Optimize performance of text replacer context with autowrap and already formatted document
  • Loading branch information
cdietrich authored Aug 8, 2024
2 parents 7f5d3d5 + 4f5f3d6 commit 2bce8b6
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ public int getLeadingCharsInLineCount() {
count += logicalLength(text);
lastOffset = rep.getOffset();
}
final ITextReplacer replacer = current.getReplacer();
if (replacer != null) {
final int offset = replacer.getRegion().getOffset();
if (offset < lastOffset) {
final String text = access.textForOffset(offset, lastOffset - offset);
final int idx = text.lastIndexOf('\n');
if (idx >= 0) {
return count + logicalLength(text.substring(idx + 1));
}
count += logicalLength(text);
lastOffset = offset;
}
}
current = current.getPreviousContext();
}
String rest = access.textForOffset(0, lastOffset);
Expand Down Expand Up @@ -330,6 +343,9 @@ protected String toStringLocal() {

@Override
public ITextReplacerContext withDocument(IFormattableDocument document) {
if (document == this.document) {
return this;
}
TextReplacerContext context = new TextReplacerContext(document, this, indentation, null);
if (this.nextReplacerIsChild)
context.setNextReplacerIsChild();
Expand All @@ -338,11 +354,17 @@ public ITextReplacerContext withDocument(IFormattableDocument document) {

@Override
public ITextReplacerContext withIndentation(int indentation) {
if(indentation == this.indentation) {
return this;
}
return new TextReplacerContext(document, this, indentation, null);
}

@Override
public ITextReplacerContext withReplacer(ITextReplacer replacer) {
if(replacer == this.replacer) {
return this;
}
ITextReplacerContext current = this;
while (current != null) {
ITextReplacer lastReplacer = current.getReplacer();
Expand Down

0 comments on commit 2bce8b6

Please sign in to comment.