mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-24 09:25:31 +02:00
Avoid overlapping folding positions for #if/#elif/#else
This commit is contained in:
parent
5cf4415dde
commit
d643f4f018
4 changed files with 30 additions and 20 deletions
|
@ -71,7 +71,7 @@ public class FoldingTest extends TestCase {
|
|||
|
||||
fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(fTestFilename), true);
|
||||
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
|
||||
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
|
||||
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 300));
|
||||
}
|
||||
|
||||
protected void tearDown () throws Exception {
|
||||
|
@ -90,12 +90,17 @@ public class FoldingTest extends TestCase {
|
|||
super.tearDown();
|
||||
}
|
||||
|
||||
protected void assertEqualPositions(Position[] expected, Position[] actual) {
|
||||
protected void assertEqualPositions(Position[] expected, Position[] actual) throws BadLocationException {
|
||||
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());
|
||||
assertEquals(expected[i].getOffset(), actual[i].getOffset());
|
||||
assertEquals(expected[i].getLength(), actual[i].getLength());
|
||||
assertEquals(expectedStartLine, actualStartLine);
|
||||
assertEquals(expectedEndLine, actualEndLine);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,17 +148,17 @@ public class FoldingTest extends TestCase {
|
|||
Position[] expected= new Position[] {
|
||||
createPosition(0, 2),
|
||||
createPosition(4, 7),
|
||||
createPosition(9, 13),
|
||||
createPosition(9, 12),
|
||||
createPosition(10, 12),
|
||||
createPosition(13, 15),
|
||||
createPosition(13, 14),
|
||||
createPosition(15, 27),
|
||||
createPosition(16, 26),
|
||||
createPosition(17, 21),
|
||||
createPosition(17, 20),
|
||||
createPosition(18, 20),
|
||||
createPosition(21, 25),
|
||||
createPosition(22, 24),
|
||||
createPosition(29, 31),
|
||||
createPosition(34, 36),
|
||||
createPosition(34, 35),
|
||||
createPosition(35, 40),
|
||||
createPosition(36, 38),
|
||||
createPosition(42, 46),
|
||||
|
@ -163,7 +168,7 @@ public class FoldingTest extends TestCase {
|
|||
createPosition(61, 63),
|
||||
createPosition(65, 67),
|
||||
};
|
||||
if (true) System.out.println(toString(actual));
|
||||
if (false) System.out.println(toString(actual));
|
||||
assertEqualPositions(expected, actual);
|
||||
}
|
||||
|
||||
|
|
|
@ -73,19 +73,24 @@ public class InactiveCodeHighlightingTest extends TestCase {
|
|||
super.tearDown();
|
||||
}
|
||||
|
||||
protected void assertEqualPositions(Position[] expected, Position[] actual) {
|
||||
protected void assertEqualPositions(Position[] expected, Position[] actual) throws BadLocationException {
|
||||
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());
|
||||
assertEquals(expected[i].getOffset(), actual[i].getOffset());
|
||||
assertEquals(expected[i].getLength(), actual[i].getLength());
|
||||
assertEquals(expectedStartLine, actualStartLine);
|
||||
assertEquals(expectedEndLine, actualEndLine);
|
||||
}
|
||||
}
|
||||
|
||||
protected Position createPosition(int startLine, int endLine) throws BadLocationException {
|
||||
IDocument document= fSourceViewer.getDocument();
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -96,7 +101,7 @@ public class InactiveCodeHighlightingTest extends TestCase {
|
|||
for (int i= 0, n= positions.length; i < n; i++) {
|
||||
Position position= positions[i];
|
||||
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("};\n");
|
||||
|
@ -116,9 +121,9 @@ public class InactiveCodeHighlightingTest extends TestCase {
|
|||
createPosition(2, 4),
|
||||
createPosition(11, 13),
|
||||
createPosition(15, 22),
|
||||
createPosition(28, 34),
|
||||
createPosition(28, 33),
|
||||
createPosition(39, 41),
|
||||
createPosition(47, 58),
|
||||
createPosition(47, 57),
|
||||
createPosition(67, 69),
|
||||
};
|
||||
if (false) System.out.println(toString(actual));
|
||||
|
|
|
@ -262,7 +262,7 @@ public class InactiveCodeHighlighting implements ICReconcilingListener {
|
|||
inInactiveCode = true;
|
||||
} else if (elseStmt.taken() && inInactiveCode) {
|
||||
IASTNodeLocation nodeLocation = elseStmt.getNodeLocations()[0];
|
||||
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
||||
int inactiveCodeEnd = nodeLocation.getNodeOffset();
|
||||
positions.add(new HighlightPosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, fHighlightKey));
|
||||
inInactiveCode = false;
|
||||
}
|
||||
|
@ -274,7 +274,7 @@ public class InactiveCodeHighlighting implements ICReconcilingListener {
|
|||
inInactiveCode = true;
|
||||
} else if (elifStmt.taken() && inInactiveCode) {
|
||||
IASTNodeLocation nodeLocation = elifStmt.getNodeLocations()[0];
|
||||
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
||||
int inactiveCodeEnd = nodeLocation.getNodeOffset();
|
||||
positions.add(new HighlightPosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, fHighlightKey));
|
||||
inInactiveCode = false;
|
||||
}
|
||||
|
|
|
@ -1139,7 +1139,7 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
|
|||
Branch branch= (Branch)branchStack.pop();
|
||||
IASTPreprocessorElseStatement elseStmt = (IASTPreprocessorElseStatement)statement;
|
||||
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;
|
||||
branches.add(new Branch(converted.getOffset(), converted.getLength(), branch.taken()));
|
||||
} else if (statement instanceof IASTPreprocessorElifStatement) {
|
||||
|
@ -1150,7 +1150,7 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
|
|||
Branch branch= (Branch)branchStack.pop();
|
||||
IASTPreprocessorElifStatement elifStmt = (IASTPreprocessorElifStatement) statement;
|
||||
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;
|
||||
branches.add(new Branch(converted.getOffset(), converted.getLength(), branch.taken()));
|
||||
} else if (statement instanceof IASTPreprocessorEndifStatement) {
|
||||
|
|
Loading…
Add table
Reference in a new issue