1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-11 18:25:40 +02:00

Avoid overlapping folding positions for #if/#elif/#else

This commit is contained in:
Anton Leherbauer 2006-09-12 07:06:04 +00:00
parent 5cf4415dde
commit d643f4f018
4 changed files with 30 additions and 20 deletions

View file

@ -71,7 +71,7 @@ public class FoldingTest extends TestCase {
fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(fTestFilename), true); fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(fTestFilename), true);
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor); fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100)); assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 300));
} }
protected void tearDown () throws Exception { protected void tearDown () throws Exception {
@ -90,12 +90,17 @@ public class FoldingTest extends TestCase {
super.tearDown(); super.tearDown();
} }
protected void assertEqualPositions(Position[] expected, Position[] actual) { protected void assertEqualPositions(Position[] expected, Position[] actual) throws BadLocationException {
assertEquals(expected.length, actual.length); assertEquals(expected.length, actual.length);
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());
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()); assertEquals(expected[i].isDeleted(), actual[i].isDeleted());
assertEquals(expected[i].getOffset(), actual[i].getOffset()); assertEquals(expectedStartLine, actualStartLine);
assertEquals(expected[i].getLength(), actual[i].getLength()); assertEquals(expectedEndLine, actualEndLine);
} }
} }
@ -143,17 +148,17 @@ public class FoldingTest extends TestCase {
Position[] expected= new Position[] { Position[] expected= new Position[] {
createPosition(0, 2), createPosition(0, 2),
createPosition(4, 7), createPosition(4, 7),
createPosition(9, 13), createPosition(9, 12),
createPosition(10, 12), createPosition(10, 12),
createPosition(13, 15), createPosition(13, 14),
createPosition(15, 27), createPosition(15, 27),
createPosition(16, 26), createPosition(16, 26),
createPosition(17, 21), createPosition(17, 20),
createPosition(18, 20), createPosition(18, 20),
createPosition(21, 25), createPosition(21, 25),
createPosition(22, 24), createPosition(22, 24),
createPosition(29, 31), createPosition(29, 31),
createPosition(34, 36), createPosition(34, 35),
createPosition(35, 40), createPosition(35, 40),
createPosition(36, 38), createPosition(36, 38),
createPosition(42, 46), createPosition(42, 46),
@ -163,7 +168,7 @@ public class FoldingTest extends TestCase {
createPosition(61, 63), createPosition(61, 63),
createPosition(65, 67), createPosition(65, 67),
}; };
if (true) System.out.println(toString(actual)); if (false) System.out.println(toString(actual));
assertEqualPositions(expected, actual); assertEqualPositions(expected, actual);
} }

View file

@ -73,19 +73,24 @@ public class InactiveCodeHighlightingTest extends TestCase {
super.tearDown(); super.tearDown();
} }
protected void assertEqualPositions(Position[] expected, Position[] actual) { protected void assertEqualPositions(Position[] expected, Position[] actual) throws BadLocationException {
assertEquals(expected.length, actual.length); assertEquals(expected.length, actual.length);
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());
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()); assertEquals(expected[i].isDeleted(), actual[i].isDeleted());
assertEquals(expected[i].getOffset(), actual[i].getOffset()); assertEquals(expectedStartLine, actualStartLine);
assertEquals(expected[i].getLength(), actual[i].getLength()); assertEquals(expectedEndLine, actualEndLine);
} }
} }
protected Position createPosition(int startLine, int endLine) throws BadLocationException { protected Position createPosition(int startLine, int endLine) throws BadLocationException {
IDocument document= fSourceViewer.getDocument(); IDocument document= fSourceViewer.getDocument();
int startOffset= document.getLineOffset(startLine); int startOffset= document.getLineOffset(startLine);
int endOffset= document.getLineOffset(endLine) + document.getLineLength(endLine) - document.getLineDelimiter(endLine).length(); int endOffset= document.getLineOffset(endLine) + document.getLineLength(endLine);
return new Position(startOffset, endOffset - startOffset); return new Position(startOffset, endOffset - startOffset);
} }
@ -96,7 +101,7 @@ public class InactiveCodeHighlightingTest extends TestCase {
for (int i= 0, n= positions.length; i < n; i++) { for (int i= 0, n= positions.length; i < n; i++) {
Position position= positions[i]; Position position= positions[i];
int startLine= document.getLineOfOffset(position.getOffset()); int startLine= document.getLineOfOffset(position.getOffset());
int endLine= document.getLineOfOffset(position.getOffset()+position.getLength()-1); int endLine= document.getLineOfOffset(position.getOffset()+position.getLength());
buf.append("\tcreatePosition(" + startLine + ", " + endLine + "),\n"); buf.append("\tcreatePosition(" + startLine + ", " + endLine + "),\n");
} }
buf.append("};\n"); buf.append("};\n");
@ -116,9 +121,9 @@ public class InactiveCodeHighlightingTest extends TestCase {
createPosition(2, 4), createPosition(2, 4),
createPosition(11, 13), createPosition(11, 13),
createPosition(15, 22), createPosition(15, 22),
createPosition(28, 34), createPosition(28, 33),
createPosition(39, 41), createPosition(39, 41),
createPosition(47, 58), createPosition(47, 57),
createPosition(67, 69), createPosition(67, 69),
}; };
if (false) System.out.println(toString(actual)); if (false) System.out.println(toString(actual));

View file

@ -262,7 +262,7 @@ public class InactiveCodeHighlighting implements ICReconcilingListener {
inInactiveCode = true; inInactiveCode = true;
} else if (elseStmt.taken() && inInactiveCode) { } else if (elseStmt.taken() && inInactiveCode) {
IASTNodeLocation nodeLocation = elseStmt.getNodeLocations()[0]; IASTNodeLocation nodeLocation = elseStmt.getNodeLocations()[0];
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength(); int inactiveCodeEnd = nodeLocation.getNodeOffset();
positions.add(new HighlightPosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, fHighlightKey)); positions.add(new HighlightPosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, fHighlightKey));
inInactiveCode = false; inInactiveCode = false;
} }
@ -274,7 +274,7 @@ public class InactiveCodeHighlighting implements ICReconcilingListener {
inInactiveCode = true; inInactiveCode = true;
} else if (elifStmt.taken() && inInactiveCode) { } else if (elifStmt.taken() && inInactiveCode) {
IASTNodeLocation nodeLocation = elifStmt.getNodeLocations()[0]; IASTNodeLocation nodeLocation = elifStmt.getNodeLocations()[0];
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength(); int inactiveCodeEnd = nodeLocation.getNodeOffset();
positions.add(new HighlightPosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, fHighlightKey)); positions.add(new HighlightPosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, fHighlightKey));
inInactiveCode = false; inInactiveCode = false;
} }

View file

@ -1139,7 +1139,7 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
Branch branch= (Branch)branchStack.pop(); Branch branch= (Branch)branchStack.pop();
IASTPreprocessorElseStatement elseStmt = (IASTPreprocessorElseStatement)statement; IASTPreprocessorElseStatement elseStmt = (IASTPreprocessorElseStatement)statement;
branchStack.push(new Branch(stmtLocation.getNodeOffset(), elseStmt.taken())); branchStack.push(new Branch(stmtLocation.getNodeOffset(), elseStmt.taken()));
branch.setEndOffset(stmtLocation.getNodeOffset() + stmtLocation.getNodeLength()); branch.setEndOffset(stmtLocation.getNodeOffset() - 1);
IRegion converted= converter != null ? converter.historicToActual(branch) : branch; IRegion converted= converter != null ? converter.historicToActual(branch) : branch;
branches.add(new Branch(converted.getOffset(), converted.getLength(), branch.taken())); branches.add(new Branch(converted.getOffset(), converted.getLength(), branch.taken()));
} else if (statement instanceof IASTPreprocessorElifStatement) { } else if (statement instanceof IASTPreprocessorElifStatement) {
@ -1150,7 +1150,7 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
Branch branch= (Branch)branchStack.pop(); Branch branch= (Branch)branchStack.pop();
IASTPreprocessorElifStatement elifStmt = (IASTPreprocessorElifStatement) statement; IASTPreprocessorElifStatement elifStmt = (IASTPreprocessorElifStatement) statement;
branchStack.push(new Branch(stmtLocation.getNodeOffset(), elifStmt.taken())); branchStack.push(new Branch(stmtLocation.getNodeOffset(), elifStmt.taken()));
branch.setEndOffset(stmtLocation.getNodeOffset() + stmtLocation.getNodeLength()); branch.setEndOffset(stmtLocation.getNodeOffset() - 1);
IRegion converted= converter != null ? converter.historicToActual(branch) : branch; IRegion converted= converter != null ? converter.historicToActual(branch) : branch;
branches.add(new Branch(converted.getOffset(), converted.getLength(), branch.taken())); branches.add(new Branch(converted.getOffset(), converted.getLength(), branch.taken()));
} else if (statement instanceof IASTPreprocessorEndifStatement) { } else if (statement instanceof IASTPreprocessorEndifStatement) {