mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-25 01:45:33 +02:00
Formatter improvements.
This commit is contained in:
parent
c1b808e0ff
commit
35255ca2cc
3 changed files with 41 additions and 20 deletions
|
@ -507,8 +507,8 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Answer next indentation level based on column estimated position (if
|
* Returns next indentation level based on column estimated position (if
|
||||||
* column is not indented, then use indentationLevel)
|
* column is not indented, then uses indentationLevel)
|
||||||
*/
|
*/
|
||||||
public int getNextIndentationLevel(int someColumn) {
|
public int getNextIndentationLevel(int someColumn) {
|
||||||
int indent= someColumn - 1;
|
int indent= someColumn - 1;
|
||||||
|
|
|
@ -68,8 +68,9 @@ public class Alignment {
|
||||||
public int shiftBreakIndentationLevel;
|
public int shiftBreakIndentationLevel;
|
||||||
public int[] fragmentBreaks;
|
public int[] fragmentBreaks;
|
||||||
public boolean wasSplit;
|
public boolean wasSplit;
|
||||||
|
public int currentFragmentStartLine;
|
||||||
|
|
||||||
public Scribe scribe;
|
public final Scribe scribe;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Alignment modes
|
* Alignment modes
|
||||||
|
@ -168,14 +169,17 @@ public class Alignment {
|
||||||
this.wasSplit = false;
|
this.wasSplit = false;
|
||||||
this.fragmentIndentations = new int[this.fragmentCount];
|
this.fragmentIndentations = new int[this.fragmentCount];
|
||||||
this.fragmentBreaks = new int[this.fragmentCount];
|
this.fragmentBreaks = new int[this.fragmentCount];
|
||||||
|
this.currentFragmentStartLine = this.scribe.line;
|
||||||
|
|
||||||
// Initialize the break indentation level, using modes and continuationIndentationLevel preference
|
|
||||||
final int indentSize = this.scribe.indentationSize;
|
|
||||||
int currentColumn = this.location.outputColumn;
|
int currentColumn = this.location.outputColumn;
|
||||||
if (currentColumn == 1) {
|
if (currentColumn == 1) {
|
||||||
currentColumn = this.location.outputIndentationLevel + 1;
|
currentColumn = this.location.outputIndentationLevel + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize the break indentation level, using modes and continuationIndentationLevel
|
||||||
|
// preference.
|
||||||
|
final int indentSize = this.scribe.indentationSize;
|
||||||
|
|
||||||
if ((mode & M_INDENT_ON_COLUMN) != 0) {
|
if ((mode & M_INDENT_ON_COLUMN) != 0) {
|
||||||
// Indent broken fragments at next indentation level, based on current column
|
// Indent broken fragments at next indentation level, based on current column
|
||||||
this.breakIndentationLevel = this.scribe.getNextIndentationLevel(currentColumn);
|
this.breakIndentationLevel = this.scribe.getNextIndentationLevel(currentColumn);
|
||||||
|
@ -185,11 +189,18 @@ public class Alignment {
|
||||||
if (continuationIndent == 0) {
|
if (continuationIndent == 0) {
|
||||||
this.fragmentBreaks[0] = BREAK_NOT_ALLOWED;
|
this.fragmentBreaks[0] = BREAK_NOT_ALLOWED;
|
||||||
}
|
}
|
||||||
} else if ((mode & M_INDENT_BY_ONE) != 0) {
|
|
||||||
// Indent broken fragments exactly one level deeper than current indentation
|
|
||||||
this.breakIndentationLevel = this.location.outputIndentationLevel + indentSize;
|
|
||||||
} else {
|
} else {
|
||||||
this.breakIndentationLevel = this.location.outputIndentationLevel + continuationIndent * indentSize;
|
int baseIndentationLevel = this.location.outputIndentationLevel;
|
||||||
|
if (name != TRAILING_TEXT && this.scribe.currentAlignment != null &&
|
||||||
|
(this.scribe.currentAlignment.mode & M_INDENT_ON_COLUMN) != 0) {
|
||||||
|
baseIndentationLevel = this.scribe.getNextIndentationLevel(currentColumn);
|
||||||
|
}
|
||||||
|
if ((mode & M_INDENT_BY_ONE) != 0) {
|
||||||
|
// Indent broken fragments exactly one level deeper than current indentation
|
||||||
|
this.breakIndentationLevel = baseIndentationLevel + indentSize;
|
||||||
|
} else {
|
||||||
|
this.breakIndentationLevel = baseIndentationLevel + continuationIndent * indentSize;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.shiftBreakIndentationLevel = this.breakIndentationLevel + indentSize;
|
this.shiftBreakIndentationLevel = this.breakIndentationLevel + indentSize;
|
||||||
|
|
||||||
|
@ -203,7 +214,7 @@ public class Alignment {
|
||||||
if (this.chunkKind != kind) {
|
if (this.chunkKind != kind) {
|
||||||
this.chunkKind = kind;
|
this.chunkKind = kind;
|
||||||
|
|
||||||
// when redoing same chunk alignment, must not reset
|
// When redoing same chunk alignment, must not reset
|
||||||
if (startIndex != this.chunkStartIndex) {
|
if (startIndex != this.chunkStartIndex) {
|
||||||
this.chunkStartIndex = startIndex;
|
this.chunkStartIndex = startIndex;
|
||||||
this.location.update(this.scribe, sourceRestart);
|
this.location.update(this.scribe, sourceRestart);
|
||||||
|
@ -216,7 +227,7 @@ public class Alignment {
|
||||||
|
|
||||||
public void checkColumn() {
|
public void checkColumn() {
|
||||||
if ((this.mode & M_MULTICOLUMN) != 0) {
|
if ((this.mode & M_MULTICOLUMN) != 0) {
|
||||||
int currentIndentation = this.scribe.getNextIndentationLevel(this.scribe.column+(this.scribe.needSpace ? 1 : 0));
|
int currentIndentation = this.scribe.getNextIndentationLevel(this.scribe.column + (this.scribe.needSpace ? 1 : 0));
|
||||||
int fragmentIndentation = this.fragmentIndentations[this.fragmentIndex];
|
int fragmentIndentation = this.fragmentIndentations[this.fragmentIndex];
|
||||||
if (currentIndentation > fragmentIndentation) {
|
if (currentIndentation > fragmentIndentation) {
|
||||||
this.fragmentIndentations[this.fragmentIndex] = currentIndentation;
|
this.fragmentIndentations[this.fragmentIndex] = currentIndentation;
|
||||||
|
@ -227,7 +238,7 @@ public class Alignment {
|
||||||
this.needRedoColumnAlignment = true;
|
this.needRedoColumnAlignment = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// backtrack only once all fragments got checked
|
// Backtrack only once all fragments got checked
|
||||||
if (this.needRedoColumnAlignment && this.fragmentIndex == this.fragmentCount - 1) { // alignment too small
|
if (this.needRedoColumnAlignment && this.fragmentIndex == this.fragmentCount - 1) { // alignment too small
|
||||||
// if (CodeFormatterVisitor.DEBUG) {
|
// if (CodeFormatterVisitor.DEBUG) {
|
||||||
// System.out.println("ALIGNMENT TOO SMALL");
|
// System.out.println("ALIGNMENT TOO SMALL");
|
||||||
|
@ -338,7 +349,7 @@ public class Alignment {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return false; // cannot split better
|
return false; // Cannot split better
|
||||||
}
|
}
|
||||||
|
|
||||||
public Alignment getAlignment(String targetName) {
|
public Alignment getAlignment(String targetName) {
|
||||||
|
@ -362,13 +373,21 @@ public class Alignment {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((this.mode & M_INDENT_ON_COLUMN) != 0 && this.fragmentIndex > 0 &&
|
||||||
|
this.scribe.line > currentFragmentStartLine) {
|
||||||
|
// The previous fragment spans multiple line. Put the current fragment on a new line.
|
||||||
|
this.fragmentBreaks[this.fragmentIndex] = BREAK;
|
||||||
|
this.fragmentIndentations[this.fragmentIndex] = this.breakIndentationLevel;
|
||||||
|
wasSplit = true;
|
||||||
|
}
|
||||||
if (this.fragmentBreaks[this.fragmentIndex] == BREAK) {
|
if (this.fragmentBreaks[this.fragmentIndex] == BREAK) {
|
||||||
this.scribe.startNewLine();
|
this.scribe.startNewLine();
|
||||||
}
|
}
|
||||||
if (this.fragmentIndentations[this.fragmentIndex] > 0) {
|
if (this.fragmentIndentations[this.fragmentIndex] > 0) {
|
||||||
this.scribe.indentationLevel = this.fragmentIndentations[this.fragmentIndex];
|
this.scribe.indentationLevel = this.fragmentIndentations[this.fragmentIndex];
|
||||||
}
|
}
|
||||||
|
currentFragmentStartLine = this.scribe.line;
|
||||||
}
|
}
|
||||||
|
|
||||||
// test whether this is an 'indent-on-column' type alignment and aligns on the given column
|
// test whether this is an 'indent-on-column' type alignment and aligns on the given column
|
||||||
|
|
|
@ -1138,7 +1138,8 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//
|
//
|
||||||
//void test() {
|
//void test() {
|
||||||
// function_with_a_long_name(function(1000000, 2000000, 3000000, 4000000,
|
// function_with_a_long_name(function(1000000, 2000000, 3000000, 4000000,
|
||||||
// 5000000), 6000000);
|
// 5000000),
|
||||||
|
// 6000000);
|
||||||
//}
|
//}
|
||||||
public void testFunctionCall_2() throws Exception {
|
public void testFunctionCall_2() throws Exception {
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
|
@ -1158,8 +1159,8 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//int function_with_a_looooooooooooooooooooooooooooooooong_name(int);
|
//int function_with_a_looooooooooooooooooooooooooooooooong_name(int);
|
||||||
//
|
//
|
||||||
//void test() {
|
//void test() {
|
||||||
// function(function_with_a_looooooooooooooooooooooooooooooooong_name(
|
// function(
|
||||||
// 1000000));
|
// function_with_a_looooooooooooooooooooooooooooooooong_name(1000000));
|
||||||
//}
|
//}
|
||||||
public void testFunctionCall_3() throws Exception {
|
public void testFunctionCall_3() throws Exception {
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
|
@ -2271,6 +2272,7 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
|
|
||||||
//class Stream {
|
//class Stream {
|
||||||
//Stream& operator<<(const char* s);
|
//Stream& operator<<(const char* s);
|
||||||
|
//Stream& operator<<(int i);
|
||||||
//};
|
//};
|
||||||
//
|
//
|
||||||
//Stream stream;
|
//Stream stream;
|
||||||
|
@ -2283,6 +2285,7 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
|
|
||||||
//class Stream {
|
//class Stream {
|
||||||
// Stream& operator<<(const char* s);
|
// Stream& operator<<(const char* s);
|
||||||
|
// Stream& operator<<(int i);
|
||||||
//};
|
//};
|
||||||
//
|
//
|
||||||
//Stream stream;
|
//Stream stream;
|
||||||
|
@ -2290,11 +2293,10 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//
|
//
|
||||||
//void test() {
|
//void test() {
|
||||||
// stream << (variable_with_a_long_name + another_variable_with_a_long_name)
|
// stream << (variable_with_a_long_name + another_variable_with_a_long_name)
|
||||||
// * variable_with_a_long_name
|
// * variable_with_a_long_name
|
||||||
// << "01234567890123456789";
|
// << "01234567890123456789";
|
||||||
//}
|
//}
|
||||||
// TODO(sprigogin): Enable the test when the formatter is fixed.
|
public void testOverloadedLeftShiftChain_4() throws Exception {
|
||||||
public void _testOverloadedLeftShiftChain_4() throws Exception {
|
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_OVERLOADED_LEFT_SHIFT_CHAIN,
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_OVERLOADED_LEFT_SHIFT_CHAIN,
|
||||||
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
||||||
|
|
Loading…
Add table
Reference in a new issue