1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 23:05:47 +02:00

Bug 255018 - [editor] Highlight inactive code of unterminated #ifdef

This commit is contained in:
Anton Leherbauer 2008-11-12 16:02:09 +00:00
parent 3a657e5260
commit 9da49555f6
6 changed files with 27 additions and 6 deletions

View file

@ -126,3 +126,6 @@ void foo() {
{ {
} }
} }
// http://bugs.eclipse.org/255018
#if 0
// #endif missing

View file

@ -70,3 +70,7 @@
#endif #endif
#endif // unbalanced endif because of invalid ifdef above #endif // unbalanced endif because of invalid ifdef above
#if foo // unterminated #if - http://bugs.eclipse.org/255018
// inactive code
// #endif

View file

@ -233,6 +233,7 @@ public class FoldingTest extends TestCase {
createPosition(119, 127), createPosition(119, 127),
createPosition(120, 122), createPosition(120, 122),
createPosition(123, 126), createPosition(123, 126),
createPosition(129, 130),
}; };
assertEquals(toString(expected), toString(actual)); assertEquals(toString(expected), toString(actual));
assertEqualPositions(expected, actual); assertEqualPositions(expected, actual);

View file

@ -55,6 +55,7 @@ public class InactiveCodeHighlightingTest extends TestCase {
super(name); super(name);
} }
@Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
fCProject= EditorTestHelper.createCProject(PROJECT, LINKED_FOLDER); fCProject= EditorTestHelper.createCProject(PROJECT, LINKED_FOLDER);
@ -64,6 +65,7 @@ public class InactiveCodeHighlightingTest extends TestCase {
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100)); assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
} }
@Override
protected void tearDown () throws Exception { protected void tearDown () throws Exception {
EditorTestHelper.closeEditor(fEditor); EditorTestHelper.closeEditor(fEditor);
@ -95,13 +97,13 @@ public class InactiveCodeHighlightingTest extends TestCase {
} }
String toString(Position[] positions) throws BadLocationException { String toString(Position[] positions) throws BadLocationException {
StringBuffer buf= new StringBuffer(); StringBuilder buf= new StringBuilder();
IDocument document= fSourceViewer.getDocument(); IDocument document= fSourceViewer.getDocument();
buf.append("Position[] expected= new Position[] {\n"); buf.append("Position[] expected= new Position[] {\n");
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()); int endLine= document.getLineOfOffset(position.getOffset()+position.getLength()-1);
buf.append("\tcreatePosition(" + startLine + ", " + endLine + "),\n"); buf.append("\tcreatePosition(" + startLine + ", " + endLine + "),\n");
} }
buf.append("};\n"); buf.append("};\n");
@ -111,8 +113,8 @@ public class InactiveCodeHighlightingTest extends TestCase {
protected Position[] getInactiveCodePositions() { protected Position[] getInactiveCodePositions() {
CSourceViewerDecorationSupport support= (CSourceViewerDecorationSupport) new Accessor(fEditor, AbstractDecoratedTextEditor.class).get("fSourceViewerDecorationSupport"); CSourceViewerDecorationSupport support= (CSourceViewerDecorationSupport) new Accessor(fEditor, AbstractDecoratedTextEditor.class).get("fSourceViewerDecorationSupport");
InactiveCodeHighlighting highlighting= (InactiveCodeHighlighting) new Accessor(support, support.getClass()).get("fInactiveCodeHighlighting"); InactiveCodeHighlighting highlighting= (InactiveCodeHighlighting) new Accessor(support, support.getClass()).get("fInactiveCodeHighlighting");
List positions= (List) new Accessor(highlighting, highlighting.getClass()).get("fInactiveCodePositions"); List<Position> positions= (List<Position>) new Accessor(highlighting, highlighting.getClass()).get("fInactiveCodePositions");
return (Position[]) positions.toArray(new Position[positions.size()]); return positions.toArray(new Position[positions.size()]);
} }
public void testInactiveCodePositions() throws BadLocationException { public void testInactiveCodePositions() throws BadLocationException {
@ -125,8 +127,9 @@ public class InactiveCodeHighlightingTest extends TestCase {
createPosition(39, 41), createPosition(39, 41),
createPosition(47, 57), createPosition(47, 57),
createPosition(67, 69), createPosition(67, 69),
createPosition(73, 75),
}; };
if (false) System.out.println(toString(actual)); assertEquals(toString(expected), toString(actual));
assertEqualPositions(expected, actual); assertEqualPositions(expected, actual);
} }

View file

@ -297,7 +297,9 @@ public class InactiveCodeHighlighting implements ICReconcilingListener, ITextInp
} }
} }
if (inInactiveCode) { if (inInactiveCode) {
// handle dangling #if? // handle unterminated #if - http://bugs.eclipse.org/255018
int inactiveCodeEnd = fDocument.getLength();
positions.add(createHighlightPosition(inactiveCodeStart, inactiveCodeEnd, true, fHighlightKey));
} }
return positions; return positions;
} }

View file

@ -1423,6 +1423,14 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
} }
} }
if (!branchStack.isEmpty()) {
// unterminated #if
Branch branch= branchStack.pop();
branch.setEndOffset(getDocument().getLength());
branch.setInclusive(true);
branches.add(branch);
}
Map<String, Counter> keys= new HashMap<String, Counter>(branches.size()); Map<String, Counter> keys= new HashMap<String, Counter>(branches.size());
for (Branch branch : branches) { for (Branch branch : branches) {
IRegion aligned = alignRegion(branch, ctx, branch.fInclusive); IRegion aligned = alignRegion(branch, ctx, branch.fInclusive);