### Eclipse Workspace Patch 1.0 #P org.eclipse.cdt.ui.tests Index: ui/org/eclipse/cdt/ui/tests/text/AddBlockCommentTest.java =================================================================== RCS file: ui/org/eclipse/cdt/ui/tests/text/AddBlockCommentTest.java diff -N ui/org/eclipse/cdt/ui/tests/text/AddBlockCommentTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ui/org/eclipse/cdt/ui/tests/text/AddBlockCommentTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,303 @@ +/******************************************************************************* + * Copyright (c) 2007, 2008 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 + * Andrew Gvozdev + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text; + +import junit.framework.TestSuite; + +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.testplugin.CProjectHelper; +import org.eclipse.cdt.ui.tests.BaseUITestCase; + +import org.eclipse.cdt.internal.core.model.ext.SourceRange; + +import org.eclipse.cdt.internal.ui.editor.CEditor; + +/** + * Tests for the AddBlockCommentAction. + * + * @since 5.0 + */ +public class AddBlockCommentTest extends BaseUITestCase { + private ICProject fCProject; + public static TestSuite suite() { + return suite(AddBlockCommentTest.class, "_"); + } + + private CEditor fEditor; + private IDocument fDocument; + + /* + * The class describes a position on a line counting in ordinary people way, + * starting from 1. + */ + static class LinePosition { + private int line; + private int position; + private IDocument fDoc; + + LinePosition(int line, int positionOnLine, IDocument doc) { + this.line = line; + this.position = positionOnLine; + this.fDoc = doc; + } + + int getOffset() throws BadLocationException { + return fDoc.getLineOffset(line-1) + position-1; + } + + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + final String PROJECT= "BlockComment"; + // Using any existing file just to open CEditor, the content is not used + final String filename= "/BlockComment/src/sample/Before.cpp"; + fCProject= EditorTestHelper.createCProject(PROJECT, "resources/formatter"); + fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.TAB); + fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(filename), true); + fDocument= EditorTestHelper.getSourceViewer(fEditor).getDocument(); + // Delete contents + fDocument.set(""); + } + + @Override + protected void tearDown() throws Exception { + EditorTestHelper.closeEditor(fEditor); + + if (fCProject != null) + CProjectHelper.delete(fCProject); + + super.tearDown(); + } + + protected void assertFormatterResult( + LinePosition startLinePosition, + LinePosition endLinePosition) throws Exception { + + StringBuffer[] contents= getContentsForTest(2); + String before = contents[0].toString(); + String after = contents[1].toString(); + + fDocument.set(before); + + SourceRange range = new SourceRange( startLinePosition.getOffset(), + endLinePosition.getOffset()-startLinePosition.getOffset() ); + fEditor.setSelection(range, true); + + IAction commentAction= fEditor.getAction("AddBlockComment"); + assertNotNull("No AddBlockComment action", commentAction); + commentAction.setEnabled(true); + commentAction.run(); + + String expected= after; + assertEquals(expected, fDocument.get()); + } + + //int i, j, k; + + //int i, /*j,*/ k; + public void testCommentPlain() throws Exception { + LinePosition startSelection = new LinePosition(1,8,fDocument); + LinePosition endSelection = new LinePosition(1,10,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i, + // j, + // k; + + //int /*i, + // j,*/ + // k; + public void testCommentPartialLines1() throws Exception { + LinePosition startSelection = new LinePosition(1,5,fDocument); + LinePosition endSelection = new LinePosition(2,7,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i, + // j, + // k; + + //int i, + ///* j, + // k*/; + public void testCommentPartialLines2() throws Exception { + LinePosition startSelection = new LinePosition(2,1,fDocument); + LinePosition endSelection = new LinePosition(3,6,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i; + //int j; + //int k; + + //int i; + ///*int j;*/ + //int k; + public void testCommentExactlyOneLine() throws Exception { + LinePosition startSelection = new LinePosition(2,1,fDocument); + LinePosition endSelection = new LinePosition(3,1,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i; + //int j; + //int k; + //int l; + + //int i; + ///* + //int j; + //int k; + //*/ + //int l; + public void testCommentTwoOrMoreLines() throws Exception { + LinePosition startSelection = new LinePosition(2,1,fDocument); + LinePosition endSelection = new LinePosition(4,1,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //const int i; + + ///*const*/ int i; + public void testCommentFirstCharacterInFile() throws Exception { + LinePosition startSelection = new LinePosition(1,1,fDocument); + LinePosition endSelection = new LinePosition(1,6,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //#include + //#include + + ///*#include */ + //#include + public void testCommentPreprocessorFirstLine() throws Exception { + LinePosition startSelection = new LinePosition(1,1,fDocument); + LinePosition endSelection = new LinePosition(1,6,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i; // comment + //int j; + + //int i; /*// comment*/ + //int j; + public void testCommentCppComment() throws Exception { + LinePosition startSelection = new LinePosition(1,10,fDocument); + LinePosition endSelection = new LinePosition(1,12,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //#include + //#include + //#include + + //#include + ///*#include */ + //#include + public void testCommentSpecialPartition() throws Exception { + LinePosition startSelection = new LinePosition(2,1,fDocument); + LinePosition endSelection = new LinePosition(3,1,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //#include + // #include \ + // + //#include + + //#include + ///* + // #include \ + // + //*/ + //#include + public void testCommentSpecialPartitionExtra() throws Exception { + LinePosition startSelection = new LinePosition(2,8,fDocument); + LinePosition endSelection = new LinePosition(2,10,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + ///*comment*/ + //int i; + + ///*comment*/ + //int i; + public void testCommentCommentNoScrewUp() throws Exception { + LinePosition startSelection = new LinePosition(1,1,fDocument); + LinePosition endSelection = new LinePosition(2,1,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + + //int i; + ///*comment*/ + //int j; + + ///*int i; + //comment*/ + //int j; + public void testCommentMergeUp() throws Exception { + LinePosition startSelection = new LinePosition(1,1,fDocument); + LinePosition endSelection = new LinePosition(2,5,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i; + ///*comment*/ + //int j; + + //int i; + ///*comment + //int j;*/ + public void testCommentMergeDown() throws Exception { + LinePosition startSelection = new LinePosition(2,5,fDocument); + LinePosition endSelection = new LinePosition(3,7,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + ///*comment1*/ + ///*comment2*/ + //int i; + + ///*comment1 + //comment2*/ + //int i; + public void testCommentMergeComments() throws Exception { + LinePosition startSelection = new LinePosition(1,5,fDocument); + LinePosition endSelection = new LinePosition(2,5,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + // + // + + // + ///* + // + //*/ + public void testCommentEndOfFileNoLoopingPlease() throws Exception { + // Don't care much about formatting but no looping please + LinePosition startSelection = new LinePosition(2,1,fDocument); + LinePosition endSelection = new LinePosition(3,1,fDocument); + assertFormatterResult(startSelection,endSelection); + } +} Index: ui/org/eclipse/cdt/ui/tests/text/RemoveBlockCommentTest.java =================================================================== RCS file: ui/org/eclipse/cdt/ui/tests/text/RemoveBlockCommentTest.java diff -N ui/org/eclipse/cdt/ui/tests/text/RemoveBlockCommentTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ui/org/eclipse/cdt/ui/tests/text/RemoveBlockCommentTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,282 @@ +/******************************************************************************* + * Copyright (c) 2007, 2008 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 + * Andrew Gvozdev + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text; + +import junit.framework.TestSuite; + +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.text.IDocument; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.testplugin.CProjectHelper; +import org.eclipse.cdt.ui.tests.BaseUITestCase; +import org.eclipse.cdt.ui.tests.text.AddBlockCommentTest.LinePosition; + +import org.eclipse.cdt.internal.core.model.ext.SourceRange; + +import org.eclipse.cdt.internal.ui.editor.CEditor; + +/** + * Tests for the RemoveBlockCommentAction. + * + * @since 5.0 + */ +public class RemoveBlockCommentTest extends BaseUITestCase { + private ICProject fCProject; + public static TestSuite suite() { + return suite(RemoveBlockCommentTest.class, "_"); + } + + private CEditor fEditor; + private IDocument fDocument; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + final String PROJECT= "BlockComment"; + // Using any existing file just to open CEditor, the content is not used + final String filename= "/BlockComment/src/sample/Before.cpp"; + fCProject= EditorTestHelper.createCProject(PROJECT, "resources/formatter"); + fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.TAB); + fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(filename), true); + + fDocument= EditorTestHelper.getSourceViewer(fEditor).getDocument(); + // Delete contents + fDocument.set(""); + } + + @Override + protected void tearDown() throws Exception { + EditorTestHelper.closeEditor(fEditor); + + if (fCProject != null) + CProjectHelper.delete(fCProject); + + super.tearDown(); + } + + protected void assertFormatterResult( + LinePosition startLinePosition, + LinePosition endLinePosition) throws Exception { + + StringBuffer[] contents= getContentsForTest(2); + String before = contents[0].toString(); + String after = contents[1].toString(); + + fDocument.set(before); + + SourceRange range = new SourceRange( startLinePosition.getOffset(), + endLinePosition.getOffset()-startLinePosition.getOffset() ); + fEditor.setSelection(range, true); + + IAction commentAction= fEditor.getAction("RemoveBlockComment"); + assertNotNull("No RemoveBlockComment action", commentAction); + commentAction.setEnabled(true); + commentAction.run(); + + String expected= after; + assertEquals(expected, fDocument.get()); + } + + //int i, /*j,*/ k; + + //int i, j, k; + public void testUncommentPlain() throws Exception { + LinePosition startSelection = new LinePosition(1,10,fDocument); + LinePosition endSelection = new LinePosition(1,11,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int /*j,*/ k; + + //int j, k; + public void testRightBorderIn() throws Exception { + LinePosition startSelection = new LinePosition(1,10,fDocument); + LinePosition endSelection = new LinePosition(1,11,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int /*j,*/ k; + + //int /*j,*/ k; + public void testRightBorderOut() throws Exception { + LinePosition startSelection = new LinePosition(1,11,fDocument); + LinePosition endSelection = new LinePosition(1,12,fDocument); + assertFormatterResult(startSelection,endSelection); + } + //123456789-123 + + //int /*j,*/ k; + + //int j, k; + public void testLeftBorderIn() throws Exception { + LinePosition startSelection = new LinePosition(1,5,fDocument); + LinePosition endSelection = new LinePosition(1,6,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int /*j,*/ k; + + //int /*j,*/ k; + public void testLeftBorderOut() throws Exception { + LinePosition startSelection = new LinePosition(1,4,fDocument); + LinePosition endSelection = new LinePosition(1,5,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int /*i, + // j,*/ + // k; + + //int i, + // j, + // k; + public void testUncommentPartialLines1() throws Exception { + LinePosition startSelection = new LinePosition(1,7,fDocument); + LinePosition endSelection = new LinePosition(2,2,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i, + ///* j, + // k*/; + + //int i, + // j, + // k; + public void testUncommentPartialLines2() throws Exception { + LinePosition startSelection = new LinePosition(2,1,fDocument); + LinePosition endSelection = new LinePosition(3,8,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i; + ///*int j;*/ + //int k; + + //int i; + //int j; + //int k; + public void testUncommentExactlyOneLineNoLoopingPlease() throws Exception { + LinePosition startSelection = new LinePosition(2,1,fDocument); + LinePosition endSelection = new LinePosition(3,1,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i; + ///* + //int j; + //int k; + //*/ + //int l; + + //int i; + //int j; + //int k; + //int l; + public void testUncommentTwoOrMoreLines() throws Exception { + LinePosition startSelection = new LinePosition(2,1,fDocument); + LinePosition endSelection = new LinePosition(4,1,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + ///*const*/ int i; + + //const int i; + public void testUncommentFirstCharacterInFile() throws Exception { + LinePosition startSelection = new LinePosition(1,1,fDocument); + LinePosition endSelection = new LinePosition(1,6,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + ///*#include */ + //#include + + //#include + //#include + public void testUncommentPreprocessorFirstLine() throws Exception { + LinePosition startSelection = new LinePosition(1,1,fDocument); + LinePosition endSelection = new LinePosition(1,6,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //int i; /*// comment*/ + //int j; + + //int i; // comment + //int j; + public void testUncommentCppComment() throws Exception { + LinePosition startSelection = new LinePosition(1,10,fDocument); + LinePosition endSelection = new LinePosition(1,12,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //#include + ///*#include */ + //#include + + //#include + //#include + //#include + public void testUncommentSpecialPartition() throws Exception { + LinePosition startSelection = new LinePosition(2,1,fDocument); + LinePosition endSelection = new LinePosition(3,1,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + //#include + ///* + // #include \ + // + //*/ + //#include + + //#include + // #include \ + // + //#include + public void testUncommentSpecialPartitionExtra() throws Exception { + LinePosition startSelection = new LinePosition(3,8,fDocument); + LinePosition endSelection = new LinePosition(3,10,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + ///*int i;*/ + ///*int j;*/ + //int k; + + //int i; + //int j; + //int k; + public void testUncommentConnectedComments() throws Exception { + LinePosition startSelection = new LinePosition(1,5,fDocument); + LinePosition endSelection = new LinePosition(2,5,fDocument); + assertFormatterResult(startSelection,endSelection); + } + + // + ///* + // + //*/ + + // + // + public void testUncommentEndOfFile() throws Exception { + LinePosition startSelection = new LinePosition(3,1,fDocument); + LinePosition endSelection = new LinePosition(5,2,fDocument); + assertFormatterResult(startSelection,endSelection); + } + +} #P org.eclipse.cdt.ui Index: src/org/eclipse/cdt/internal/ui/actions/AddBlockCommentAction.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt/all/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/AddBlockCommentAction.java,v retrieving revision 1.9 diff -u -r1.9 AddBlockCommentAction.java --- src/org/eclipse/cdt/internal/ui/actions/AddBlockCommentAction.java 11 Jun 2008 15:50:41 -0000 1.9 +++ src/org/eclipse/cdt/internal/ui/actions/AddBlockCommentAction.java 12 Jun 2008 04:18:22 -0000 @@ -16,19 +16,20 @@ import java.util.List; import java.util.ResourceBundle; -import org.eclipse.core.runtime.Assert; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.BadPartitioningException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocumentExtension3; +import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.ITypedRegion; +import org.eclipse.jface.text.Region; import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.cdt.ui.text.ICPartitions; /** - * Action that encloses the editor's current selection with Java block comment terminators + * Action that encloses the editor's current selection with Java/C block comment terminators * (/* and */). * * @since 3.0 @@ -51,104 +52,130 @@ * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3, org.eclipse.jdt.internal.ui.actions.BlockCommentAction.Edit.EditFactory) */ @Override - protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException { - int selectionOffset= selection.getOffset(); - int selectionEndOffset= selectionOffset + selection.getLength(); + protected void runInternal(ITextSelection selection, + IDocumentExtension3 docExtension, Edit.EditFactory factory) + throws BadLocationException, BadPartitioningException { + + if ( !(docExtension instanceof IDocument) ) return; + List edits= new LinkedList(); - ITypedRegion partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, selectionOffset, false); - - handleFirstPartition(partition, edits, factory, selectionOffset); - while (partition.getOffset() + partition.getLength() < selectionEndOffset) { - partition= handleInteriorPartition(partition, edits, factory, docExtension); + ITypedRegion firstPartition = docExtension.getPartition(ICPartitions.C_PARTITIONING, + selection.getOffset(), false); + ITypedRegion lastPartition = docExtension.getPartition(ICPartitions.C_PARTITIONING, + selection.getOffset() + selection.getLength() - 1, false); + + int commentAreaStart = selection.getOffset(); + int commentAreaEnd = selection.getOffset()+selection.getLength(); + // Include special partitions fully in the comment area + if (isSpecialPartition(firstPartition.getType())) { + commentAreaStart = firstPartition.getOffset(); } + if (isSpecialPartition(lastPartition.getType())) { + commentAreaEnd = lastPartition.getOffset() + lastPartition.getLength(); + } + Region estimatedCommentArea = new Region(commentAreaStart,commentAreaEnd-commentAreaStart); + - handleLastPartition(partition, edits, factory, selectionEndOffset); + Region commentArea = handleEnclosingPartitions(estimatedCommentArea, lastPartition, + (IDocument)docExtension, factory, edits); + + handleInteriorPartition(commentArea, firstPartition, docExtension, factory, edits); executeEdits(edits); } /** - * Handle the first partition of the selected text. + * Add enclosing comment tags for the whole area to be commented * - * @param partition - * @param edits - * @param factory - * @param offset - */ - private void handleFirstPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int offset) throws BadLocationException { - - int partOffset= partition.getOffset(); - String partType= partition.getType(); - - Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$ - - // first partition: mark start of comment - if (partType == IDocument.DEFAULT_CONTENT_TYPE) { - // Java code: right where selection starts - edits.add(factory.createEdit(offset, 0, getCommentStart())); - } else if (isSpecialPartition(partType)) { - // special types: include the entire partition - edits.add(factory.createEdit(partOffset, 0, getCommentStart())); - } // javadoc: no mark, will only start after comment + * @param commentArea initial comment area which can be adjusted + * @param lastPartition last partition + * @param doc document + * @param factory Edit factory + * @param edits List of edits to update the document + * @return new possibly adjusted comment area + * @throws BadLocationException + */ + private Region handleEnclosingPartitions(Region commentArea, + ITypedRegion lastPartition, IDocument doc, Edit.EditFactory factory, + List edits) throws BadLocationException { + + int commentAreaStart = commentArea.getOffset(); + int commentAreaEnd = commentArea.getOffset() + commentArea.getLength(); + + String commentStartTag = getCommentStart(); // "/*" + String commentEndTag = getCommentEnd(); // "*/" + + final String EOL = doc.getLineDelimiter(doc.getLineOfOffset(commentAreaStart)); + + boolean isLeftEol = commentAreaStart edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException { - - // end of previous partition - String partType= partition.getType(); - int partEndOffset= partition.getOffset() + partition.getLength(); - int tokenLength= getCommentStart().length(); - - if (partType == ICPartitions.C_MULTI_LINE_COMMENT) { - // already in a comment - remove ending mark - edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$ - } - - // advance to next partition - partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, partEndOffset, false); - partType= partition.getType(); - - // start of next partition - if (partType == ICPartitions.C_MULTI_LINE_COMMENT) { - // already in a comment - remove startToken - edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$ + private void handleInteriorPartition(IRegion commentArea, + ITypedRegion partition, IDocumentExtension3 docExtension, + Edit.EditFactory factory, List edits) + throws BadLocationException, BadPartitioningException { + + int commentAreaEnd = commentArea.getOffset() + commentArea.getLength(); + int prevPartitionEnd = -1; + int partitionEnd = partition.getOffset()+partition.getLength(); + + final int startCommentTokenLength = getCommentStart().length(); + final int endCommentTokenLength = getCommentEnd().length(); + + while (partitionEnd<=commentAreaEnd) { + if (partition.getType() == ICPartitions.C_MULTI_LINE_COMMENT) { + // already in a comment - remove start/end tokens + edits.add(factory.createEdit(partition.getOffset(), startCommentTokenLength, "")); //$NON-NLS-1$ + edits.add(factory.createEdit(partitionEnd - endCommentTokenLength, endCommentTokenLength, "")); //$NON-NLS-1$ + } + // advance to next partition + prevPartitionEnd = partitionEnd; + partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, partitionEnd, false); + partitionEnd = partition.getOffset() + partition.getLength(); + + // break the loop if we get stuck and no advance was made + if (partitionEnd<=prevPartitionEnd) break; } - return partition; - } - - /** - * Handles the end of the last partition. - * - * @param partition - * @param edits - * @param factory - * @param endOffset - */ - private void handleLastPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int endOffset) throws BadLocationException { - - String partType= partition.getType(); - - if (partType == IDocument.DEFAULT_CONTENT_TYPE) { - // normal java: end comment where selection ends - edits.add(factory.createEdit(endOffset, 0, getCommentEnd())); - } else if (isSpecialPartition(partType)) { - // special types: consume entire partition - edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd())); - } - } /** @@ -162,6 +189,7 @@ return partType == ICPartitions.C_CHARACTER || partType == ICPartitions.C_STRING || partType == ICPartitions.C_SINGLE_LINE_COMMENT + || partType == ICPartitions.C_MULTI_LINE_COMMENT || partType == ICPartitions.C_PREPROCESSOR; } Index: src/org/eclipse/cdt/internal/ui/actions/RemoveBlockCommentAction.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt/all/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/RemoveBlockCommentAction.java,v retrieving revision 1.7 diff -u -r1.7 RemoveBlockCommentAction.java --- src/org/eclipse/cdt/internal/ui/actions/RemoveBlockCommentAction.java 11 Jun 2008 15:50:41 -0000 1.7 +++ src/org/eclipse/cdt/internal/ui/actions/RemoveBlockCommentAction.java 12 Jun 2008 04:18:22 -0000 @@ -17,6 +17,7 @@ import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.BadPartitioningException; +import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocumentExtension3; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.ITypedRegion; @@ -25,7 +26,7 @@ import org.eclipse.cdt.ui.text.ICPartitions; /** - * Action that removes the enclosing comment marks from a Java block comment. + * Action that removes the enclosing comment marks from a Java/C block comment. * * @since 3.0 */ @@ -49,34 +50,57 @@ */ @Override protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadPartitioningException, BadLocationException { + if ( !(docExtension instanceof IDocument) ) return; + List edits= new LinkedList(); - int tokenLength= getCommentStart().length(); - int offset= selection.getOffset(); - int endOffset= offset + selection.getLength(); + int partitionStart = -1; + int partitionEnd = selection.getOffset(); - ITypedRegion partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, offset, false); - int partOffset= partition.getOffset(); - int partEndOffset= partOffset + partition.getLength(); - - while (partEndOffset < endOffset) { - + do { + ITypedRegion partition = docExtension.getPartition(ICPartitions.C_PARTITIONING, partitionEnd, false); + if (partition.getOffset() <= partitionStart) { + // If we did not advance break the loop + break; + } + partitionStart = partition.getOffset(); + partitionEnd = partitionStart + partition.getLength(); if (partition.getType() == ICPartitions.C_MULTI_LINE_COMMENT) { - edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$ - edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$ + uncommentPartition((IDocument)docExtension, factory, edits, partitionStart, partitionEnd); } - - partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, partEndOffset, false); - partOffset= partition.getOffset(); - partEndOffset= partOffset + partition.getLength(); - } + } while (partitionEnd < selection.getOffset()+selection.getLength()); + + executeEdits(edits); + } - if (partition.getType() == ICPartitions.C_MULTI_LINE_COMMENT) { - edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$ - edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$ + private void uncommentPartition(IDocument doc, Edit.EditFactory factory, + List edits, int partitionStart, int partitionEnd) + throws BadLocationException { + + final String EOL = doc.getLineDelimiter(doc.getLineOfOffset(partitionStart)); + int startCommentTokenLength = getCommentStart().length(); + int endCommentTokenLength = getCommentEnd().length(); + + // Remove whole line (with EOL) if it contains start or end comment tag + // and nothing else + if (partitionStart > 0) { + // start comment tag '/*' + String suspect = doc.get(partitionStart-EOL.length(),startCommentTokenLength+2*EOL.length()); + if (suspect.equals(EOL+getCommentStart()+EOL)) { + startCommentTokenLength = startCommentTokenLength+EOL.length(); + } + } + int commentContentEnd = partitionEnd - endCommentTokenLength; + if (partitionEnd < doc.getLength()) { + // end comment tag '*/' + String suspect = doc.get(commentContentEnd-EOL.length(),endCommentTokenLength+2*EOL.length()); + if (suspect.equals(EOL+getCommentEnd()+EOL)) { + endCommentTokenLength = endCommentTokenLength+EOL.length(); + } } - executeEdits(edits); + edits.add(factory.createEdit(partitionStart, startCommentTokenLength, "")); //$NON-NLS-1$ + edits.add(factory.createEdit(commentContentEnd, endCommentTokenLength, "")); //$NON-NLS-1$ } /*