From 1f0f7d68e7a76c8768b0318a3cf2419686e1b97a Mon Sep 17 00:00:00 2001 From: Martin Jobst Date: Fri, 2 Aug 2024 20:00:06 +0200 Subject: [PATCH 1/2] Avoid unnecessary text replacer contexts #3119 Avoid unnecessary text replacer contexts by returning the current context when the resulting context would be identical. Signed-off-by: Martin Jobst --- .../xtext/formatting2/internal/TextReplacerContext.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/formatting2/internal/TextReplacerContext.java b/org.eclipse.xtext/src/org/eclipse/xtext/formatting2/internal/TextReplacerContext.java index 379985865ae..d1bc432aed3 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/formatting2/internal/TextReplacerContext.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/formatting2/internal/TextReplacerContext.java @@ -330,6 +330,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(); @@ -338,11 +341,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(); From 4f5f3d6ff0828eda167e3e90ea6e3998cb7ba5e1 Mon Sep 17 00:00:00 2001 From: Martin Jobst Date: Fri, 2 Aug 2024 20:00:10 +0200 Subject: [PATCH 2/2] Use replacer region for calculating leading chars in line #3119 When calculating the leading chars in a line, the replacer context iterates over all previous contexts if there are no replacements. This change also uses the region of the associated replacer to find the beginning of the current line. Signed-off-by: Martin Jobst --- .../formatting2/internal/TextReplacerContext.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/formatting2/internal/TextReplacerContext.java b/org.eclipse.xtext/src/org/eclipse/xtext/formatting2/internal/TextReplacerContext.java index d1bc432aed3..d40530b3a2c 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/formatting2/internal/TextReplacerContext.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/formatting2/internal/TextReplacerContext.java @@ -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);