diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java index 0a5a54574f8..ae5733c56d4 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java @@ -2221,6 +2221,16 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, // operand 1 op1.accept(this); + // operator + final int nextToken= peekNextToken(); + // in case of C++ alternative operators, like 'and', 'not', etc. a space + boolean forceSpace= Character.isJavaIdentifierStart(peekNextChar()); + + scribe.printNextToken(nextToken, forceSpace || preferences.insert_space_before_assignment_operator); + if (forceSpace || preferences.insert_space_after_assignment_operator) { + scribe.space(); + } + Alignment expressionAlignment= scribe.createAlignment( "assignmentExpression", //$NON-NLS-1$ preferences.alignment_for_assignment, @@ -2233,16 +2243,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, try { scribe.alignFragment(expressionAlignment, 0); - // operator - final int nextToken= peekNextToken(); - // in case of C++ alternative operators, like 'and', 'not', etc. a space - boolean forceSpace= Character.isJavaIdentifierStart(peekNextChar()); - - scribe.printNextToken(nextToken, forceSpace || preferences.insert_space_before_assignment_operator); - if (forceSpace || preferences.insert_space_after_assignment_operator) { - scribe.space(); - } - // operand 2 final IASTExpression op2= node.getOperand2(); op2.accept(this); diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index c81bc61ed5a..b53f0b33284 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -8,6 +8,7 @@ * Contributors: * Anton Leherbauer (Wind River Systems) - initial API and implementation * Andrew Ferguson (Symbian) + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.ui.tests.text; @@ -38,7 +39,6 @@ import org.eclipse.cdt.internal.formatter.align.Alignment; * @since 4.0 */ public class CodeFormatterTest extends BaseUITestCase { - private Map fOptions; private Map fDefaultOptions; @@ -72,10 +72,10 @@ public class CodeFormatterTest extends BaseUITestCase { edit.apply(document); assertEquals(expected, document.get()); } - + //void foo(int arg); //void foo(int arg){} - + //void foo (int arg); //void foo (int arg) { //} @@ -132,12 +132,12 @@ public class CodeFormatterTest extends BaseUITestCase { public void testIndentConfusionByCastExpression_Bug191021() throws Exception { assertFormatterResult(); } - + //int //var; //int* //pvar; - + //int var; //int* pvar; public void testSpaceBetweenTypeAndIdentifier_Bug194603() throws Exception { @@ -145,7 +145,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //int a = sizeof( int) ; - + //int a = sizeof(int); public void testSizeofExpression_Bug195246() throws Exception { assertFormatterResult(); @@ -153,7 +153,7 @@ public class CodeFormatterTest extends BaseUITestCase { //int x; //int a = sizeof x ; - + //int x; //int a = sizeof x; public void testSizeofExpression_Bug201330() throws Exception { @@ -257,16 +257,16 @@ public class CodeFormatterTest extends BaseUITestCase { } //int verylooooooooooooooooooooooooooooooooooongname = 0000000000000000000000000000000; - + //int verylooooooooooooooooooooooooooooooooooongname = // 0000000000000000000000000000000; public void testLineWrappingOfInitializerExpression_Bug200961() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSIGNMENT, Integer.toString(Alignment.M_COMPACT_SPLIT)); assertFormatterResult(); } - + //void functionWithLooooooooooooooooooooooooooooooooooooooooooooooooongName() throw(float); - + //void functionWithLooooooooooooooooooooooooooooooooooooooooooooooooongName() // throw (float); public void testLineWrappingOfThrowSpecification_Bug200959() throws Exception { @@ -277,7 +277,7 @@ public class CodeFormatterTest extends BaseUITestCase { //public: //A(); //}; - + //class A // { //public: @@ -294,7 +294,7 @@ public class CodeFormatterTest extends BaseUITestCase { //public: //A(); //}; - + //class A // { // public: @@ -311,7 +311,7 @@ public class CodeFormatterTest extends BaseUITestCase { //public: //A(); //}; - + //class A // { // public: @@ -373,7 +373,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //int main(int argc, char const int * argv[]) { try { for (int i = 1; i < argc; ++i) { } return 0; } catch (float e) { return 1; } catch (...) { return 2; } } - + //int main(int argc, char const int * argv[]) { // try { // for (int i = 1; i < argc; ++i) { @@ -401,7 +401,7 @@ public class CodeFormatterTest extends BaseUITestCase { public void testMacroAsInitializer_Bug214354() throws Exception { assertFormatterResult(); } - + //#define break_start(); { int foo; //#define break_end(); foo = 0; } // @@ -419,7 +419,7 @@ public class CodeFormatterTest extends BaseUITestCase { // if(b == a) // indentation_remains(); //} - + //#define break_start(); { int foo; //#define break_end(); foo = 0; } // @@ -440,18 +440,18 @@ public class CodeFormatterTest extends BaseUITestCase { public void testBracesInMacros_Bug217435() throws Exception { assertFormatterResult(); } - + //int a=1+2; //int b= - a; //int c =b ++/-- b; - + //int a = 1 + 2; //int b = -a; //int c = b++ / --b; public void testWhitespaceSurroundingOperators() throws Exception { assertFormatterResult(); } - + //void f() { //int *px= :: new int( 0 ); //int* py [] = new int [5 ] (0, 1,2,3, 4); @@ -542,7 +542,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //class Example: public FooClass, public virtual BarClass {}; - + //class Example: // public FooClass, // public virtual BarClass { @@ -554,7 +554,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //class Example: public FooClass, public virtual BarClass {}; - + //class Example: public FooClass, // public virtual BarClass { //}; @@ -580,12 +580,40 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //class ClassWithALongName { + //public: + //ClassWithALongName* methodWithAQuiteLongName(); + //}; + // + //void test() { + //ClassWithALongName* variable_with_a_long_name = variable_with_a_long_name->methodWithAQuiteLongName(); + //variable_with_a_long_name = variable_with_a_long_name->methodWithAQuiteLongName(); + //} + + //class ClassWithALongName { + //public: + // ClassWithALongName* methodWithAQuiteLongName(); + //}; + // + //void test() { + // ClassWithALongName* variable_with_a_long_name = + // variable_with_a_long_name->methodWithAQuiteLongName(); + // variable_with_a_long_name = + // variable_with_a_long_name->methodWithAQuiteLongName(); + //} + public void testAssignment() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSIGNMENT, + Integer.toString(Alignment.M_COMPACT_SPLIT)); + assertFormatterResult(); + } + //int foo(){try{}catch(...){}} //float* bar(); //templateclass basic_ios : public ios_base{public: // // Types: //}; - + //int //foo() //{ @@ -648,7 +676,7 @@ public class CodeFormatterTest extends BaseUITestCase { fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap()); assertFormatterResult(); } - + //enum Tthe3rdtestIds //{ //ECommand1 = 0x6001, @@ -683,7 +711,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //void f() { throw 42; } - + //void f() { // throw 42; //} @@ -697,7 +725,7 @@ public class CodeFormatterTest extends BaseUITestCase { // // Comment // for(;;); //} - + //struct { // int l; //} s; @@ -717,7 +745,7 @@ public class CodeFormatterTest extends BaseUITestCase { // // Comment // for(;;); //} - + //struct { // int e; //} s; @@ -737,7 +765,7 @@ public class CodeFormatterTest extends BaseUITestCase { // // Comment // for(;;); //} - + //struct { // int f; //} s; @@ -752,7 +780,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //int a = 0, b = 1, c = 2, d = 3; - + //int a = 0,b = 1,c = 2,d = 3; public void testSpaceAfterCommaInDeclaratorList_Bug234915() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_DECLARATOR_LIST, CCorePlugin.DO_NOT_INSERT); @@ -760,7 +788,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //int a = 0,b = 1,c = 2,d = 3; - + //int a = 0, b = 1, c = 2, d = 3; public void testSpaceAfterCommaInDeclaratorList2_Bug234915() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_DECLARATOR_LIST, CCorePlugin.INSERT); @@ -840,7 +868,7 @@ public class CodeFormatterTest extends BaseUITestCase { public void testNestedTemplatedArgument_Bug241058() throws Exception { assertFormatterResult(); } - + //#define TP_SMALLINT int32_t //void foo(const TP_SMALLINT &intVal) { } //void bar(const TP_SMALLINT intVal) { } @@ -853,7 +881,7 @@ public class CodeFormatterTest extends BaseUITestCase { public void testPreserveSpaceInParameterDecl_Bug241967() throws Exception { assertFormatterResult(); } - + //#define MY_MACRO int a; \ // int b; \ // int c(); @@ -865,7 +893,7 @@ public class CodeFormatterTest extends BaseUITestCase { // asdf(); //~asdf(); //}; - + //#define MY_MACRO int a; \ // int b; \ // int c(); @@ -884,7 +912,7 @@ public class CodeFormatterTest extends BaseUITestCase { //void foo() { //for(int i=0;i<50;++i){} //} - + //void foo() { // for (int i = 0 ; i < 50 ; ++i) { // } @@ -895,7 +923,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //char *b, * const a; - + //char *b, * const a; public void testPreserveSpaceBetweenPointerModifierAndIdentifier_Bug243056() throws Exception { assertFormatterResult(); @@ -1139,7 +1167,7 @@ public class CodeFormatterTest extends BaseUITestCase { public void testDotStarAndArrowStarOperators_Bug257700() throws Exception { assertFormatterResult(); } - + //void zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz1(unsigned char __attribute__((unused)) x, unsigned char __attribute__((unused)) y){;} //void zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz1( @@ -1150,7 +1178,7 @@ public class CodeFormatterTest extends BaseUITestCase { public void test__attribute__InParameterDecl_Bug206271() throws Exception { assertFormatterResult(); } - + //#define assert(e) if(!(e)) printf("Failed assertion") //void test(){assert(1 > 0);} @@ -1184,7 +1212,7 @@ public class CodeFormatterTest extends BaseUITestCase { public void testPreserveSpaceBetweenNameAnd__attribute__Bug261967() throws Exception { assertFormatterResult(); } - + //extern "C" void f(int i, char c, float x); //extern "C" void f(int i, char c, float x); @@ -1215,12 +1243,12 @@ public class CodeFormatterTest extends BaseUITestCase { public void testFormatterProblemsWithTypename_Bug269590() throws Exception { assertFormatterResult(); } - + //void //foo(); //int* //bar(); - + //void //foo(); //int* @@ -1356,7 +1384,7 @@ public class CodeFormatterTest extends BaseUITestCase { //#define new new(__FILE__, __LINE__) //void func() {char* a = new char[10];} - + //#define new new(__FILE__, __LINE__) //void func() { // char* a = new char[10]; @@ -1367,7 +1395,7 @@ public class CodeFormatterTest extends BaseUITestCase { //#define MACRO(a) class b : public a //MACRO(aClass){ int a;}; - + //#define MACRO(a) class b : public a //MACRO(aClass) { // int a; @@ -1375,12 +1403,12 @@ public class CodeFormatterTest extends BaseUITestCase { public void testCompositeTypeSpecAsMacro_Bug298592() throws Exception { assertFormatterResult(); } - + //void f() { //w_char* p = L"wide string literal"; //int x = 0; //if (x == 0) x = 5;} - + //void f() { // w_char* p = L"wide string literal"; // int x = 0; @@ -1393,13 +1421,13 @@ public class CodeFormatterTest extends BaseUITestCase { //#define INT (int) //int i = INT 1; - + //#define INT (int) //int i = INT 1; public void testCastAsMacro_Bug285901() throws Exception { assertFormatterResult(); } - + //PARENT_T sample={.a=1,.b={a[2]=1,.b.c=2}}; //PARENT_T sample = { .a = 1, .b = { a[2] = 1, .b.c = 2 } }; @@ -1409,7 +1437,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //void extend_terminal_bond_to_label(vector &atom, const vector &letters, int n_letters, const vector &bond, int n_bond, const vector &label, int n_label, double avg, double maxh, double max_dist_double_bond); - + //void extend_terminal_bond_to_label(vector &atom, const vector &letters, int n_letters, // const vector &bond, int n_bond, const vector &label, int n_label, // double avg, double maxh, double max_dist_double_bond); @@ -1441,7 +1469,7 @@ public class CodeFormatterTest extends BaseUITestCase { } //void extend_terminal_bond_to_label(vector &atom, const vector &letters, int n_letters, const vector &bond, int n_bond, const vector &label, int n_label, double avg, double maxh, double max_dist_double_bond); - + //void extend_terminal_bond_to_label(vector &atom, // const vector &letters, // int n_letters, const vector &bond, @@ -1473,7 +1501,7 @@ public class CodeFormatterTest extends BaseUITestCase { //{ 1,2, 3,4 }, // {1, 2,3, 4} // }; - + //int table[][] = { // { 1, 2, 3, 4 }, // { 1, 2, 3, 4 }, @@ -1484,7 +1512,7 @@ public class CodeFormatterTest extends BaseUITestCase { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_JOIN_WRAPPED_LINES, DefaultCodeFormatterConstants.FALSE); assertFormatterResult(); } - + //#define X() { } //void g() { // X(); @@ -1506,7 +1534,7 @@ public class CodeFormatterTest extends BaseUITestCase { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_JOIN_WRAPPED_LINES, DefaultCodeFormatterConstants.FALSE); assertFormatterResult(); } - + //void f() { //double confidence = 0.316030 // //- 0.016315 * C_Count // @@ -1527,7 +1555,7 @@ public class CodeFormatterTest extends BaseUITestCase { Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN)); assertFormatterResult(); } - + //#define m() f() //void f() { //if (1) f();