From 9da49555f6af5dda6a7ae333c2bcec1ab2321b16 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Wed, 12 Nov 2008 16:02:09 +0000 Subject: [PATCH] Bug 255018 - [editor] Highlight inactive code of unterminated #ifdef --- .../resources/folding/FoldingTest.cpp | 3 +++ .../resources/inactiveCode/InactiveCodeTest.c | 4 ++++ .../org/eclipse/cdt/ui/tests/text/FoldingTest.java | 1 + .../ui/tests/text/InactiveCodeHighlightingTest.java | 13 ++++++++----- .../ui/editor/InactiveCodeHighlighting.java | 4 +++- .../folding/DefaultCFoldingStructureProvider.java | 8 ++++++++ 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/resources/folding/FoldingTest.cpp b/core/org.eclipse.cdt.ui.tests/resources/folding/FoldingTest.cpp index fb254808f6f..1fcae93f79c 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/folding/FoldingTest.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/folding/FoldingTest.cpp @@ -126,3 +126,6 @@ void foo() { { } } +// http://bugs.eclipse.org/255018 +#if 0 +// #endif missing diff --git a/core/org.eclipse.cdt.ui.tests/resources/inactiveCode/InactiveCodeTest.c b/core/org.eclipse.cdt.ui.tests/resources/inactiveCode/InactiveCodeTest.c index 43a422f843b..dc085753487 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/inactiveCode/InactiveCodeTest.c +++ b/core/org.eclipse.cdt.ui.tests/resources/inactiveCode/InactiveCodeTest.c @@ -70,3 +70,7 @@ #endif #endif // unbalanced endif because of invalid ifdef above + +#if foo // unterminated #if - http://bugs.eclipse.org/255018 +// inactive code +// #endif diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FoldingTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FoldingTest.java index 04722fccfbb..876e3a5c4e1 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FoldingTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FoldingTest.java @@ -233,6 +233,7 @@ public class FoldingTest extends TestCase { createPosition(119, 127), createPosition(120, 122), createPosition(123, 126), + createPosition(129, 130), }; assertEquals(toString(expected), toString(actual)); assertEqualPositions(expected, actual); diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/InactiveCodeHighlightingTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/InactiveCodeHighlightingTest.java index 02711b99d2b..9daaaccad39 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/InactiveCodeHighlightingTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/InactiveCodeHighlightingTest.java @@ -55,6 +55,7 @@ public class InactiveCodeHighlightingTest extends TestCase { super(name); } + @Override protected void setUp() throws Exception { super.setUp(); fCProject= EditorTestHelper.createCProject(PROJECT, LINKED_FOLDER); @@ -64,6 +65,7 @@ public class InactiveCodeHighlightingTest extends TestCase { assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100)); } + @Override protected void tearDown () throws Exception { EditorTestHelper.closeEditor(fEditor); @@ -95,13 +97,13 @@ public class InactiveCodeHighlightingTest extends TestCase { } String toString(Position[] positions) throws BadLocationException { - StringBuffer buf= new StringBuffer(); + StringBuilder buf= new StringBuilder(); IDocument document= fSourceViewer.getDocument(); buf.append("Position[] expected= new Position[] {\n"); 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()); + int endLine= document.getLineOfOffset(position.getOffset()+position.getLength()-1); buf.append("\tcreatePosition(" + startLine + ", " + endLine + "),\n"); } buf.append("};\n"); @@ -111,8 +113,8 @@ public class InactiveCodeHighlightingTest extends TestCase { protected Position[] getInactiveCodePositions() { CSourceViewerDecorationSupport support= (CSourceViewerDecorationSupport) new Accessor(fEditor, AbstractDecoratedTextEditor.class).get("fSourceViewerDecorationSupport"); InactiveCodeHighlighting highlighting= (InactiveCodeHighlighting) new Accessor(support, support.getClass()).get("fInactiveCodeHighlighting"); - List positions= (List) new Accessor(highlighting, highlighting.getClass()).get("fInactiveCodePositions"); - return (Position[]) positions.toArray(new Position[positions.size()]); + List positions= (List) new Accessor(highlighting, highlighting.getClass()).get("fInactiveCodePositions"); + return positions.toArray(new Position[positions.size()]); } public void testInactiveCodePositions() throws BadLocationException { @@ -125,8 +127,9 @@ public class InactiveCodeHighlightingTest extends TestCase { createPosition(39, 41), createPosition(47, 57), createPosition(67, 69), + createPosition(73, 75), }; - if (false) System.out.println(toString(actual)); + assertEquals(toString(expected), toString(actual)); assertEqualPositions(expected, actual); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/InactiveCodeHighlighting.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/InactiveCodeHighlighting.java index 6a86cb25ca7..46f7c1e5135 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/InactiveCodeHighlighting.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/InactiveCodeHighlighting.java @@ -297,7 +297,9 @@ public class InactiveCodeHighlighting implements ICReconcilingListener, ITextInp } } 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; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/folding/DefaultCFoldingStructureProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/folding/DefaultCFoldingStructureProvider.java index fd5f4b38f8d..d153ac6866d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/folding/DefaultCFoldingStructureProvider.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/folding/DefaultCFoldingStructureProvider.java @@ -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 keys= new HashMap(branches.size()); for (Branch branch : branches) { IRegion aligned = alignRegion(branch, ctx, branch.fInclusive);