Avoid double post single line comments
diff --git a/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextAbstractCommentSegmentSupport.java b/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextAbstractCommentSegmentSupport.java
index d1b5743..d60f379 100644
--- a/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextAbstractCommentSegmentSupport.java
+++ b/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextAbstractCommentSegmentSupport.java
@@ -10,6 +10,9 @@
*******************************************************************************/
package org.eclipse.ocl.examples.xtext.idioms.serializer;
+import java.util.ArrayList;
+import java.util.List;
+
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
@@ -22,6 +25,7 @@
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.impl.HiddenLeafNode;
+import org.eclipse.xtext.nodemodel.impl.RootNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.util.Strings;
@@ -253,6 +257,77 @@
return null;
}
+ protected @Nullable List<@NonNull Comment> getPostComments(@NonNull INode node) {
+ // showSiblings("Post", node);
+ String text = node.getText();
+ List<@NonNull Comment> comments = null;
+ for (INode nextNode = node; (nextNode = getNextLeaf(nextNode)) instanceof HiddenLeafNode; ) {
+ HiddenLeafNode leafNode = (HiddenLeafNode) nextNode;
+ String leafText = leafNode.getText();
+ Comment comment = null;
+ if (isSingleLineComment(leafNode)) {
+ comment = createSingleLineComment(leafNode);
+ if (comments == null) {
+ comments = new ArrayList<>();
+ }
+ comments.add(comment);
+ if (leafText.contains("\n")) { // New line separates post/pre comments unless comments on outer root
+ break;
+ }
+ }
+ else if (leafText.contains("\n")) { // New line separates post/pre comments unless comments on outer root
+ if (!(node.getParent() instanceof RootNode)) {
+ // break;
+ }
+ INode tailNode = nextNode;
+ for (; (tailNode = getNextLeaf(tailNode)) instanceof HiddenLeafNode; ) {}
+ if (tailNode != null) {
+ break;
+ }
+ }
+ }
+ return comments;
+ }
+
+ protected @Nullable List<@NonNull Comment> getPreComments(@NonNull INode node) {
+// showSiblings("Pre", node);
+ String text = node.getText();
+ List<@NonNull Comment> comments = null;
+ for (INode prevNode = node; (prevNode = prevNode.getPreviousSibling()) instanceof HiddenLeafNode; ) {
+ HiddenLeafNode leafNode = (HiddenLeafNode) prevNode;
+ String leafText = leafNode.getText();
+ Comment comment = null;
+ if (isMultipleLineComment(leafNode)) {
+ comment = createMultipleLineComment(leafNode);
+ }
+ else if (isSingleLineComment(leafNode)) {
+ if (leafNode.getOffset() <= 0) {
+ comment = createSingleLineComment(leafNode);
+ }
+ else {
+ for (INode prevPrevNode = prevNode; (prevPrevNode = prevPrevNode.getPreviousSibling()) instanceof HiddenLeafNode; ) {
+ String prevText = prevPrevNode.getText();
+ HiddenLeafNode prevLeafNode = (HiddenLeafNode) prevPrevNode;
+ if (isMultipleLineComment(prevLeafNode)) {
+ break;
+ }
+ if (endsinNewLine(prevLeafNode)) {
+ comment = createSingleLineComment(leafNode);
+ break;
+ }
+ }
+ }
+ }
+ if (comment != null) {
+ if (comments == null) {
+ comments = new ArrayList<>();
+ }
+ comments.add(0, comment);
+ }
+ }
+ return comments;
+ }
+
protected void showSiblings(String string, @NonNull ICompositeNode node) {
StringBuilder s = new StringBuilder();
s.append(string);
diff --git a/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextPostCommentSegmentSupport.java b/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextPostCommentSegmentSupport.java
index 9db1947..5ec9a48 100644
--- a/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextPostCommentSegmentSupport.java
+++ b/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextPostCommentSegmentSupport.java
@@ -10,16 +10,10 @@
*******************************************************************************/
package org.eclipse.ocl.examples.xtext.idioms.serializer;
-import java.util.ArrayList;
-import java.util.List;
-
import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.examples.xtext.serializer.SerializationBuilder;
import org.eclipse.ocl.examples.xtext.serializer.UserElementSerializer;
import org.eclipse.xtext.nodemodel.INode;
-import org.eclipse.xtext.nodemodel.impl.HiddenLeafNode;
-import org.eclipse.xtext.nodemodel.impl.RootNode;
/**
* XtextPostCommentSegmentSupport provides support for serializing and formatting typical
@@ -27,37 +21,6 @@
*/
public class XtextPostCommentSegmentSupport extends XtextAbstractCommentSegmentSupport
{
- protected @Nullable List<@NonNull Comment> getPostComments(@NonNull INode node) {
- // showSiblings("Post", node);
- String text = node.getText();
- List<@NonNull Comment> comments = null;
- for (INode nextNode = node; (nextNode = getNextLeaf(nextNode)) instanceof HiddenLeafNode; ) {
- HiddenLeafNode leafNode = (HiddenLeafNode) nextNode;
- String leafText = leafNode.getText();
- Comment comment = null;
- if (isSingleLineComment(leafNode)) {
- comment = createSingleLineComment(leafNode);
- }
- else if (leafText.contains("\n")) { // New line separates post/pre comments unless comments on outer root
- if (!(node.getParent() instanceof RootNode)) {
- // break;
- }
- INode tailNode = nextNode;
- for (; (tailNode = getNextLeaf(tailNode)) instanceof HiddenLeafNode; ) {}
- if (tailNode != null) {
- break;
- }
- }
- if (comment != null) {
- if (comments == null) {
- comments = new ArrayList<>();
- }
- comments.add(comment);
- }
- }
- return comments;
- }
-
/* @Override
public void format(@NonNull SerializationStep serializationStep, @NonNull UserElementFormatter fomatter, @NonNull SerializationBuilder serializationBuilder) {
EObject eObject = fomatter.getElement();
diff --git a/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextPreCommentSegmentSupport.java b/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextPreCommentSegmentSupport.java
index d17c362..68e7bd5 100644
--- a/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextPreCommentSegmentSupport.java
+++ b/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/XtextPreCommentSegmentSupport.java
@@ -10,17 +10,12 @@
*******************************************************************************/
package org.eclipse.ocl.examples.xtext.idioms.serializer;
-import java.util.ArrayList;
-import java.util.List;
-
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.examples.xtext.serializer.SerializationBuilder;
import org.eclipse.ocl.examples.xtext.serializer.SerializationStep;
import org.eclipse.ocl.examples.xtext.serializer.UserElementSerializer;
import org.eclipse.xtext.nodemodel.INode;
-import org.eclipse.xtext.nodemodel.impl.HiddenLeafNode;
/**
* XtextPreCommentSegmentSupport provides support for serializing and formatting typical
@@ -28,45 +23,6 @@
*/
public class XtextPreCommentSegmentSupport extends XtextAbstractCommentSegmentSupport
{
- protected @Nullable List<@NonNull Comment> getPreComments(@NonNull INode node) {
-// showSiblings("Pre", node);
- String text = node.getText();
- List<@NonNull Comment> comments = null;
- for (INode prevNode = node; (prevNode = prevNode.getPreviousSibling()) instanceof HiddenLeafNode; ) {
- HiddenLeafNode leafNode = (HiddenLeafNode) prevNode;
- String leafText = leafNode.getText();
- Comment comment = null;
- if (isMultipleLineComment(leafNode)) {
- comment = createMultipleLineComment(leafNode);
- }
- else if (isSingleLineComment(leafNode)) {
- if (leafNode.getOffset() <= 0) {
- comment = createSingleLineComment(leafNode);
- }
- else {
- for (INode prevPrevNode = prevNode; (prevPrevNode = prevPrevNode.getPreviousSibling()) instanceof HiddenLeafNode; ) {
- String prevText = prevPrevNode.getText();
- HiddenLeafNode prevLeafNode = (HiddenLeafNode) prevPrevNode;
- if (isMultipleLineComment(prevLeafNode)) {
- break;
- }
- if (endsinNewLine(prevLeafNode)) {
- comment = createSingleLineComment(leafNode);
- break;
- }
- }
- }
- }
- if (comment != null) {
- if (comments == null) {
- comments = new ArrayList<>();
- }
- comments.add(0, comment);
- }
- }
- return comments;
- }
-
/* @Override
public void format(@NonNull SerializationStep serializationStep, @NonNull UserElementFormatter fomatter, @NonNull SerializationBuilder serializationBuilder) {
EObject eObject = fomatter.getElement();
@@ -98,15 +54,4 @@
}
}
}
-/* private void traverseSiblings(@NonNull INode parent, @NonNull String position) {
- System.out.println(position + " " + parent.getOffset() + ":" + parent.getLength() + " " + parent.getClass().getSimpleName() + " " + parent.getGrammarElement().getClass().getSimpleName() + " - \"" + parent.getText().replace("\n", "\\n") + "\"");
- if (parent instanceof ICompositeNode) {
- int width = 0;
- for (INode child : ((ICompositeNode)parent).getChildren()) {
- assert child != null;
- String nestedPosition = position+"."+width++;
- traverseSiblings(child, nestedPosition);
- }
- }
- } */
}
diff --git a/tests/org.eclipse.ocl.examples.xtext.tests/models/idioms/DebugTest.idioms b/tests/org.eclipse.ocl.examples.xtext.tests/models/idioms/DebugTest.idioms
index d58e2b4..cc8853d 100644
--- a/tests/org.eclipse.ocl.examples.xtext.tests/models/idioms/DebugTest.idioms
+++ b/tests/org.eclipse.ocl.examples.xtext.tests/models/idioms/DebugTest.idioms
@@ -1,9 +1,3 @@
-/* head */
-model DebugTest
-
+model DebugTest // Suffix single line
// prefix
-idiom FINAL at final // final
-do // do
-soft-space value // value
-soft-space;
-// tail
+idiom FINAL at final do soft-space value soft-space;
diff --git a/tests/org.eclipse.ocl.examples.xtext.tests/models/idioms/Test.idioms b/tests/org.eclipse.ocl.examples.xtext.tests/models/idioms/Test.idioms
index cd22222..17e0e69 100644
--- a/tests/org.eclipse.ocl.examples.xtext.tests/models/idioms/Test.idioms
+++ b/tests/org.eclipse.ocl.examples.xtext.tests/models/idioms/Test.idioms
@@ -31,4 +31,9 @@
idiom SUBIDIOMS_SPACING at assignment idioms::Idiom::ownedSubIdioms do value soft-new-line;
/** idiom imposing default spacing for leaf terms must be last */
-idiom FINAL at final do soft-space value soft-space;
+// prefix
+idiom FINAL at final // final
+do // do
+soft-space value // value
+soft-space;
+// tail