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 new file mode 100644 index 00000000000..5b9090a9802 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Anton Leherbauer (Wind River Systems) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestSuite; + +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.source.LineRange; + +import org.eclipse.cdt.ui.tests.BaseUITestCase; + +import org.eclipse.cdt.internal.formatter.DefaultCodeFormatterOptions; + +import org.eclipse.cdt.internal.ui.editor.CDocumentSetupParticipant; +import org.eclipse.cdt.internal.ui.editor.IndentUtil; +import org.eclipse.cdt.internal.ui.text.CHeuristicScanner; +import org.eclipse.cdt.internal.ui.text.CIndenter; + +/** + * Tests for the CIndenter. + * + * @since 4.0 + */ +public class CIndenterTest extends BaseUITestCase { + + private Map fOptions; + private Map fDefaultOptions; + + public static TestSuite suite() { + return suite(CIndenterTest.class, "_"); + } + + protected void setUp() throws Exception { + super.setUp(); + fDefaultOptions= DefaultCodeFormatterOptions.getDefaultSettings().getMap(); + fOptions= new HashMap(fDefaultOptions); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + protected void assertIndenterResult() throws Exception { + StringBuffer[] contents= getContentsForTest(2); + String before= contents[0].toString(); + IDocument document= new Document(before); + String expected= contents[1].toString(); + new CDocumentSetupParticipant().setup(document); + CIndenter indenter= new CIndenter(document, new CHeuristicScanner(document)); + IndentUtil.indentLines(document, new LineRange(0, document.getNumberOfLines()), null, null); + assertEquals(expected, document.get()); + } + + //foo(arg, + //"string"); + + //foo(arg, + // "string"); + public void testIndentationOfStringAsLastArgument_Bug192412() throws Exception { + assertIndenterResult(); + } + + //if (1) + //foo->bar(); + //dontIndent(); + + //if (1) + // foo->bar(); + //dontIndent(); + public void testIndentationAfterArrowOperator_Bug192412() throws Exception { + assertIndenterResult(); + } +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java index 34e7643b95f..98ccb72157f 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java @@ -33,6 +33,7 @@ public class TextTestSuite extends TestSuite { addTest(IndentActionTest.suite()); addTest(FormatActionTest.suite()); addTest(CodeFormatterTest.suite()); + addTest(CIndenterTest.suite()); // Break iterator tests. addTest(CBreakIteratorTest.suite()); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CHeuristicScanner.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CHeuristicScanner.java index 4ce61d2113b..9ce4ebbaa85 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CHeuristicScanner.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CHeuristicScanner.java @@ -301,6 +301,16 @@ public final class CHeuristicScanner implements Symbols { * @return a constant from {@link Symbols} describing the next token */ public int nextToken(int start, int bound) { + try { + // check for string or char literal + char ch = fDocument.getChar(start); + if (ch == '"' || ch == '\'') { + fChar= ch; + fPos= fNonWSDefaultPart.nextPosition(start, true); + return TokenOTHER; + } + } catch (BadLocationException exc) { + } int pos= scanForward(start, bound, fNonWSDefaultPart); if (pos == NOT_FOUND) return TokenEOF; @@ -331,11 +341,23 @@ public final class CHeuristicScanner implements Symbols { case LANGLE: return TokenLESSTHAN; case RANGLE: - if (peekNextChar() == RANGLE) { + switch (peekNextChar()) { + case RANGLE: ++fPos; return TokenSHIFTRIGHT; } return TokenGREATERTHAN; + case DOT: + return TokenDOT; + case MINUS: + switch (peekNextChar()) { + case RANGLE: + ++fPos; + return TokenARROW; + } + return TokenMINUS; + case TILDE: + return TokenTILDE; } // else @@ -407,9 +429,13 @@ public final class CHeuristicScanner implements Symbols { case LANGLE: return TokenLESSTHAN; case RANGLE: - if (peekPreviousChar() == RANGLE) { + switch (peekPreviousChar()) { + case RANGLE: --fPos; return TokenSHIFTRIGHT; + case MINUS: + --fPos; + return TokenARROW; } return TokenGREATERTHAN; case DOT: @@ -966,11 +992,8 @@ public final class CHeuristicScanner implements Symbols { if (token == Symbols.TokenDOT) { return true; } - if (token == Symbols.TokenGREATERTHAN) { - token= previousToken(getPosition(), bound); - if (token == Symbols.TokenMINUS) { - return true; - } + if (token == Symbols.TokenARROW) { + return true; } else if (token == Symbols.TokenCOLON) { token= previousToken(getPosition(), bound); if (token == Symbols.TokenCOLON) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/Symbols.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/Symbols.java index fed73e3fa18..9bfc034e706 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/Symbols.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/Symbols.java @@ -35,6 +35,7 @@ public interface Symbols { int TokenMINUS= 16; int TokenTILDE= 17; int TokenSHIFTRIGHT= 18; + int TokenARROW= 19; int TokenIF= 109; int TokenDO= 1010; int TokenFOR= 1011;