mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 01:36:01 +02:00
Fix for 214590: Code Folding issue
This commit is contained in:
parent
f4bbc8aaa1
commit
c9c34a49b4
3 changed files with 79 additions and 31 deletions
|
@ -66,3 +66,11 @@ struct CppStruct {
|
||||||
union CppUnion {
|
union CppUnion {
|
||||||
int unionField;
|
int unionField;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// http://bugs.eclipse.org/214590
|
||||||
|
int
|
||||||
|
main(int argc,
|
||||||
|
char *argv[])
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* 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.preference.IPreferenceStore;
|
||||||
import org.eclipse.jface.text.BadLocationException;
|
import org.eclipse.jface.text.BadLocationException;
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
|
import org.eclipse.jface.text.IRegion;
|
||||||
import org.eclipse.jface.text.Position;
|
import org.eclipse.jface.text.Position;
|
||||||
import org.eclipse.jface.text.source.Annotation;
|
import org.eclipse.jface.text.source.Annotation;
|
||||||
import org.eclipse.jface.text.source.SourceViewer;
|
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.jface.text.source.projection.ProjectionAnnotationModel;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.model.ICProject;
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
|
@ -40,6 +42,20 @@ import org.eclipse.cdt.internal.ui.editor.CEditor;
|
||||||
*/
|
*/
|
||||||
public class FoldingTest extends TestCase {
|
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 LINKED_FOLDER= "resources/folding";
|
||||||
private static final String PROJECT= "FoldingTest";
|
private static final String PROJECT= "FoldingTest";
|
||||||
|
|
||||||
|
@ -92,13 +108,24 @@ public class FoldingTest extends TestCase {
|
||||||
assertEquals(expected.length, actual.length);
|
assertEquals(expected.length, actual.length);
|
||||||
IDocument document= fSourceViewer.getDocument();
|
IDocument document= fSourceViewer.getDocument();
|
||||||
for (int i= 0, n= expected.length; i < n; i++) {
|
for (int i= 0, n= expected.length; i < n; i++) {
|
||||||
int expectedStartLine= document.getLineOfOffset(expected[i].getOffset());
|
final Position exp = expected[i];
|
||||||
int expectedEndLine= document.getLineOfOffset(expected[i].getOffset()+expected[i].getLength());
|
int expectedStartLine= document.getLineOfOffset(exp.getOffset());
|
||||||
int actualStartLine= document.getLineOfOffset(actual[i].getOffset());
|
int expectedEndLine= document.getLineOfOffset(exp.getOffset()+exp.getLength());
|
||||||
int actualEndLine= document.getLineOfOffset(actual[i].getOffset()+expected[i].getLength());
|
final Position act = actual[i];
|
||||||
assertEquals(expected[i].isDeleted(), actual[i].isDeleted());
|
int actualStartLine= document.getLineOfOffset(act.getOffset());
|
||||||
|
int actualEndLine= document.getLineOfOffset(act.getOffset()+exp.getLength());
|
||||||
|
assertEquals(exp.isDeleted(), act.isDeleted());
|
||||||
assertEquals(expectedStartLine, actualStartLine);
|
assertEquals(expectedStartLine, actualStartLine);
|
||||||
assertEquals(expectedEndLine, actualEndLine);
|
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);
|
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 {
|
String toString(Position[] positions) throws BadLocationException {
|
||||||
StringBuffer buf= new StringBuffer();
|
StringBuffer buf= new StringBuffer();
|
||||||
IDocument document= fSourceViewer.getDocument();
|
IDocument document= fSourceViewer.getDocument();
|
||||||
|
@ -165,8 +200,9 @@ public class FoldingTest extends TestCase {
|
||||||
createPosition(57, 59),
|
createPosition(57, 59),
|
||||||
createPosition(61, 63),
|
createPosition(61, 63),
|
||||||
createPosition(65, 67),
|
createPosition(65, 67),
|
||||||
|
createPosition(70, 75, 71),
|
||||||
};
|
};
|
||||||
if (false) System.out.println(toString(actual));
|
if (true) System.out.println(toString(actual));
|
||||||
assertEqualPositions(expected, actual);
|
assertEqualPositions(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,8 +224,10 @@ public class FoldingTest extends TestCase {
|
||||||
createPosition(57, 59),
|
createPosition(57, 59),
|
||||||
createPosition(61, 63),
|
createPosition(61, 63),
|
||||||
createPosition(65, 67),
|
createPosition(65, 67),
|
||||||
|
createPosition(70, 75, 71),
|
||||||
};
|
};
|
||||||
if (false) System.out.println(toString(actual));
|
if (true) System.out.println(toString(actual));
|
||||||
assertEqualPositions(expected, actual);
|
assertEqualPositions(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* 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.ICElement;
|
||||||
import org.eclipse.cdt.core.model.IElementChangedListener;
|
import org.eclipse.cdt.core.model.IElementChangedListener;
|
||||||
import org.eclipse.cdt.core.model.ILanguage;
|
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.IParent;
|
||||||
import org.eclipse.cdt.core.model.ISourceRange;
|
import org.eclipse.cdt.core.model.ISourceRange;
|
||||||
import org.eclipse.cdt.core.model.ISourceReference;
|
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 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);
|
super(offset, length);
|
||||||
Assert.isNotNull(member);
|
Assert.isNotNull(element);
|
||||||
fMember= member;
|
fElement= element;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMember(IMember member) {
|
public void setElement(ICElement member) {
|
||||||
Assert.isNotNull(member);
|
Assert.isNotNull(member);
|
||||||
fMember= member;
|
fElement= member;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -468,10 +467,11 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
|
||||||
* lead to reentrant situations. Therefore, we optimistically
|
* lead to reentrant situations. Therefore, we optimistically
|
||||||
* assume that the name range is correct, but double check the
|
* assume that the name range is correct, but double check the
|
||||||
* received lines below. */
|
* received lines below. */
|
||||||
ISourceRange sourceRange= fMember.getSourceRange();
|
if (fElement instanceof ISourceReference) {
|
||||||
|
ISourceRange sourceRange= ((ISourceReference) fElement).getSourceRange();
|
||||||
if (sourceRange != null)
|
if (sourceRange != null)
|
||||||
nameStart= sourceRange.getIdStartPos();
|
nameStart= sourceRange.getIdStartPos();
|
||||||
|
}
|
||||||
} catch (CModelException e) {
|
} catch (CModelException e) {
|
||||||
// ignore and use default
|
// ignore and use default
|
||||||
}
|
}
|
||||||
|
@ -521,9 +521,11 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
|
||||||
int nameStart= offset;
|
int nameStart= offset;
|
||||||
try {
|
try {
|
||||||
// need a reconcile here?
|
// need a reconcile here?
|
||||||
ISourceRange sourceRange= fMember.getSourceRange();
|
if (fElement instanceof ISourceReference) {
|
||||||
|
ISourceRange sourceRange= ((ISourceReference) fElement).getSourceRange();
|
||||||
if (sourceRange != null)
|
if (sourceRange != null)
|
||||||
nameStart= sourceRange.getIdStartPos();
|
nameStart= sourceRange.getIdStartPos();
|
||||||
|
}
|
||||||
} catch (CModelException e) {
|
} catch (CModelException e) {
|
||||||
// ignore and use default
|
// ignore and use default
|
||||||
}
|
}
|
||||||
|
@ -930,9 +932,9 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
|
||||||
Object element= match.annotation.getElement();
|
Object element= match.annotation.getElement();
|
||||||
deleted.setElement(element);
|
deleted.setElement(element);
|
||||||
deletedPosition.setLength(match.position.getLength());
|
deletedPosition.setLength(match.position.getLength());
|
||||||
if (deletedPosition instanceof CElementPosition && element instanceof IMember) {
|
if (deletedPosition instanceof CElementPosition && element instanceof ICElement) {
|
||||||
CElementPosition jep= (CElementPosition) deletedPosition;
|
CElementPosition cep= (CElementPosition) deletedPosition;
|
||||||
jep.setMember((IMember) element);
|
cep.setElement((ICElement) element);
|
||||||
}
|
}
|
||||||
|
|
||||||
deletionIterator.remove();
|
deletionIterator.remove();
|
||||||
|
@ -1333,7 +1335,7 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
|
||||||
if (regions.length > 0) {
|
if (regions.length > 0) {
|
||||||
IRegion normalized= alignRegion(regions[regions.length - 1], ctx, true);
|
IRegion normalized= alignRegion(regions[regions.length - 1], ctx, true);
|
||||||
if (normalized != null) {
|
if (normalized != null) {
|
||||||
Position position= element instanceof IMember ? createMemberPosition(normalized, (IMember) element) : createCommentPosition(normalized);
|
Position position= createElementPosition(normalized, element);
|
||||||
if (position != null) {
|
if (position != null) {
|
||||||
collapse= collapse && !position.includes(fCursorPosition);
|
collapse= collapse && !position.includes(fCursorPosition);
|
||||||
ctx.addProjectionRange(new CProjectionAnnotation(collapse, element, false), position);
|
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}
|
* {@link #alignRegion(IRegion, DefaultCFoldingStructureProvider.FoldingStructureComputationContext, boolean) aligned}
|
||||||
* region.
|
* region.
|
||||||
*
|
*
|
||||||
* @param aligned an 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>
|
* @return a folding position corresponding to <code>aligned</code>
|
||||||
*/
|
*/
|
||||||
protected final Position createMemberPosition(IRegion aligned, IMember member) {
|
protected final Position createElementPosition(IRegion aligned, ICElement element) {
|
||||||
return new CElementPosition(aligned.getOffset(), aligned.getLength(), member);
|
return new CElementPosition(aligned.getOffset(), aligned.getLength(), element);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue