Index: Scribe.java
===================================================================
--- Scribe.java	(.../trunk/eclipseformatter/Scribe.java)	(revision 37)
+++ Scribe.java	(.../branches/ef-changed/eclipseformatter/Scribe.java)	(revision 37)
@@ -1,3 +1,10 @@
+/*
+ * This file has been modified to allow the code
+ * formatter to insert curly braces {}.
+ * See http://kruithof.xs4all.nl//eclipseform/eclipsewithbraces.html
+ * for more info.
+ */
+
 /*******************************************************************************
  * Copyright (c) 2000, 2005 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
@@ -75,6 +82,10 @@
 	public int numberOfIndentations;
 	private boolean useTabsOnlyForLeadingIndents;
 
+	/* martijn kruithof: used for adding {} */
+	private int addBraceDepth = 0;
+
+
 	Scribe(CodeFormatterVisitor formatter, Map settings, int offset, int length, CodeSnippetParsingUtil codeSnippetParsingUtil) {
 		if (settings != null) {
 			Object sourceLevelOption = settings.get(JavaCore.COMPILER_SOURCE);
@@ -130,7 +141,17 @@
 		addOptimizedReplaceEdit(insertPosition, 0, insertedString);
 	}
 
+	    
+    private final void mandatoryInsertEdit(int insertPosition, String insertedString) {
+        if (this.edits.length == this.editsIndex) {
+            // resize
+            resize();
+        }
+        this.edits[this.editsIndex++] = new OptimizedReplaceEdit(insertPosition, 0, insertedString, false);
+    }
+
 	private final void addOptimizedReplaceEdit(int offset, int length, String replacement) {
+		final OptimizedReplaceEdit next = new OptimizedReplaceEdit(offset, length, replacement);
 		if (this.editsIndex > 0) {
 			// try to merge last two edits
 			final OptimizedReplaceEdit previous = this.edits[this.editsIndex-1];
@@ -151,7 +172,7 @@
 				this.editsIndex--;
 				return;
 			}
-			if (endOffsetOfPreviousEdit == offset) {
+			if (endOffsetOfPreviousEdit == offset && previous.mergeable) {
 				if (length != 0) {
 					if (replacementLength != 0) {
 						this.edits[this.editsIndex - 1] = new OptimizedReplaceEdit(previousOffset, previousLength + length, previousReplacement + replacement);
@@ -184,10 +205,10 @@
 					}
 				}
 			} else {
-				this.edits[this.editsIndex++] = new OptimizedReplaceEdit(offset, length, replacement);
+				this.edits[this.editsIndex++] = next;
 			}
 		} else {
-			this.edits[this.editsIndex++] = new OptimizedReplaceEdit(offset, length, replacement);
+			this.edits[this.editsIndex++] = next;
 		}
 	}
 	
@@ -1612,4 +1633,76 @@
 		this.indentationLevel -= this.indentationSize;
 		this.numberOfIndentations--;
 	}
-}
\ No newline at end of file
+    
+	/** 
+	 * Martijn Kruithof: needed to insert {} at if.
+	 *
+	 */
+
+    public void insertLBrace(boolean considerSpaceIfAny){
+//        if (checkLineWrapping && column + 1 > this.pageWidth) {
+//            handleLineTooLong();
+//        }
+        this.lastNumberOfNewLines = 0;
+        printIndentationIfNecessary();
+        if (considerSpaceIfAny) {
+            this.space();
+        }
+        int pos = this.scanner.getCurrentTokenStartPosition();
+        if (pos >= this.textRegionStart && pos <= this.textRegionEnd)
+        {
+            if (this.pendingSpace)
+            {
+                this.mandatoryInsertEdit(this.scanner.getCurrentTokenStartPosition(),
+                        " {"); //$NON-NLS-1$
+            }
+            else
+            {
+                this.mandatoryInsertEdit(this.scanner.getCurrentTokenStartPosition(),
+                        "{"); //$NON-NLS-1$
+            }
+            this.pendingSpace = false;
+            this.needSpace = false;
+            column += 1;
+            this.addBraceDepth++;
+        }
+
+    }
+    
+    public void insertRBrace(boolean considerSpaceIfAny){
+//      if (checkLineWrapping && column + 1 > this.pageWidth) {
+//          handleLineTooLong();
+//      }
+        this.lastNumberOfNewLines = 0;
+        printIndentationIfNecessary();
+        if (considerSpaceIfAny) {
+            this.space();
+        }
+        Exception e = new Exception();
+        e.setStackTrace(new StackTraceElement[0]);
+//        Util.log(e,("Inserting } at " + this.scanner.getCurrentTokenEndPosition() + "\neofPos =" + this.scanner.eofPosition + "\nslen =" + this.scanner.source.length));
+        int pos = this.scanner.getCurrentTokenEndPosition() + 1;
+        if (this.addBraceDepth > 0)
+        {
+            if (pos > this.textRegionEnd)
+            {
+                this.textRegionEnd = pos;
+            }
+            if (this.pendingSpace)
+            {
+                this.mandatoryInsertEdit(
+                        this.scanner.getCurrentTokenEndPosition() + 1, " }"); //$NON-NLS-1$
+            }
+            else
+            {
+                this.mandatoryInsertEdit(
+                        this.scanner.getCurrentTokenEndPosition() + 1, "}"); //$NON-NLS-1$
+            }
+            this.pendingSpace = false;
+            this.needSpace = false;
+            column += 1;
+            this.addBraceDepth--;
+        }
+    }
+    
+}
