From d643f4f0183b847683e5bec78d02c61f6fec8414 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Tue, 12 Sep 2006 07:06:04 +0000 Subject: [PATCH] Avoid overlapping folding positions for #if/#elif/#else --- .../cdt/ui/tests/text/FoldingTest.java | 23 +++++++++++-------- .../text/InactiveCodeHighlightingTest.java | 19 +++++++++------ .../ui/editor/InactiveCodeHighlighting.java | 4 ++-- .../DefaultCFoldingStructureProvider.java | 4 ++-- 4 files changed, 30 insertions(+), 20 deletions(-) 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 bdca78ff1d8..94ec7da93f5 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 @@ -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); } 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 3b5ca7c8bc1..02711b99d2b 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 @@ -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)); 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 9f4db33b83b..d8ce166b373 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 @@ -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; } 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 7d93f398e07..d09d469cfce 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 @@ -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) {