Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Stornelli2019-04-12 12:36:10 -0400
committerJonah Graham2019-04-13 14:28:30 -0400
commitef2468b390ffbd3950607116d09db504f014ea86 (patch)
treed60946f4566cbd5ba481c07a51b12faf024c4464
parent50daf5b6e84329a8f395612f74f55557ef4e1757 (diff)
downloadorg.eclipse.cdt-ef2468b390ffbd3950607116d09db504f014ea86.tar.gz
org.eclipse.cdt-ef2468b390ffbd3950607116d09db504f014ea86.tar.xz
org.eclipse.cdt-ef2468b390ffbd3950607116d09db504f014ea86.zip
Bug 546391 - Fix on/off tags
Change-Id: If23ac8f7777c5088b892a31a543741c244dd1a4a Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java24
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/InactivePosition.java47
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java16
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java65
4 files changed, 133 insertions, 19 deletions
diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
index e71e9c5a82..57cbe88923 100644
--- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
+++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
@@ -439,7 +439,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
localScanner.setSource(compilationUnitSource);
scribe.initializeScanner(compilationUnitSource);
- List<Position> inactive = collectInactiveCodePositions(unit);
+ List<InactivePosition> inactive = collectInactiveCodePositions(unit);
inactive.addAll(collectNoFormatCodePositions(unit));
scribe.setSkipInactivePositions(inactive);
@@ -4530,7 +4530,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
* @param translationUnit the {@link IASTTranslationUnit}, may be <code>null</code>
* @return a {@link List} of {@link Position}s
*/
- private List<Position> collectNoFormatCodePositions(IASTTranslationUnit translationUnit) {
+ private List<InactivePosition> collectNoFormatCodePositions(IASTTranslationUnit translationUnit) {
if (translationUnit == null || !this.preferences.use_fomatter_comment_tag) {
return Collections.emptyList();
}
@@ -4538,7 +4538,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (fileName == null) {
return Collections.emptyList();
}
- List<Position> positions = new ArrayList<>();
+ List<InactivePosition> positions = new ArrayList<>();
int inactiveCodeStart = -1;
boolean inInactiveCode = false;
@@ -4569,14 +4569,15 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
} else if (onPos != -1 && onPos > offPos) {
if (inInactiveCode) {
- int inactiveCodeEnd = nodeLocation.getNodeOffset();
- positions.add(new Position(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
+ int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
+ positions.add(new InactivePosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, false));
}
inInactiveCode = false;
}
}
if (inInactiveCode) {
- positions.add(new Position(inactiveCodeStart, translationUnit.getFileLocation().getNodeLength()));
+ positions.add(
+ new InactivePosition(inactiveCodeStart, translationUnit.getFileLocation().getNodeLength(), false));
inInactiveCode = false;
}
return positions;
@@ -4589,7 +4590,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
* @param translationUnit the {@link IASTTranslationUnit}, may be <code>null</code>
* @return a {@link List} of {@link Position}s
*/
- private static List<Position> collectInactiveCodePositions(IASTTranslationUnit translationUnit) {
+ private static List<InactivePosition> collectInactiveCodePositions(IASTTranslationUnit translationUnit) {
if (translationUnit == null) {
return Collections.emptyList();
}
@@ -4597,7 +4598,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (fileName == null) {
return Collections.emptyList();
}
- List<Position> positions = new ArrayList<>();
+ List<InactivePosition> positions = new ArrayList<>();
int inactiveCodeStart = -1;
boolean inInactiveCode = false;
Stack<Boolean> inactiveCodeStack = new Stack<>();
@@ -4648,7 +4649,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
inInactiveCode = true;
} else if (elseStmt.taken() && inInactiveCode) {
int inactiveCodeEnd = nodeLocation.getNodeOffset();
- positions.add(new Position(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
+ positions.add(new InactivePosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, true));
inInactiveCode = false;
}
} else if (statement instanceof IASTPreprocessorElifStatement) {
@@ -4658,7 +4659,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
inInactiveCode = true;
} else if (elifStmt.taken() && inInactiveCode) {
int inactiveCodeEnd = nodeLocation.getNodeOffset();
- positions.add(new Position(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
+ positions.add(new InactivePosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, true));
inInactiveCode = false;
}
} else if (statement instanceof IASTPreprocessorEndifStatement) {
@@ -4666,7 +4667,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
boolean wasInInactiveCode = inactiveCodeStack.pop().booleanValue();
if (inInactiveCode && !wasInInactiveCode) {
int inactiveCodeEnd = nodeLocation.getNodeOffset();
- positions.add(new Position(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
+ positions.add(
+ new InactivePosition(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart, true));
}
inInactiveCode = wasInInactiveCode;
} catch (EmptyStackException e) {
diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/InactivePosition.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/InactivePosition.java
new file mode 100644
index 0000000000..0edc4fde7b
--- /dev/null
+++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/InactivePosition.java
@@ -0,0 +1,47 @@
+package org.eclipse.cdt.internal.formatter;
+
+import org.eclipse.jface.text.Position;
+
+/*******************************************************************************
+ * Copyright (c) 2019 Marco Stornelli
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ *******************************************************************************/
+public class InactivePosition extends Position {
+
+ private boolean preprocessorRegion;
+
+ public boolean isPreprocessorRegion() {
+ return preprocessorRegion;
+ }
+
+ /**
+ * Creates a new position with the given offset and length 0.
+ *
+ * @param offset the position offset, must be &gt;= 0
+ * @param preprocessor True if the region is a preprocessor region, false otherwise
+ */
+ public InactivePosition(int offset, boolean preprocessor) {
+ super(offset, 0);
+ preprocessorRegion = preprocessor;
+ }
+
+ /**
+ * Creates a new position with the given offset and length.
+ *
+ * @param offset the position offset, must be &gt;= 0
+ * @param length the position length, must be &gt;= 0
+ * @param preprocessor True if the region is a preprocessor region, false otherwise
+ */
+ public InactivePosition(int offset, int length, boolean preprocessor) {
+ super(offset, length);
+ preprocessorRegion = preprocessor;
+ }
+
+}
diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java
index 62b8da5354..8bba672f41 100644
--- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java
+++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java
@@ -27,7 +27,6 @@ import org.eclipse.cdt.internal.formatter.align.Alignment;
import org.eclipse.cdt.internal.formatter.align.AlignmentException;
import org.eclipse.cdt.internal.formatter.scanner.Scanner;
import org.eclipse.cdt.internal.formatter.scanner.Token;
-import org.eclipse.jface.text.Position;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
@@ -91,7 +90,7 @@ public class Scribe {
/**
* It keeps the list of inactive region.
*/
- private List<Position> fSkipInactivePositions = Collections.emptyList();
+ private List<InactivePosition> fSkipInactivePositions = Collections.emptyList();
/**
* When scribe is in inactive state, source edits are not allowed.
*/
@@ -679,7 +678,7 @@ public class Scribe {
/**
* @param list
*/
- public void setSkipInactivePositions(List<Position> list) {
+ public void setSkipInactivePositions(List<InactivePosition> list) {
fSkipInactivePositions = list;
skipOverInactive = !list.isEmpty();
}
@@ -1086,7 +1085,7 @@ public class Scribe {
int lines = 0;
while ((currentToken = scanner.nextToken()) != null) {
if (skipOverInactive) {
- Position inactivePos = getInactivePosAt(scanner.getCurrentTokenStartPosition());
+ InactivePosition inactivePos = getInactivePosAt(scanner.getCurrentTokenStartPosition());
if (inactivePos != null) {
int startOffset = Math.min(scanner.getCurrentTokenStartPosition(), inactivePos.getOffset());
int endOffset = Math.min(scannerEndPosition, inactivePos.getOffset() + inactivePos.getLength());
@@ -1098,7 +1097,8 @@ public class Scribe {
* We are entering in inactive state so if we added a new line previously,
* starting a new line, we need to remove it.
*/
- if (editsIndex > 0 && lineSeparator.equals(edits[editsIndex - 1].replacement)) {
+ if (inactivePos.isPreprocessorRegion() && editsIndex > 0
+ && lineSeparator.equals(edits[editsIndex - 1].replacement)) {
editsIndex--;
}
printRaw(startOffset, endOffset - startOffset);
@@ -1311,9 +1311,9 @@ public class Scribe {
* @param offset
* @return
*/
- private Position getInactivePosAt(int offset) {
- for (Iterator<Position> iter = fSkipInactivePositions.iterator(); iter.hasNext();) {
- Position pos = iter.next();
+ private InactivePosition getInactivePosAt(int offset) {
+ for (Iterator<InactivePosition> iter = fSkipInactivePositions.iterator(); iter.hasNext();) {
+ InactivePosition pos = iter.next();
if (pos.includes(offset)) {
return pos;
}
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java
index 021b5a813e..e73ffa8a74 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java
@@ -4018,4 +4018,69 @@ public class CodeFormatterTest extends BaseUITestCase {
public void testBoolInStructWithMacro_Bug397710() throws Exception {
assertFormatterResult();
}
+
+ ///* @formatter:off */
+ //int xx2(){string s1="abc";string s2="def";}
+ ///* @formatter:on */
+ //
+ ///* @formatter:on */
+ //int xx(){string s1="abc";string s2="def";}
+ ///* @formatter:off */
+ ///* @formatter:on */
+ //int xx3(){string s1="abc";string s2="def";}
+
+ ///* @formatter:off */
+ //int xx2(){string s1="abc";string s2="def";}
+ ///* @formatter:on */
+ //
+ ///* @formatter:on */
+ //int xx() {
+ // string s1 = "abc";
+ // string s2 = "def";
+ //}
+ ///* @formatter:off */
+ ///* @formatter:on */
+ //int xx3() {
+ // string s1 = "abc";
+ // string s2 = "def";
+ //}
+ public void testOnOffTags1_Bug546391() throws Exception {
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_USE_COMMENT_TAG, true);
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_ON_TAG, "@formatter:on");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_OFF_TAG, "@formatter:off");
+ assertFormatterResult();
+ }
+
+ //int xx(){string s1="abc";string s2="def";}
+ ///* @formatter:off */
+
+ //int xx() {
+ // string s1 = "abc";
+ // string s2 = "def";
+ //}
+ ///* @formatter:off */
+ public void testOnOffTags2_Bug546391() throws Exception {
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_USE_COMMENT_TAG, true);
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_ON_TAG, "@formatter:on");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_OFF_TAG, "@formatter:off");
+ assertFormatterResult();
+ }
+
+ ///* @formatter:off */
+ //int xx(){string s1="abc";string s2="def";}
+ ///* @formatter:on
+ // *
+ // */
+
+ ///* @formatter:off */
+ //int xx(){string s1="abc";string s2="def";}
+ ///* @formatter:on
+ // *
+ // */
+ public void testOnOffTags3_Bug546391() throws Exception {
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_USE_COMMENT_TAG, true);
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_ON_TAG, "@formatter:on");
+ fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_OFF_TAG, "@formatter:off");
+ assertFormatterResult();
+ }
}

Back to the top