From 90e95ab4eb6c4aa06fd1b894255582964631e533 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Fri, 31 Oct 2014 15:38:25 -0700 Subject: [PATCH] Bug 419399 - Preference for Format with no selection More robust implementation of region expansion. --- .../internal/formatter/CCodeFormatter.java | 19 ++++++++++++------- .../cdt/internal/ui/editor/CEditor.java | 2 ++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java index 5467e1568ef..0eb81128721 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java @@ -194,10 +194,10 @@ public class CCodeFormatter extends CodeFormatter { IASTTranslationUnit ast) { for (int i = 0; i < regions.length; i++) { IRegion region = regions[i]; - if (region.getLength() == 0) { + if (shouldFormatWholeStatements()) { // An empty region is replaced by the region containing the line corresponding to // the offset and all statements overlapping with that line. - region = getLineOrStatementRegion(source, region.getOffset(), ast); + region = getLineOrStatementRegion(source, region, ast); } CodeFormatterVisitor codeFormatter = new CodeFormatterVisitor(preferences, region.getOffset(), region.getLength()); @@ -209,13 +209,18 @@ public class CCodeFormatter extends CodeFormatter { } } + private boolean shouldFormatWholeStatements() { + Object obj = options.get(DefaultCodeFormatterConstants.FORMATTER_STATEMENT_SCOPE); + return obj instanceof Boolean && ((Boolean) obj).booleanValue(); + } + /** - * Returns the smallest region containing the line corresponding to the given offset and all - * statements overlapping with that line. + * Returns the smallest region containing the lines overlapping with the given region and all + * statements overlapping with those lines. */ - private IRegion getLineOrStatementRegion(String source, int offset, IASTTranslationUnit ast) { - int start = TextUtil.getLineStart(source, offset); - int end = TextUtil.skipToNextLine(source, offset); + private IRegion getLineOrStatementRegion(String source, IRegion region, IASTTranslationUnit ast) { + int start = TextUtil.getLineStart(source, region.getOffset()); + int end = TextUtil.skipToNextLine(source, region.getOffset() + region.getLength()); IASTNode node = findOverlappingPreprocessorStatement(start, end, ast); if (node != null) { IASTFileLocation location = node.getFileLocation(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java index 645e8bc1692..2b91ba52433 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java @@ -421,6 +421,8 @@ public class CEditor extends TextEditor implements ICEditor, ISelectionChangedLi PreferenceConstants.FORMATTING_SCOPE_DOCUMENT, null); if (PreferenceConstants.FORMATTING_SCOPE_DOCUMENT.equals(scope)) { formatWholeDocument = true; + } else { + preferences.put(DefaultCodeFormatterConstants.FORMATTER_STATEMENT_SCOPE, Boolean.TRUE); } } if (!formatWholeDocument) {