1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-09 09:15:38 +02:00

Bug 419399 - Preference for Format with no selection

More robust implementation of region expansion.
This commit is contained in:
Sergey Prigogin 2014-10-31 15:38:25 -07:00
parent 31119f627e
commit 90e95ab4eb
2 changed files with 14 additions and 7 deletions

View file

@ -194,10 +194,10 @@ public class CCodeFormatter extends CodeFormatter {
IASTTranslationUnit ast) { IASTTranslationUnit ast) {
for (int i = 0; i < regions.length; i++) { for (int i = 0; i < regions.length; i++) {
IRegion region = regions[i]; IRegion region = regions[i];
if (region.getLength() == 0) { if (shouldFormatWholeStatements()) {
// An empty region is replaced by the region containing the line corresponding to // An empty region is replaced by the region containing the line corresponding to
// the offset and all statements overlapping with that line. // the offset and all statements overlapping with that line.
region = getLineOrStatementRegion(source, region.getOffset(), ast); region = getLineOrStatementRegion(source, region, ast);
} }
CodeFormatterVisitor codeFormatter = CodeFormatterVisitor codeFormatter =
new CodeFormatterVisitor(preferences, region.getOffset(), region.getLength()); 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 * Returns the smallest region containing the lines overlapping with the given region and all
* statements overlapping with that line. * statements overlapping with those lines.
*/ */
private IRegion getLineOrStatementRegion(String source, int offset, IASTTranslationUnit ast) { private IRegion getLineOrStatementRegion(String source, IRegion region, IASTTranslationUnit ast) {
int start = TextUtil.getLineStart(source, offset); int start = TextUtil.getLineStart(source, region.getOffset());
int end = TextUtil.skipToNextLine(source, offset); int end = TextUtil.skipToNextLine(source, region.getOffset() + region.getLength());
IASTNode node = findOverlappingPreprocessorStatement(start, end, ast); IASTNode node = findOverlappingPreprocessorStatement(start, end, ast);
if (node != null) { if (node != null) {
IASTFileLocation location = node.getFileLocation(); IASTFileLocation location = node.getFileLocation();

View file

@ -421,6 +421,8 @@ public class CEditor extends TextEditor implements ICEditor, ISelectionChangedLi
PreferenceConstants.FORMATTING_SCOPE_DOCUMENT, null); PreferenceConstants.FORMATTING_SCOPE_DOCUMENT, null);
if (PreferenceConstants.FORMATTING_SCOPE_DOCUMENT.equals(scope)) { if (PreferenceConstants.FORMATTING_SCOPE_DOCUMENT.equals(scope)) {
formatWholeDocument = true; formatWholeDocument = true;
} else {
preferences.put(DefaultCodeFormatterConstants.FORMATTER_STATEMENT_SCOPE, Boolean.TRUE);
} }
} }
if (!formatWholeDocument) { if (!formatWholeDocument) {