Skip to main content
summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorSergey Prigogin2009-07-12 00:17:30 -0400
committerSergey Prigogin2009-07-12 00:17:30 -0400
commited26ae3fac6f271213cbaa1ceba22942fb7dcd0e (patch)
treef9f3fc20ed3cdd0ab5a83eaf9a19b08ce718f7cc /core
parentf604cd2e2002dfd07e700a9bdf83f2baf5447075 (diff)
downloadorg.eclipse.cdt-ed26ae3fac6f271213cbaa1ceba22942fb7dcd0e.tar.gz
org.eclipse.cdt-ed26ae3fac6f271213cbaa1ceba22942fb7dcd0e.tar.xz
org.eclipse.cdt-ed26ae3fac6f271213cbaa1ceba22942fb7dcd0e.zip
Auto indenter improvements. Bug 283230.
Diffstat (limited to 'core')
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java131
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/IndentActionTest.java1
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java2
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java80
4 files changed, 195 insertions, 19 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java
index 7556c99e9e..6bd26e6b1b 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java
@@ -67,10 +67,25 @@ public class CIndenterTest extends BaseUITestCase {
IndentUtil.indentLines(document, new LineRange(0, numLines), null, null);
assertEquals(expected, document.get());
}
-
+
+ //int array[] =
+ //{
+ // sizeof(x)
+ // , 1
+ //};
+
+ //int array[] =
+ //{
+ // sizeof(x)
+ // , 1
+ //};
+ public void testArrayInitializer() throws Exception {
+ assertIndenterResult();
+ }
+
//foo(arg,
//"string");
-
+
//foo(arg,
// "string");
public void testIndentationOfStringLiteralAsLastArgument1_Bug192412() throws Exception {
@@ -79,7 +94,7 @@ public class CIndenterTest extends BaseUITestCase {
//a::foo(arg,
//"string");
-
+
//a::foo(arg,
// "string");
public void testIndentationOfStringLiteralAsLastArgument2_Bug192412() throws Exception {
@@ -88,7 +103,7 @@ public class CIndenterTest extends BaseUITestCase {
//a::foo(arg,
// "string");
-
+
//a::foo(arg,
// "string");
public void testIndentationOfStringLiteralAsLastArgument3_Bug192412() throws Exception {
@@ -137,7 +152,104 @@ public class CIndenterTest extends BaseUITestCase {
// const BinFileParser::Exception& exp)
//{
//}
- public void testIndentationOfOperatorMethodBody_Bug192412() throws Exception {
+ public void testIndentationOfOperatorMethodBody_Bug192412_1() throws Exception {
+ assertIndenterResult();
+ }
+
+ //std::ostream& operator<<(std::ostream& stream,
+ //const BinFileParser::Exception& exp)
+ //{
+ //}
+
+ //std::ostream& operator<<(std::ostream& stream,
+ // const BinFileParser::Exception& exp)
+ //{
+ //}
+ public void testIndentationOfOperatorMethodBody_Bug192412_2() throws Exception {
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
+ DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_COMPACT,
+ DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
+ assertIndenterResult();
+ }
+
+ //void func(std::vector<int>* v,
+ //const std::string& s)
+ //{
+ //}
+
+ //void func(std::vector<int>* v,
+ // const std::string& s)
+ //{
+ //}
+ public void testFunctionParameters_1() throws Exception {
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "2");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, "2");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
+ DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_COMPACT,
+ DefaultCodeFormatterConstants.INDENT_DEFAULT));
+ assertIndenterResult();
+ }
+
+ //void func(std::vector<int>* v,
+ //const std::string& s)
+ //{
+ //}
+
+ //void func(std::vector<int>* v,
+ // const std::string& s)
+ //{
+ //}
+ public void testFunctionParameters_2() throws Exception {
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "2");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, "2");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
+ DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_COMPACT,
+ DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
+ assertIndenterResult();
+ }
+
+ //void func(
+ //std::vector<int>* v,
+ //const std::string& s)
+ //{
+ //}
+
+ //void func(
+ // std::vector<int>* v,
+ // const std::string& s)
+ //{
+ //}
+ public void testFunctionParameters_3() throws Exception {
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "2");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, "2");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
+ DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_COMPACT,
+ DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
+ assertIndenterResult();
+ }
+
+ //void func(
+ //std::vector<int>* v,
+ //const std::string& s)
+ //{
+ //}
+
+ //void func(
+ // std::vector<int>* v,
+ // const std::string& s)
+ //{
+ //}
+ public void testFunctionParameters_4() throws Exception {
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "2");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, "2");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
+ DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE,
+ DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
assertIndenterResult();
}
@@ -482,6 +594,15 @@ public class CIndenterTest extends BaseUITestCase {
assertIndenterResult();
}
+ //cout << "long text"
+ //<< " more text";
+
+ //cout << "long text"
+ // << " more text";
+ public void testWrappedOutputStream() throws Exception {
+ assertIndenterResult();
+ }
+
///* comment */
//#define MACRO(a, b) \
//value
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/IndentActionTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/IndentActionTest.java
index 20373e654a..15025ea361 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/IndentActionTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/IndentActionTest.java
@@ -122,5 +122,4 @@ public class IndentActionTest extends TestCase {
selectAll();
assertIndentResult();
}
-
}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java
index 0b0578f2cd..54bcfc7b09 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java
@@ -460,7 +460,7 @@ public final class IndentUtil {
if (computed != null)
indent= computed.toString();
else
- indent= new String();
+ indent= ""; //$NON-NLS-1$
}
// change document:
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java
index 4ef796732d..800241c8e3 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java
@@ -54,8 +54,10 @@ public final class CIndenter {
final int prefSimpleIndent;
final int prefBracketIndent;
final boolean prefMethodDeclDeepIndent;
+ final boolean prefMethodDeclFirstParameterDeepIndent;
final int prefMethodDeclIndent;
final boolean prefMethodCallDeepIndent;
+ final boolean prefMethodCallFirstParameterDeepIndent;
final int prefMethodCallIndent;
final boolean prefParenthesisDeepIndent;
final int prefParenthesisIndent;
@@ -106,8 +108,10 @@ public final class CIndenter {
prefSimpleIndent= prefSimpleIndent();
prefBracketIndent= prefBracketIndent();
prefMethodDeclDeepIndent= prefMethodDeclDeepIndent();
+ prefMethodDeclFirstParameterDeepIndent= prefMethodDeclFirstParameterDeepIndent();
prefMethodDeclIndent= prefMethodDeclIndent();
prefMethodCallDeepIndent= prefMethodCallDeepIndent();
+ prefMethodCallFirstParameterDeepIndent= prefMethodCallFirstParameterDeepIndent();
prefMethodCallIndent= prefMethodCallIndent();
prefParenthesisDeepIndent= prefParenthesisDeepIndent();
prefParenthesisIndent= prefParenthesisIndent();
@@ -218,7 +222,23 @@ public final class CIndenter {
private boolean prefMethodDeclDeepIndent() {
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION);
try {
- return DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_ON_COLUMN;
+ int indentStyle = DefaultCodeFormatterConstants.getIndentStyle(option);
+ return indentStyle == DefaultCodeFormatterConstants.INDENT_ON_COLUMN;
+ } catch (IllegalArgumentException e) {
+ // ignore and return default
+ }
+
+ return false;
+ }
+
+ private boolean prefMethodDeclFirstParameterDeepIndent() {
+ String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION);
+ try {
+ int indentStyle = DefaultCodeFormatterConstants.getIndentStyle(option);
+ int wrappingStyle = DefaultCodeFormatterConstants.getWrappingStyle(option);
+ return indentStyle == DefaultCodeFormatterConstants.INDENT_ON_COLUMN &&
+ (wrappingStyle == DefaultCodeFormatterConstants.WRAP_COMPACT_FIRST_BREAK ||
+ wrappingStyle == DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE);
} catch (IllegalArgumentException e) {
// ignore and return default
}
@@ -242,7 +262,22 @@ public final class CIndenter {
private boolean prefMethodCallDeepIndent() {
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION);
try {
- return DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_ON_COLUMN;
+ int indentStyle = DefaultCodeFormatterConstants.getIndentStyle(option);
+ return indentStyle == DefaultCodeFormatterConstants.INDENT_ON_COLUMN;
+ } catch (IllegalArgumentException e) {
+ // ignore and return default
+ }
+ return false; // sensible default
+ }
+
+ private boolean prefMethodCallFirstParameterDeepIndent() {
+ String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION);
+ try {
+ int indentStyle = DefaultCodeFormatterConstants.getIndentStyle(option);
+ int wrappingStyle = DefaultCodeFormatterConstants.getWrappingStyle(option);
+ return indentStyle == DefaultCodeFormatterConstants.INDENT_ON_COLUMN &&
+ (wrappingStyle == DefaultCodeFormatterConstants.WRAP_COMPACT_FIRST_BREAK ||
+ wrappingStyle == DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE);
} catch (IllegalArgumentException e) {
// ignore and return default
}
@@ -861,7 +896,7 @@ public final class CIndenter {
* should be indented, or {@link CHeuristicScanner#NOT_FOUND}
*/
public int findReferencePosition(int offset, boolean danglingElse, boolean matchBrace, boolean matchParen,
- boolean matchCase, boolean matchAccessSpecifier) {
+ boolean matchCase, boolean matchAccessSpecifier) {
fIndent= 0; // the indentation modification
fAlign= CHeuristicScanner.NOT_FOUND;
fPosition= offset;
@@ -946,7 +981,7 @@ public final class CIndenter {
case Symbols.TokenLPAREN:
case Symbols.TokenLBRACE:
case Symbols.TokenLBRACKET:
- return handleScopeIntroduction(Math.min(offset + 1, fDocument.getLength()));
+ return handleScopeIntroduction(Math.min(offset + 1, fDocument.getLength()), true);
case Symbols.TokenEOF:
// trap when hitting start of document
@@ -1463,7 +1498,7 @@ public final class CIndenter {
* the reference position returned is determined depending on the type of list:
* The indentation will either match the list scope introducer (e.g. for
* method declarations), so called deep indents, or simply increase the
- * indentation by a number of standard indents. See also {@link #handleScopeIntroduction(int)}.
+ * indentation by a number of standard indents. See also {@link #handleScopeIntroduction(int, boolean)}.
* @return the reference position for a list item: either a previous list item
* that has its own indentation, or the list introduction start.
*/
@@ -1471,6 +1506,8 @@ public final class CIndenter {
int startLine= fLine;
int startPosition= fPosition;
boolean seenEqual = fToken == Symbols.TokenEQUAL;
+ boolean seenShiftLeft = fToken == Symbols.TokenSHIFTLEFT;
+ boolean seenRightParen = fToken == Symbols.TokenRPAREN;
while (true) {
nextToken();
@@ -1480,7 +1517,8 @@ public final class CIndenter {
int lineOffset= fDocument.getLineOffset(startLine);
int bound= Math.min(fDocument.getLength(), startPosition + 1);
if ((fToken == Symbols.TokenSEMICOLON || fToken == Symbols.TokenRBRACE ||
- fToken == Symbols.TokenLBRACE) && seenEqual) {
+ fToken == Symbols.TokenLBRACE && !looksLikeArrayInitializerIntro()) &&
+ (seenEqual || seenShiftLeft || seenRightParen)) {
fIndent = fPrefs.prefContinuationIndent;
} else {
fAlign= fScanner.findNonWhitespaceForwardInAnyPartition(lineOffset, bound);
@@ -1494,6 +1532,8 @@ public final class CIndenter {
switch (fToken) {
// scopes: skip them
case Symbols.TokenRPAREN:
+ seenRightParen = true;
+ //$FALL-THROUGH$
case Symbols.TokenRBRACKET:
case Symbols.TokenRBRACE:
skipScope();
@@ -1503,7 +1543,7 @@ public final class CIndenter {
case Symbols.TokenLPAREN:
case Symbols.TokenLBRACE:
case Symbols.TokenLBRACKET:
- return handleScopeIntroduction(startPosition + 1);
+ return handleScopeIntroduction(startPosition + 1, false);
case Symbols.TokenSEMICOLON:
return fPosition;
@@ -1520,8 +1560,12 @@ public final class CIndenter {
seenEqual = true;
break;
+ case Symbols.TokenSHIFTLEFT:
+ seenShiftLeft = true;
+ break;
+
case Symbols.TokenEOF:
- if (seenEqual) {
+ if (seenEqual || seenShiftLeft || seenRightParen) {
fIndent = fPrefs.prefContinuationIndent;
}
return 0;
@@ -1599,9 +1643,11 @@ public final class CIndenter {
*
* @param bound the bound for the search for the first token after the scope
* introduction.
+ * @param firstToken <code>true</code> if we are dealing with the first token after
+ * the opening parenthesis.
* @return the indent
*/
- private int handleScopeIntroduction(int bound) {
+ private int handleScopeIntroduction(int bound, boolean firstToken) {
int pos= fPosition; // store
switch (fToken) {
@@ -1609,7 +1655,7 @@ public final class CIndenter {
case Symbols.TokenLPAREN:
// special: method declaration deep indentation
if (looksLikeMethodDecl()) {
- if (fPrefs.prefMethodDeclDeepIndent) {
+ if (firstToken ? fPrefs.prefMethodDeclFirstParameterDeepIndent : fPrefs.prefMethodDeclDeepIndent) {
return setFirstElementAlignment(pos, bound);
} else {
fIndent= fPrefs.prefMethodDeclIndent;
@@ -1618,7 +1664,7 @@ public final class CIndenter {
} else {
fPosition= pos;
if (looksLikeMethodCall()) {
- if (fPrefs.prefMethodCallDeepIndent) {
+ if (firstToken ? fPrefs.prefMethodCallFirstParameterDeepIndent : fPrefs.prefMethodCallDeepIndent) {
return setFirstElementAlignment(pos, bound);
} else {
fIndent= fPrefs.prefMethodCallIndent;
@@ -1691,8 +1737,18 @@ public final class CIndenter {
private int setFirstElementAlignment(int scopeIntroducerOffset, int bound) {
int firstPossible= scopeIntroducerOffset + 1; // align with the first position after the scope intro
fAlign= fScanner.findNonWhitespaceForwardInAnyPartition(firstPossible, bound);
- if (fAlign == CHeuristicScanner.NOT_FOUND)
+ if (fAlign == CHeuristicScanner.NOT_FOUND) {
fAlign= firstPossible;
+ } else {
+ try {
+ IRegion lineRegion = fDocument.getLineInformationOfOffset(scopeIntroducerOffset);
+ if (fAlign > lineRegion.getOffset() + lineRegion.getLength()) {
+ fAlign= firstPossible;
+ }
+ } catch (BadLocationException e) {
+ // Ignore.
+ }
+ }
return fAlign;
}

Back to the top