Index: /trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java
===================================================================
--- /trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java	(revision 30)
+++ /trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java	(revision 31)
@@ -8,5 +8,4 @@
 import org.eclipse.xtext.parsetree.CompositeNode;
 import org.eclipse.xtext.parsetree.LeafNode;
-import org.eclipse.xtext.parsetree.NodeAdapter;
 import org.eclipse.xtext.parsetree.NodeUtil;
 
@@ -181,11 +180,4 @@
 	
 	private void weaveComments() {
-//		if (true) {
-//			StringBuffer result = new StringBuffer();
-//			result.append(buf.toString());
-//			buf = result;
-//			return;
-//		}
-		
 		bufferPositionOriginalText = 0;
 		bufferPositionFormattedTextNoWhitespaces = 0;
@@ -248,4 +240,5 @@
 					}
 				} else { // disaster handling: return original unformatted text!
+					System.err.println("Disaster Recovery: returning original text!!");
 					buf = new StringBuffer();
 					buf.append(originalText);
@@ -287,4 +280,21 @@
 	}
 
+	private boolean lastIsClosingParentheses() {
+		char ch = buf.toString().charAt(buf.toString().length()-1);
+		if ((ch == ')') || (ch == ']') || (ch == '}'))
+			return true;
+		return false;
+	}
+
+	private void wrap() {
+		if ((config.isWrapAfterThreshold()) && (newLineOffsetCounter > config.getWrapThreshold())) {
+			newLine();
+			if (ruleSpacingStack.size() > 1)
+				spaces(ruleSpacingStack.peek() + 1);
+			else
+				spaces(ruleSpacingStack.peek());
+		}
+	}
+
 	// -----------------------------------------------------------------------------
 
@@ -372,14 +382,4 @@
 	}
 
-	protected void visitBefore(GroupedSequence node) {
-		text("(");
-		ruleSpacingStack.push(newLineOffsetCounter);
-	}
-
-	protected void visitAfter(GroupedSequence node) {
-		text(")");
-		ruleSpacingStack.pop();
-	}
-
 	protected void visitBefore(HookCombinator node) {
 	}
@@ -402,10 +402,34 @@
 	}
 
+	protected void visitBefore(GroupedSequence node) {
+		wrap();
+		text("(");
+		ruleSpacingStack.push(newLineOffsetCounter-1);
+	}
+
+	protected void visitAfter(GroupedSequence node) {
+//		if ((config.isAlignParentheses() && (node.eContents().get(0).eContents().size() >= config.getAlignParenthesesElementCountThreshold())) || (lastIsClosingParentheses())) {
+		if (config.isAlignParentheses() && (node.eContents().get(0).eContents().size() >= config.getAlignParenthesesElementCountThreshold())) {
+			newLine();
+			spaces(ruleSpacingStack.peek());
+		}
+		
+		text(")");
+		ruleSpacingStack.pop();
+	}
+
 	protected void visitBefore(OptionalSequence node) {
+		wrap();
 		text("[");
-		ruleSpacingStack.push(newLineOffsetCounter);
+		ruleSpacingStack.push(newLineOffsetCounter-1);
 	}
 
 	protected void visitAfter(OptionalSequence node) {
+//		if ((config.isAlignParentheses() && (node.eContents().get(0).eContents().size() >= config.getAlignParenthesesElementCountThreshold())) || (lastIsClosingParentheses())) {
+		if (config.isAlignParentheses() && (node.eContents().get(0).eContents().size() >= config.getAlignParenthesesElementCountThreshold())) {
+			newLine();
+			spaces(ruleSpacingStack.peek());
+		}
+
 		text("]");
 		ruleSpacingStack.pop();
@@ -413,9 +437,16 @@
 
 	protected void visitBefore(RepeatedSequence node) {
+		wrap();
 		text("{");
-		ruleSpacingStack.push(newLineOffsetCounter);
+		ruleSpacingStack.push(newLineOffsetCounter-1);
 	}
 
 	protected void visitAfter(RepeatedSequence node) {
+//		if ((config.isAlignParentheses() && (node.eContents().get(0).eContents().size() >= config.getAlignParenthesesElementCountThreshold())) || (lastIsClosingParentheses())) {
+		if (config.isAlignParentheses() && (node.eContents().get(0).eContents().size() >= config.getAlignParenthesesElementCountThreshold())) {
+			newLine();
+			spaces(ruleSpacingStack.peek());
+		}
+
 		text("}");
 		if (node.isMorethanonce())
@@ -454,4 +485,5 @@
 
 	protected void visitBefore(RuleReference node) {
+		wrap();
 		text(node.getRuleref().getName());
 	}
@@ -482,13 +514,18 @@
 				if (config.isPreventNewLineAfterAlternativeOnLessThanThreeElements()) {
 					DefinitionList definitionList = (DefinitionList) node.eContainer();
-					
 					if ((definitionList.eContents().size() > 2) && (!preventAlternativeBreakShortAlternatives)) {
 						newLine();
-						spaces(ruleSpacingStack.peek());
+						if (ruleSpacingStack.size() > 1)
+							spaces(ruleSpacingStack.peek() + 1);
+						else
+							spaces(ruleSpacingStack.peek());
 					}
 				} else {
 					if (!preventAlternativeBreakShortAlternatives) {
 						newLine();
-						spaces(ruleSpacingStack.peek());
+						if (ruleSpacingStack.size() > 1)
+							spaces(ruleSpacingStack.peek() + 1);
+						else
+							spaces(ruleSpacingStack.peek());
 					}
 				}
@@ -498,4 +535,5 @@
 
 	protected void visitBefore(StringRule node) {
+		wrap();
 		if (node.getLiteral() != null)
 			text("\"" + node.getLiteral() + "\"");
Index: /trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/FormatterConfig.java
===================================================================
--- /trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/FormatterConfig.java	(revision 30)
+++ /trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/FormatterConfig.java	(revision 31)
@@ -6,4 +6,8 @@
 	private boolean preventNewLineAfterAlternativeOnShortAlternatives = true;
 	private double shortAlternativeThreshold = 3.0;
+	private boolean alignParentheses = true;
+	private int alignParenthesesElementCountThreshold = 3;
+	private boolean wrapAfterThreshold = true;
+	private int wrapThreshold = 80;
 
 	public boolean isNewLineAfterAlternative() {
@@ -41,3 +45,36 @@
 	}
 
+	public boolean isAlignParentheses() {
+		return alignParentheses;
+	}
+
+	public void setAlignParentheses(boolean alignParentheses) {
+		this.alignParentheses = alignParentheses;
+	}
+
+	public int getAlignParenthesesElementCountThreshold() {
+		return alignParenthesesElementCountThreshold;
+	}
+
+	public void setAlignParenthesesElementCountThreshold(
+			int alignParenthesesElementCountThreshold) {
+		this.alignParenthesesElementCountThreshold = alignParenthesesElementCountThreshold;
+	}
+
+	public boolean isWrapAfterThreshold() {
+		return wrapAfterThreshold;
+	}
+
+	public void setWrapAfterThreshold(boolean wrapAfterThreshold) {
+		this.wrapAfterThreshold = wrapAfterThreshold;
+	}
+
+	public int getWrapThreshold() {
+		return wrapThreshold;
+	}
+
+	public void setWrapThreshold(int wrapThreshold) {
+		this.wrapThreshold = wrapThreshold;
+	}
+
 }
