1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Fix for 214590: Code Folding issue

This commit is contained in:
Anton Leherbauer 2008-01-18 08:38:21 +00:00
parent f4bbc8aaa1
commit c9c34a49b4
3 changed files with 79 additions and 31 deletions

View file

@ -66,3 +66,11 @@ struct CppStruct {
union CppUnion {
int unionField;
};
// http://bugs.eclipse.org/214590
int
main(int argc,
char *argv[])
{
return 0;
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others.
* Copyright (c) 2006, 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
@ -23,9 +23,11 @@ import junit.framework.TestSuite;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.projection.IProjectionPosition;
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
import org.eclipse.cdt.core.model.ICProject;
@ -40,6 +42,20 @@ import org.eclipse.cdt.internal.ui.editor.CEditor;
*/
public class FoldingTest extends TestCase {
private static class ProjectionPosition extends Position implements IProjectionPosition, IRegion {
private int fCaptionOffset;
ProjectionPosition(int offset, int length, int captionOffset) {
super(offset, length);
fCaptionOffset= captionOffset;
}
public int computeCaptionOffset(IDocument document) throws BadLocationException {
return fCaptionOffset;
}
public IRegion[] computeProjectionRegions(IDocument document) throws BadLocationException {
return new IRegion[] { this };
}
}
private static final String LINKED_FOLDER= "resources/folding";
private static final String PROJECT= "FoldingTest";
@ -92,13 +108,24 @@ public class FoldingTest extends TestCase {
assertEquals(expected.length, actual.length);
IDocument document= fSourceViewer.getDocument();
for (int i= 0, n= expected.length; i < n; i++) {
int expectedStartLine= document.getLineOfOffset(expected[i].getOffset());
int expectedEndLine= document.getLineOfOffset(expected[i].getOffset()+expected[i].getLength());
int actualStartLine= document.getLineOfOffset(actual[i].getOffset());
int actualEndLine= document.getLineOfOffset(actual[i].getOffset()+expected[i].getLength());
assertEquals(expected[i].isDeleted(), actual[i].isDeleted());
final Position exp = expected[i];
int expectedStartLine= document.getLineOfOffset(exp.getOffset());
int expectedEndLine= document.getLineOfOffset(exp.getOffset()+exp.getLength());
final Position act = actual[i];
int actualStartLine= document.getLineOfOffset(act.getOffset());
int actualEndLine= document.getLineOfOffset(act.getOffset()+exp.getLength());
assertEquals(exp.isDeleted(), act.isDeleted());
assertEquals(expectedStartLine, actualStartLine);
assertEquals(expectedEndLine, actualEndLine);
if (exp instanceof IProjectionPosition) {
int expectedCaptionOffset= ((IProjectionPosition)exp).computeCaptionOffset(document);
if (act instanceof IProjectionPosition) {
int actualCaptionOffset= ((IProjectionPosition)act).computeCaptionOffset(document);
assertEquals(expectedCaptionOffset, actualCaptionOffset);
} else {
assertEquals(expectedCaptionOffset, 0);
}
}
}
}
@ -109,6 +136,14 @@ public class FoldingTest extends TestCase {
return new Position(startOffset, endOffset - startOffset);
}
protected Position createPosition(int startLine, int endLine, int captionLine) throws BadLocationException {
IDocument document= fSourceViewer.getDocument();
int startOffset= document.getLineOffset(startLine);
int endOffset= document.getLineOffset(endLine) + document.getLineLength(endLine);
int captionOffset= document.getLineOffset(captionLine);
return new ProjectionPosition(startOffset, endOffset - startOffset, captionOffset - startOffset);
}
String toString(Position[] positions) throws BadLocationException {
StringBuffer buf= new StringBuffer();
IDocument document= fSourceViewer.getDocument();
@ -165,8 +200,9 @@ public class FoldingTest extends TestCase {
createPosition(57, 59),
createPosition(61, 63),
createPosition(65, 67),
createPosition(70, 75, 71),
};
if (false) System.out.println(toString(actual));
if (true) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
@ -188,8 +224,10 @@ public class FoldingTest extends TestCase {
createPosition(57, 59),
createPosition(61, 63),
createPosition(65, 67),
createPosition(70, 75, 71),
};
if (false) System.out.println(toString(actual));
if (true) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2007 IBM Corporation and others.
* Copyright (c) 2000, 2008 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
@ -64,7 +64,6 @@ import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IElementChangedListener;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.IMember;
import org.eclipse.cdt.core.model.IParent;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ISourceReference;
@ -444,17 +443,17 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
*/
private static final class CElementPosition extends Position implements IProjectionPosition {
private IMember fMember;
private ICElement fElement;
public CElementPosition(int offset, int length, IMember member) {
public CElementPosition(int offset, int length, ICElement element) {
super(offset, length);
Assert.isNotNull(member);
fMember= member;
Assert.isNotNull(element);
fElement= element;
}
public void setMember(IMember member) {
public void setElement(ICElement member) {
Assert.isNotNull(member);
fMember= member;
fElement= member;
}
/*
@ -468,10 +467,11 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
* lead to reentrant situations. Therefore, we optimistically
* assume that the name range is correct, but double check the
* received lines below. */
ISourceRange sourceRange= fMember.getSourceRange();
if (sourceRange != null)
nameStart= sourceRange.getIdStartPos();
if (fElement instanceof ISourceReference) {
ISourceRange sourceRange= ((ISourceReference) fElement).getSourceRange();
if (sourceRange != null)
nameStart= sourceRange.getIdStartPos();
}
} catch (CModelException e) {
// ignore and use default
}
@ -521,9 +521,11 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
int nameStart= offset;
try {
// need a reconcile here?
ISourceRange sourceRange= fMember.getSourceRange();
if (sourceRange != null)
nameStart= sourceRange.getIdStartPos();
if (fElement instanceof ISourceReference) {
ISourceRange sourceRange= ((ISourceReference) fElement).getSourceRange();
if (sourceRange != null)
nameStart= sourceRange.getIdStartPos();
}
} catch (CModelException e) {
// ignore and use default
}
@ -930,9 +932,9 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
Object element= match.annotation.getElement();
deleted.setElement(element);
deletedPosition.setLength(match.position.getLength());
if (deletedPosition instanceof CElementPosition && element instanceof IMember) {
CElementPosition jep= (CElementPosition) deletedPosition;
jep.setMember((IMember) element);
if (deletedPosition instanceof CElementPosition && element instanceof ICElement) {
CElementPosition cep= (CElementPosition) deletedPosition;
cep.setElement((ICElement) element);
}
deletionIterator.remove();
@ -1333,7 +1335,7 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
if (regions.length > 0) {
IRegion normalized= alignRegion(regions[regions.length - 1], ctx, true);
if (normalized != null) {
Position position= element instanceof IMember ? createMemberPosition(normalized, (IMember) element) : createCommentPosition(normalized);
Position position= createElementPosition(normalized, element);
if (position != null) {
collapse= collapse && !position.includes(fCursorPosition);
ctx.addProjectionRange(new CProjectionAnnotation(collapse, element, false), position);
@ -1384,16 +1386,16 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
}
/**
* Creates a folding position that remembers its member from an
* Creates a folding position that remembers its element from an
* {@link #alignRegion(IRegion, DefaultCFoldingStructureProvider.FoldingStructureComputationContext, boolean) aligned}
* region.
*
* @param aligned an aligned region
* @param member the member to remember
* @param element the element to remember
* @return a folding position corresponding to <code>aligned</code>
*/
protected final Position createMemberPosition(IRegion aligned, IMember member) {
return new CElementPosition(aligned.getOffset(), aligned.getLength(), member);
protected final Position createElementPosition(IRegion aligned, ICElement element) {
return new CElementPosition(aligned.getOffset(), aligned.getLength(), element);
}
/**