diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/AutomatedSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/AutomatedSuite.java index 8d06e3790dc..cbab27a38a7 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/AutomatedSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/AutomatedSuite.java @@ -7,12 +7,14 @@ * * Contributors: * IBM - Initial API and implementation + * Anton Leherbauer (Wind River Systems) - Fixed bug 48339 *******************************************************************************/ package org.eclipse.cdt.ui.tests; import junit.framework.Test; import junit.framework.TestSuite; +import org.eclipse.cdt.ui.tests.text.CAutoIndentTest; import org.eclipse.cdt.ui.tests.text.NumberRuleTest; import org.eclipse.cdt.ui.tests.text.contentassist.CompletionFailedTest_MemberReference_Arrow_Prefix2; import org.eclipse.cdt.ui.tests.text.contentassist.CompletionTest_ArgumentType_NoPrefix; @@ -81,6 +83,7 @@ public class AutomatedSuite extends TestSuite { // Success Tests //addTest(PartitionTokenScannerTest.suite()); addTest(NumberRuleTest.suite()); + addTest(CAutoIndentTest.suite()); // completion tests addTest(CompletionTest_FieldType_Prefix.suite()); addTest(CompletionTest_FieldType_NoPrefix.suite()); diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java new file mode 100644 index 00000000000..1768684c63f --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java @@ -0,0 +1,251 @@ +/******************************************************************************* + * Copyright (c) 2006 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.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.cdt.internal.ui.text.CAutoIndentStrategy; +import org.eclipse.cdt.internal.ui.text.CCommentAutoIndentStrategy; +import org.eclipse.cdt.internal.ui.text.CTextTools; +import org.eclipse.cdt.internal.ui.text.ICPartitions; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.DocumentCommand; +import org.eclipse.jface.text.IAutoEditStrategy; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.TextUtilities; + +/** + * Testing the auto indent strategies. + */ +public class CAutoIndentTest extends TestCase { + + /** + * Helper class to test the auto-edit strategies on a document. + */ + static class AutoEditTester { + + private Map fStrategyMap = new HashMap(); + private IDocument fDoc; + private String fPartitioning; + private int fCaretOffset; + + public AutoEditTester(IDocument doc, String partitioning) { + super(); + fDoc = doc; + fPartitioning = partitioning; + } + + public void setAutoEditStrategy(String contentType, IAutoEditStrategy aes) { + fStrategyMap.put(contentType, aes); + } + + public IAutoEditStrategy getAutoEditStrategy(String contentType) { + return (IAutoEditStrategy)fStrategyMap.get(contentType); + } + + public void type(String text) throws BadLocationException { + for (int i = 0; i < text.length(); ++i) { + type(text.charAt(i)); + } + } + + public void type(char c) throws BadLocationException { + TestDocumentCommand command = new TestDocumentCommand(fCaretOffset, 0, new String(new char[] { c })); + customizeDocumentCommand(command); + fCaretOffset += command.exec(fDoc); + } + + private void customizeDocumentCommand(TestDocumentCommand command) throws BadLocationException { + IAutoEditStrategy aes = getAutoEditStrategy(getContentType()); + if (aes != null) { + aes.customizeDocumentCommand(fDoc, command); + } + } + + public void type(int offset, String text) throws BadLocationException { + fCaretOffset = offset; + type(text); + } + + public void type(int offset, char c) throws BadLocationException { + fCaretOffset = offset; + type(c); + } + + public void paste(String text) throws BadLocationException { + TestDocumentCommand command = new TestDocumentCommand(fCaretOffset, 0, text); + customizeDocumentCommand(command); + fCaretOffset += command.exec(fDoc); + } + + public void paste(int offset, String text) throws BadLocationException { + fCaretOffset = offset; + paste(text); + } + + public void backspace(int n) throws BadLocationException { + for (int i=0; i= 2 && c.offset == d.getLength()) { + try { + String partitioning = CUIPlugin.getDefault().getTextTools().getDocumentPartitioning(); + String contentType = org.eclipse.jface.text.TextUtilities.getContentType(d, partitioning, c.offset - 1, false); + if (ICPartitions.C_MULTILINE_COMMENT.equals(contentType)) { + return !d.get(c.offset - 2, 2).equals(MULTILINE_COMMENT_CLOSE); + } + } catch (BadLocationException exc) { + // see below + } + } + return false; + } + } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CCommentAutoIndentStrategy.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CCommentAutoIndentStrategy.java index 2b99f2f89bf..ef5a2b4166a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CCommentAutoIndentStrategy.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CCommentAutoIndentStrategy.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000 2005 IBM Corporation and others. + * Copyright (c) 2000, 2006 IBM Corporation 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 @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * QNX Software System + * Anton Leherbauer (Wind River Systems) - Fixed bug 48339 *******************************************************************************/ package org.eclipse.cdt.internal.ui.text; @@ -18,7 +19,7 @@ import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; /** - * Auto indent strategy for java doc comments + * Auto indent strategy for C/C++ multiline comments */ public class CCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { @@ -40,25 +41,24 @@ public class CCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrateg } /** - * Copies the indentation of the previous line and add a star - * If the javadoc just started on tis line add also a blank + * Copies the indentation of the previous line and adds a star. + * If the comment just started on this line adds also a blank. * * @param d the document to work on * @param c the command to deal with */ - private void jdocIndentAfterNewLine(IDocument d, DocumentCommand c) { + static void commentIndentAfterNewLine(IDocument d, DocumentCommand c) { if (c.offset == -1 || d.getLength() == 0) return; try { // find start of line - int p= (c.offset == d.getLength() ? c.offset - 1 : c.offset); - IRegion info= d.getLineInformationOfOffset(p); + IRegion info= d.getLineInformationOfOffset(c.offset); int start= info.getOffset(); // find white spaces - int end= findEndOfWhiteSpace(d, start, c.offset); + int end= findEndOfWhiteSpaceAt(d, start, c.offset); StringBuffer buf= new StringBuffer(c.text); if (end >= start) { // 1GEYL1R: ITPJUI:ALL - java doc edit smartness not work for class comments @@ -66,7 +66,7 @@ public class CCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrateg buf.append(d.get(start, end - start)); if (end < c.offset) { if (d.getChar(end) == '/') { - // javadoc started on this line + // comment started on this line buf.append(" * "); //$NON-NLS-1$ } else if (d.getChar(end) == '*') { buf.append("* "); //$NON-NLS-1$ @@ -82,7 +82,18 @@ public class CCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrateg } } - protected void jdocIndentForCommentEnd(IDocument d, DocumentCommand c) { + static int findEndOfWhiteSpaceAt(IDocument document, int offset, int end) throws BadLocationException { + while (offset < end) { + char c= document.getChar(offset); + if (c != ' ' && c != '\t') { + return offset; + } + offset++; + } + return end; + } + + static void commentIndentForCommentEnd(IDocument d, DocumentCommand c) { if (c.offset < 2 || d.getLength() == 0) { return; } @@ -102,9 +113,9 @@ public class CCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrateg */ public void customizeDocumentCommand(IDocument d, DocumentCommand c) { if (c.length == 0 && c.text != null && endsWithDelimiter(d, c.text)) - jdocIndentAfterNewLine(d, c); + commentIndentAfterNewLine(d, c); else if ("/".equals(c.text)) { //$NON-NLS-1$ - jdocIndentForCommentEnd(d, c); + commentIndentForCommentEnd(d, c); } } }