From 6117b71893e6eca3d6425090e9fb25f38b8cc253 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Fri, 17 Jan 2025 15:15:05 -0500 Subject: [PATCH] Fix insert of annotation to not change indent of following line (#3566) - add new method computeIndentInSpaces() to ASTRewriteFormatter - modify ASTRewriteAnalyzer.rewriteModifiers2() to add spaces as needed to separator of added annotation to match current indent - needed for https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/1940 --- .../jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java | 3 +++ .../jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java index 94d00d72b08..9d5a7801ba3 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java @@ -1733,6 +1733,9 @@ else if (parent instanceof Block || parent instanceof TryStatement || parent ins String separator; if (lastChild.getNewValue() instanceof Annotation) { separator= formatterPrefix.getPrefix(getIndent(pos)); + for (int i= 0; i < this.formatter.computeIndentInSpaces(getIndentOfLine(pos)) % this.formatter.getTabWidth(); ++i) { + separator += " "; //$NON-NLS-1$ + } } else { separator= String.valueOf(' '); } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java index f496a9eb637..9f61dfd21cd 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java @@ -212,6 +212,10 @@ public int computeIndentUnits(String line) { return IndentManipulation.measureIndentUnits(line, this.tabWidth, this.indentWidth); } + public int computeIndentInSpaces(String line) { + return IndentManipulation.measureIndentInSpaces(line, this.tabWidth); + } + /** * Evaluates the edit on the given string. * @param string The string to format