1) Fixed issue #215: Escaping Strings
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPUnitEditor.java
index 15c174f..d79be69 100644 (file)
@@ -26,7 +26,7 @@ import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference;
 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
 import net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner;
 import net.sourceforge.phpdt.internal.ui.text.JavaIndenter;
-import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
+//import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager;
 import net.sourceforge.phpdt.internal.ui.text.SmartSemicolonAutoEditStrategy;
 import net.sourceforge.phpdt.internal.ui.text.comment.CommentFormattingContext;
@@ -37,7 +37,7 @@ import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI.ExitFlags;
 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
 import net.sourceforge.phpdt.ui.PreferenceConstants;
 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
-import net.sourceforge.phpdt.ui.text.JavaTextTools;
+//import net.sourceforge.phpdt.ui.text.JavaTextTools;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.phpeditor.actions.RTrimAction;
 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
@@ -57,7 +57,7 @@ import org.eclipse.jface.dialogs.ErrorDialog;
 import org.eclipse.jface.dialogs.IMessageProvider;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.preference.IPreferenceStore;
-import org.eclipse.jface.preference.PreferenceConverter;
+//import org.eclipse.jface.preference.PreferenceConverter;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.DocumentCommand;
 import org.eclipse.jface.text.IAutoEditStrategy;
@@ -76,7 +76,9 @@ import org.eclipse.jface.text.source.IOverviewRuler;
 import org.eclipse.jface.text.source.ISourceViewer;
 import org.eclipse.jface.text.source.IVerticalRuler;
 import org.eclipse.jface.text.source.SourceViewerConfiguration;
-import org.eclipse.jface.util.ListenerList;
+//incastrix
+//import org.eclipse.jface.util.ListenerList;
+import org.eclipse.core.runtime.ListenerList;
 import org.eclipse.jface.util.PropertyChangeEvent;
 import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
@@ -84,7 +86,7 @@ import org.eclipse.swt.custom.VerifyKeyListener;
 import org.eclipse.swt.events.VerifyEvent;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.Point;
-import org.eclipse.swt.graphics.RGB;
+//import org.eclipse.swt.graphics.RGB;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
@@ -372,9 +374,9 @@ public class PHPUnitEditor extends PHPEditor { // implements
                protected void customizeDocumentCommand(DocumentCommand command) {
                        super.customizeDocumentCommand(command);
                        if (!fIgnoreTextConverters && fTextConverters != null) {
-                               for (Iterator e = fTextConverters.iterator(); e.hasNext();)
-                                       ((ITextConverter) e.next()).customizeDocumentCommand(
-                                                       getDocument(), command);
+                               for (Iterator e = fTextConverters.iterator(); e.hasNext();) {
+                                       ((ITextConverter) e.next()).customizeDocumentCommand (getDocument(), command);
+                               }
                        }
                }
 
@@ -414,6 +416,17 @@ public class PHPUnitEditor extends PHPEditor { // implements
                }
 
                /*
+                * Get the global 'Undo history size' setting 
+                *
+                */
+        protected int getUndoHistorySize () {
+                  IPreferenceStore store= getPreferenceStore ();
+                  
+                  return store != null ? store.getInt("undoHistorySize") : 1000;
+               }
+
+               
+               /*
                 * @see org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
                 */
                public void configure(SourceViewerConfiguration configuration) {
@@ -421,13 +434,13 @@ public class PHPUnitEditor extends PHPEditor { // implements
                        // fCorrectionAssistant = new
                        // JavaCorrectionAssistant(CompilationUnitEditor.this);
                        // fCorrectionAssistant.install(this);
-                       IAutoEditStrategy smartSemi = new SmartSemicolonAutoEditStrategy(
-                                       IPHPPartitions.PHP_PARTITIONING);
+                       IAutoEditStrategy smartSemi = new SmartSemicolonAutoEditStrategy (IPHPPartitions.PHP_PARTITIONING);
                        prependAutoEditStrategy(smartSemi, IDocument.DEFAULT_CONTENT_TYPE);
                        prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_DQ);
                        prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_SQ);
-                       prependAutoEditStrategy(smartSemi,
-                                       IPHPPartitions.PHP_STRING_HEREDOC);
+                       prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_HEREDOC);
+            
+                       fUndoManager.setMaximalUndoLevel (this.getUndoHistorySize ());  // Set every editor to the global 'Undo history size'
                }
 
                /*
@@ -959,18 +972,17 @@ public class PHPUnitEditor extends PHPEditor { // implements
                }
        }
 
-       private static class BracketLevel {
-               int fOffset;
-
-               int fLength;
-
-               LinkedPositionManager fManager;
-
-               LinkedPositionUI fEditor;
-       };
-
-       private class BracketInserter implements VerifyKeyListener,
-                       LinkedPositionUI.ExitListener {
+//     private static class BracketLevel {
+//             int fOffset;
+//
+//             int fLength;
+//
+//             LinkedPositionManager fManager;
+//
+//             LinkedPositionUI fEditor;
+//     };
+
+       private class BracketInserter implements VerifyKeyListener, LinkedPositionUI.ExitListener {
                private boolean fCloseBracketsPHP = true;
 
                private boolean fCloseStringsPHPDQ = true;
@@ -993,63 +1005,73 @@ public class PHPUnitEditor extends PHPEditor { // implements
                        fCloseStringsPHPSQ = enabled;
                }
 
-               private boolean hasIdentifierToTheRight(IDocument document, int offset) {
+               /**
+                * Check whether there is a possible identifier to the right 
+                * 
+                * @param document The source file which the user is currently modifying
+                * @param offset   The position we want to check whether there is a possible identifier to the right
+                * @return
+                */
+               private boolean hasIdentifierToTheRight (IDocument document, int offset) {
                        try {
-                               int end = offset;
-                               IRegion endLine = document.getLineInformationOfOffset(end);
-                               int maxEnd = endLine.getOffset() + endLine.getLength();
-                               while (end != maxEnd
-                                               && Character.isWhitespace(document.getChar(end)))
-                                       ++end;
-                               return end != maxEnd
-                                               && Scanner.isPHPIdentifierPart(document.getChar(end));
+                               int posToCheck = offset;
+                               IRegion endLine = document.getLineInformationOfOffset(posToCheck);
+                               int endOfLine = endLine.getOffset() + endLine.getLength();
+                               
+                               while ((posToCheck != endOfLine) &&                                                                             // As long as we are at the current line 
+                                          (Character.isWhitespace(document.getChar (posToCheck)))) {           // and the character is a white space
+                                       ++posToCheck;                                                                                                           // Go for the next position to check
+                               }
+                               
+                               return (posToCheck != endOfLine) &&                                                                             // If it not the end of line 
+                                               Scanner.isPHPIdentifierPart(document.getChar(posToCheck));              // and it could be a identifier
                        } catch (BadLocationException e) {
                                // be conservative
                                return true;
                        }
                }
 
-               private boolean hasIdentifierToTheLeft(IDocument document, int offset) {
-                       try {
-                               int start = offset;
-                               IRegion startLine = document.getLineInformationOfOffset(start);
-                               int minStart = startLine.getOffset();
-                               while (start != minStart
-                                               && Character.isWhitespace(document.getChar(start - 1)))
-                                       --start;
-                               return start != minStart
-                                               && Scanner.isPHPIdentifierPart(document
-                                                               .getChar(start - 1));
-                       } catch (BadLocationException e) {
-                               return true;
-                       }
-               }
-
-               private boolean hasCharacterToTheLeft(IDocument document, int offset,
-                               char character) {
+//             private boolean hasIdentifierToTheLeft(IDocument document, int offset) {
+//                     try {
+//                             int start = offset;
+//                             IRegion startLine = document.getLineInformationOfOffset(start);
+//                             int minStart = startLine.getOffset();
+//                             while (start != minStart
+//                                             && Character.isWhitespace(document.getChar(start - 1)))
+//                                     --start;
+//                             return start != minStart
+//                                             && Scanner.isPHPIdentifierPart(document.getChar(start - 1));
+//                     } catch (BadLocationException e) {
+//                             return true;
+//                     }
+//             }
+
+               private boolean hasCharacterToTheLeft (IDocument document, int offset, char character) {
                        try {
                                int start = offset;
                                IRegion startLine = document.getLineInformationOfOffset(start);
                                int minStart = startLine.getOffset();
-                               while (start != minStart
-                                               && Character.isWhitespace(document.getChar(start - 1)))
+                               
+                               while (start != minStart && Character.isWhitespace(document.getChar(start - 1))) {
                                        --start;
-                               return start != minStart
-                                               && document.getChar(start - 1) == character;
+                               }
+                               
+                               return start != minStart && document.getChar(start - 1) == character;
                        } catch (BadLocationException e) {
                                return false;
                        }
                }
 
-               private boolean hasCharacterToTheRight(IDocument document, int offset,
-                               char character) {
+               private boolean hasCharacterToTheRight (IDocument document, int offset, char character) {
                        try {
                                int end = offset;
                                IRegion endLine = document.getLineInformationOfOffset(end);
                                int maxEnd = endLine.getOffset() + endLine.getLength();
-                               while (end != maxEnd
-                                               && Character.isWhitespace(document.getChar(end)))
+                               
+                               while (end != maxEnd && Character.isWhitespace(document.getChar(end))) {
                                        ++end;
+                               }
+                               
                                return end != maxEnd && document.getChar(end) == character;
                        } catch (BadLocationException e) {
                                // be conservative
@@ -1057,170 +1079,216 @@ public class PHPUnitEditor extends PHPEditor { // implements
                        }
                }
 
+               private boolean hasEscapeCharToTheLeft (IDocument document, int offset) {
+                       try {
+                               int posToCheck      = offset;
+                               IRegion currentLine = document.getLineInformationOfOffset (posToCheck);
+                               int startOfLine     = currentLine.getOffset ();
+                               int numberOfEscapes = 0;
+                               
+                               posToCheck--;                                                                           // Set to the previous character
+                               
+                               while (posToCheck > startOfLine) {                                      // while we are within the current line
+                                       if (document.getChar (posToCheck) == '\\') {    // If the character is a escape char 
+                                               numberOfEscapes++;                                                      // Count it
+                                       }
+                                       else {                                                                                  // If we found the first non escape char
+                                               break;                                                                          // Leave counting
+                                       }
+                                       
+                                       posToCheck--;                                                                   // Go for the next previous character
+                               }
+                               
+                               if ((numberOfEscapes % 2) == 1) {                                       // If we have a odd number of escape characters,
+                                       return true;                                                                    // the current character is escaped.
+                               }
+                               
+                               return false;
+                       } catch (BadLocationException e) {
+                               // be conservative
+                               return true;
+                       }
+               }
+
                /*
                 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
                 */
                public void verifyKey(VerifyEvent event) {
                        if (!event.doit)
                                return;
+                       
                        final ISourceViewer sourceViewer = getSourceViewer();
                        IDocument document = sourceViewer.getDocument();
                        final Point selection = sourceViewer.getSelectedRange();
                        final int offset = selection.x;
                        final int length = selection.y;
+                       
                        try {
                                ITypedRegion partition = document.getPartition(offset);
                                String type = partition.getType();
-                               if (type.equals(IPHPPartitions.PHP_PARTITIONING)
-                                               || type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
-                                       // you will get IDocument.DEFAULT_CONTENT_TYPE for both PHP
-                                       // and HTML area
+                               
+                               if (type.equals(IPHPPartitions.PHP_PARTITIONING) || type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
+                                       // you will get IDocument.DEFAULT_CONTENT_TYPE for both PHP and HTML area
                                        switch (event.character) {
-                                       case '(':
-                                               if (hasCharacterToTheRight(document, offset + length,
-                                                               '('))
-                                                       return;
-                                               // fall through
-                                       case '[':
-                                               if (!fCloseBracketsPHP)
-                                                       return;
-                                               if (hasIdentifierToTheRight(document, offset + length))
-                                                       return;
-                                               // fall through
-                                       case '{':
-                                               if (!fCloseBracketsPHP)
-                                                       return;
-                                               if (hasIdentifierToTheRight(document, offset + length))
-                                                       return;
-                                               // fall through
-                                       case '"':
-                                               if (event.character == '"') {
-                                                       if (!fCloseStringsPHPDQ)
+                                               case '(':
+                                                       if (hasCharacterToTheRight (document, offset + length, '('))
                                                                return;
-                                                       // changed for statements like echo "" print ""
-                                                       // if (hasIdentifierToTheLeft(document, offset)
-                                                       // ||
-                                                       // hasIdentifierToTheRight(document, offset +
-                                                       // length))
-                                                       if (hasIdentifierToTheRight(document, offset
-                                                                       + length))
+                                                       // fall through
+                                               case '[':
+                                                       if (!fCloseBracketsPHP)
                                                                return;
-                                               }
-                                               // ITypedRegion partition=
-                                               // document.getPartition(offset);
-                                               // if (!
-                                               // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
-                                               // &&
-                                               // (partition.getOffset() != offset))
-                                               // return;
-                                               final char characterDQ = event.character;
-                                               final char closingCharacterDQ = getPeerCharacter(characterDQ);
-                                               final StringBuffer bufferDQ = new StringBuffer();
-                                               bufferDQ.append(characterDQ);
-                                               bufferDQ.append(closingCharacterDQ);
-                                               document.replace(offset, length, bufferDQ.toString());
-                                               LinkedPositionManager managerDQ = new LinkedPositionManager(
-                                                               document);
-                                               managerDQ.addPosition(offset + 1, 0);
-                                               fOffset = offset;
-                                               fLength = 2;
-                                               LinkedPositionUI editorDQ = new LinkedPositionUI(
-                                                               sourceViewer, managerDQ);
-                                               editorDQ.setCancelListener(this);
-                                               editorDQ.setExitPolicy(new ExitPolicy(
-                                                               closingCharacterDQ));
-                                               editorDQ.setFinalCaretOffset(offset + 2);
-                                               editorDQ.enter();
-                                               IRegion newSelectionDQ = editorDQ.getSelectedRegion();
-                                               sourceViewer.setSelectedRange(newSelectionDQ
-                                                               .getOffset(), newSelectionDQ.getLength());
-                                               event.doit = false;
-                                               break;
-                                       case '\'':
-                                               if (event.character == '\'') {
-                                                       if (!fCloseStringsPHPSQ)
+                                                       if (hasIdentifierToTheRight (document, offset + length))
                                                                return;
-                                                       // changed for statements like echo "" print ""
-                                                       // if (hasIdentifierToTheLeft(document, offset)
-                                                       // ||
-                                                       // hasIdentifierToTheRight(document, offset +
-                                                       // length))
-                                                       if (hasIdentifierToTheRight(document, offset
-                                                                       + length))
+                                                       // fall through
+                                               case '{':
+                                                       if (!fCloseBracketsPHP)
                                                                return;
-                                               }
-                                               // ITypedRegion partition=
-                                               // document.getPartition(offset);
-                                               // if (!
-                                               // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
-                                               // &&
-                                               // (partition.getOffset() != offset))
-                                               // return;
-                                               final char characterSQ = event.character;
-                                               final char closingCharacterSQ = getPeerCharacter(characterSQ);
-                                               final StringBuffer bufferSQ = new StringBuffer();
-                                               bufferSQ.append(characterSQ);
-                                               bufferSQ.append(closingCharacterSQ);
-                                               document.replace(offset, length, bufferSQ.toString());
-                                               LinkedPositionManager managerSQ = new LinkedPositionManager(
-                                                               document);
-                                               managerSQ.addPosition(offset + 1, 0);
-                                               fOffset = offset;
-                                               fLength = 2;
-                                               LinkedPositionUI editorSQ = new LinkedPositionUI(
-                                                               sourceViewer, managerSQ);
-                                               editorSQ.setCancelListener(this);
-                                               editorSQ.setExitPolicy(new ExitPolicy(
-                                                               closingCharacterSQ));
-                                               editorSQ.setFinalCaretOffset(offset + 2);
-                                               editorSQ.enter();
-                                               IRegion newSelectionSQ = editorSQ.getSelectedRegion();
-                                               sourceViewer.setSelectedRange(newSelectionSQ
-                                                               .getOffset(), newSelectionSQ.getLength());
-                                               event.doit = false;
-                                       case '\r': { // insert linebreaks and new closing brace
-                                                                       // after brace and return
-                                               if (!fCloseBracketsPHP) {
-                                                       return;
-                                               }
-                                               if (hasCharacterToTheLeft(document, offset, '{')
-                                                               && hasCharacterToTheRight(document, offset, '}')) {
-                                                       String lineDelimiter = StubUtility
-                                                                       .getLineDelimiterFor(document);
-                                                       int caretPos = sourceViewer.getTextWidget()
-                                                                       .getCaretOffset();
-                                                       final StringBuffer buffer = new StringBuffer(
-                                                                       lineDelimiter);
-                                                       // get indentation
-                                                       IRegion line = document
-                                                                       .getLineInformationOfOffset(offset);
-                                                       String currentLine = document.get(line.getOffset(),
-                                                                       line.getLength());
-                                                       int index = 0;
-                                                       int max = currentLine.length();
-                                                       StringBuffer indent = new StringBuffer();
-                                                       while (index < max
-                                                                       && Character.isWhitespace(currentLine
-                                                                                       .charAt(index))) {
-                                                               indent.append(currentLine.charAt(index));
-                                                               index++;
+                                                       if (hasIdentifierToTheRight (document, offset + length))
+                                                               return;
+                                                       // fall through
+                                               case '"':
+                                                       if (event.character == '"') {
+                                                               if (!fCloseStringsPHPDQ) {
+                                                                       return;
+                                                               }
+                                                               
+                                                               // changed for statements like echo "" print ""
+                                                               // if (hasIdentifierToTheLeft(document, offset)
+                                                               // ||
+                                                               // hasIdentifierToTheRight(document, offset +
+                                                               // length))
+                                                               
+                                                               if (hasIdentifierToTheRight (document, offset + length)) {
+                                                                       return;
+                                                               }
+                                                               
+                                                               // We should check here whether the previous character of the typed '"' is a '\'.
+                                                               // In this case we should not issue a closing "
+                                                               
+                                                               if (hasEscapeCharToTheLeft (document, offset + length)) {
+                                                                       return; 
+                                                               }                                                       
+                                                       }
+                                                       
+                                                       // ITypedRegion partition=
+                                                       // document.getPartition(offset);
+                                                       // if (!
+                                                       // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
+                                                       // &&
+                                                       // (partition.getOffset() != offset))
+                                                       // return;
+                                                       
+                                                       final char characterDQ = event.character;
+                                                       final char closingCharacterDQ = getPeerCharacter (characterDQ);
+                                                       final StringBuffer bufferDQ = new StringBuffer ();
+                                                       
+                                                       bufferDQ.append (characterDQ);
+                                                       bufferDQ.append (closingCharacterDQ);
+                                                       document.replace (offset, length, bufferDQ.toString ());
+                                                       LinkedPositionManager managerDQ = new LinkedPositionManager (document);
+                                                       managerDQ.addPosition (offset + 1, 0);
+                                                       
+                                                       fOffset = offset;
+                                                       fLength = 2;
+                                                       
+                                                       LinkedPositionUI editorDQ = new LinkedPositionUI (sourceViewer, managerDQ);
+                                                       editorDQ.setCancelListener (this);
+                                                       editorDQ.setExitPolicy (new ExitPolicy (closingCharacterDQ));
+                                                       editorDQ.setFinalCaretOffset (offset + 2);
+                                                       editorDQ.enter ();
+                                                       
+                                                       IRegion newSelectionDQ = editorDQ.getSelectedRegion ();
+                                                       sourceViewer.setSelectedRange (newSelectionDQ.getOffset(), newSelectionDQ.getLength());
+                                                       event.doit = false;
+                                                       break;
+                                                       
+                                               case '\'':
+                                                       if (event.character == '\'') {
+                                                               if (!fCloseStringsPHPSQ) {
+                                                                       return;
+                                                               }
+                                                               
+                                                               // changed for statements like echo "" print ""
+                                                               // if (hasIdentifierToTheLeft(document, offset)
+                                                               // ||
+                                                               // hasIdentifierToTheRight(document, offset +
+                                                               // length))
+                                                               if (hasIdentifierToTheRight(document, offset + length)) {
+                                                                       return;
+                                                               }
+
+                                                               // We should check here whether the previous character of the typed '"' is a '\'.
+                                                               // In this case we should not issue a closing "
+                                                               
+                                                               if (hasEscapeCharToTheLeft (document, offset + length)) {
+                                                                       return; 
+                                                               }
                                                        }
-                                                       buffer.append(indent);
-                                                       JavaHeuristicScanner scanner = new JavaHeuristicScanner(
-                                                                       document);
-                                                       JavaIndenter indenter = new JavaIndenter(document,
-                                                                       scanner);
-                                                       buffer.append(indenter.createIndent(1));
-                                                       int cursorPos = buffer.length();
-                                                       buffer.append(lineDelimiter);
-                                                       buffer.append(indent);
-                                                       document.replace(offset, length, buffer.toString());
-                                                       sourceViewer.getTextWidget().setCaretOffset(
-                                                                       caretPos + cursorPos);
+                                                       // ITypedRegion partition=
+                                                       // document.getPartition(offset);
+                                                       // if (!
+                                                       // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
+                                                       // &&
+                                                       // (partition.getOffset() != offset))
+                                                       // return;
+                                                       final char characterSQ = event.character;
+                                                       final char closingCharacterSQ = getPeerCharacter (characterSQ);
+                                                       final StringBuffer bufferSQ = new StringBuffer ();
+                                                       
+                                                       bufferSQ.append (characterSQ);
+                                                       bufferSQ.append (closingCharacterSQ);
+                                                       document.replace (offset, length, bufferSQ.toString ());
+                                                       
+                                                       LinkedPositionManager managerSQ = new LinkedPositionManager (document);
+                                                       managerSQ.addPosition (offset + 1, 0);
+                                                       
+                                                       fOffset = offset;
+                                                       fLength = 2;
+                                                       
+                                                       LinkedPositionUI editorSQ = new LinkedPositionUI (sourceViewer, managerSQ);
+                                                       editorSQ.setCancelListener (this);
+                                                       editorSQ.setExitPolicy (new ExitPolicy (closingCharacterSQ));
+                                                       editorSQ.setFinalCaretOffset (offset + 2);
+                                                       editorSQ.enter ();
+                                                       
+                                                       IRegion newSelectionSQ = editorSQ.getSelectedRegion ();
+                                                       sourceViewer.setSelectedRange (newSelectionSQ.getOffset(), newSelectionSQ.getLength());
                                                        event.doit = false;
+                                               case '\r': { // insert linebreaks and new closing brace after brace and return
+                                                       if (!fCloseBracketsPHP) {
+                                                               return;
+                                                       }
+                                                       
+                                                       if (hasCharacterToTheLeft(document, offset, '{') && hasCharacterToTheRight(document, offset, '}')) {
+                                                               String lineDelimiter = StubUtility.getLineDelimiterFor(document);
+                                                               int caretPos = sourceViewer.getTextWidget().getCaretOffset();
+                                                               final StringBuffer buffer = new StringBuffer(lineDelimiter);
+                                                               // get indentation
+                                                               IRegion line = document.getLineInformationOfOffset(offset);
+                                                               String currentLine = document.get(line.getOffset(),line.getLength());
+                                                               int index = 0;
+                                                               int max = currentLine.length();
+                                                               StringBuffer indent = new StringBuffer();
+                                                               
+                                                               while (index < max
+                                                                               && Character.isWhitespace(currentLine.charAt(index))) {
+                                                                       indent.append(currentLine.charAt(index));
+                                                                       index++;
+                                                               }
+                                                               
+                                                               buffer.append(indent);
+                                                               JavaHeuristicScanner scanner = new JavaHeuristicScanner(document);
+                                                               JavaIndenter indenter = new JavaIndenter(document,scanner);
+                                                               buffer.append(indenter.createIndent(1));
+                                                               int cursorPos = buffer.length();
+                                                               buffer.append(lineDelimiter);
+                                                               buffer.append(indent);
+                                                               document.replace(offset, length, buffer.toString());
+                                                               sourceViewer.getTextWidget().setCaretOffset(                                                                    caretPos + cursorPos);
+                                                               event.doit = false;
+                                                       }
                                                }
                                        }
-                                       }
                                }
                        } catch (BadLocationException e) {
                        }
@@ -1256,7 +1324,7 @@ public class PHPUnitEditor extends PHPEditor { // implements
        /** The editor's bracket painter */
        // private BracketPainter fBracketPainter;
        /** The editor's bracket matcher */
-       private PHPPairMatcher fBracketMatcher;
+       //private PHPPairMatcher fBracketMatcher;
 
        /** The editor's line painter */
        // private LinePainter fLinePainter;
@@ -1273,7 +1341,7 @@ public class PHPUnitEditor extends PHPEditor { // implements
        // private IPropertyChangeListener fPropertyChangeListener = new
        // PropertyChangeListener();
        /** The remembered java element */
-       private IJavaElement fRememberedElement;
+       //private IJavaElement fRememberedElement;
 
        /**
         * The remembered selection.
@@ -1283,7 +1351,7 @@ public class PHPUnitEditor extends PHPEditor { // implements
        private RememberedSelection fRememberedSelection = new RememberedSelection();
 
        /** The remembered php element offset */
-       private int fRememberedElementOffset;
+       //private int fRememberedElementOffset;
 
        /** The bracket inserter. */
        private BracketInserter fBracketInserter = new BracketInserter();
@@ -1343,52 +1411,52 @@ public class PHPUnitEditor extends PHPEditor { // implements
        // private final static String WARNING_INDICATION_COLOR =
        // PreferenceConstants.EDITOR_WARNING_INDICATION_COLOR;
        /** Preference key for task indication */
-       private final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
+       //private final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
 
        /** Preference key for task color */
-       private final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
+       //private final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
 
        /** Preference key for bookmark indication */
-       private final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
+       //private final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
 
        /** Preference key for bookmark color */
-       private final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
+       //private final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
 
        /** Preference key for search result indication */
-       private final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
+       //private final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
 
        /** Preference key for search result color */
-       private final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
+       //private final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
 
        /** Preference key for unknown annotation indication */
-       private final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
+       //private final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
 
        /** Preference key for unknown annotation color */
-       private final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
+       //private final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
 
        /** Preference key for linked position color */
-       private final static String LINKED_POSITION_COLOR = PreferenceConstants.EDITOR_LINKED_POSITION_COLOR;
+       //private final static String LINKED_POSITION_COLOR = PreferenceConstants.EDITOR_LINKED_POSITION_COLOR;
 
        /** Preference key for shwoing the overview ruler */
-       private final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
+       //private final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
 
        /** Preference key for error indication in overview ruler */
-       private final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
+       //private final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
 
        /** Preference key for warning indication in overview ruler */
-       private final static String WARNING_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
+       //private final static String WARNING_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
 
        /** Preference key for task indication in overview ruler */
-       private final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
+       //private final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
 
        /** Preference key for bookmark indication in overview ruler */
-       private final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
+       //private final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
 
        /** Preference key for search result indication in overview ruler */
-       private final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
+       //private final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
 
        /** Preference key for unknown annotation indication in overview ruler */
-       private final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
+       //private final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
 
        /** Preference key for automatically closing double quoted strings */
        private final static String CLOSE_STRINGS_DQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_DQ_PHP;
@@ -1403,22 +1471,22 @@ public class PHPUnitEditor extends PHPEditor { // implements
        private final static String CLOSE_BRACKETS_PHP = PreferenceConstants.EDITOR_CLOSE_BRACKETS_PHP;
 
        /** Preference key for automatically closing phpdocs and comments */
-       private final static String CLOSE_JAVADOCS = PreferenceConstants.EDITOR_CLOSE_JAVADOCS;
+       //private final static String CLOSE_JAVADOCS = PreferenceConstants.EDITOR_CLOSE_JAVADOCS;
 
        /** Preference key for automatically adding phpdoc tags */
-       private final static String ADD_JAVADOC_TAGS = PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS;
+       //private final static String ADD_JAVADOC_TAGS = PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS;
 
        /** Preference key for automatically formatting phpdocs */
        // private final static String FORMAT_JAVADOCS =
        // PreferenceConstants.EDITOR_FORMAT_JAVADOCS;
        /** Preference key for automatically closing strings */
-       private final static String CLOSE_STRINGS_HTML = PreferenceConstants.EDITOR_CLOSE_STRINGS_HTML;
+       //private final static String CLOSE_STRINGS_HTML = PreferenceConstants.EDITOR_CLOSE_STRINGS_HTML;
 
        /** Preference key for automatically closing brackets and parenthesis */
-       private final static String CLOSE_BRACKETS_HTML = PreferenceConstants.EDITOR_CLOSE_BRACKETS_HTML;
+       //private final static String CLOSE_BRACKETS_HTML = PreferenceConstants.EDITOR_CLOSE_BRACKETS_HTML;
 
        /** Preference key for smart paste */
-       private final static String SMART_PASTE = PreferenceConstants.EDITOR_SMART_PASTE;
+       //private final static String SMART_PASTE = PreferenceConstants.EDITOR_SMART_PASTE;
 
        // private final static class AnnotationInfo {
        // public String fColorPreference;
@@ -1741,8 +1809,8 @@ public class PHPUnitEditor extends PHPEditor { // implements
                // fPaintManager = new PaintManager(getSourceViewer());
                LinePainter linePainter;
                linePainter = new LinePainter(getSourceViewer());
-               linePainter.setHighlightColor(new Color(Display.getCurrent(), 225, 235,
-                               224));
+               linePainter.setHighlightColor(new Color(Display.getCurrent(), 225, 235, 224));
+               
                // fPaintManager.addPainter(linePainter);
                // if (isBracketHighlightingEnabled())
                // startBracketHighlighting();
@@ -1756,8 +1824,9 @@ public class PHPUnitEditor extends PHPEditor { // implements
                // if (isAnnotationIndicationEnabled(type))
                // startAnnotationIndication(type);
                // }
-               if (isTabConversionEnabled())
+               if (isTabConversionEnabled ()) {
                        startTabConversion();
+               }
                // if (isOverviewRulerVisible())
                // showOverviewRuler();
                //
@@ -1765,19 +1834,19 @@ public class PHPUnitEditor extends PHPEditor { // implements
                // PHPeclipsePlugin.getDefault().getPluginPreferences();
                // preferences.addPropertyChangeListener(fPropertyChangeListener);
                IPreferenceStore preferenceStore = getPreferenceStore();
-               boolean closeBracketsPHP = preferenceStore
-                               .getBoolean(CLOSE_BRACKETS_PHP);
-               boolean closeStringsPHPDQ = preferenceStore
-                               .getBoolean(CLOSE_STRINGS_DQ_PHP);
-               boolean closeStringsPHPSQ = preferenceStore
-                               .getBoolean(CLOSE_STRINGS_SQ_PHP);
-               fBracketInserter.setCloseBracketsPHPEnabled(closeBracketsPHP);
-               fBracketInserter.setCloseStringsPHPDQEnabled(closeStringsPHPDQ);
-               fBracketInserter.setCloseStringsPHPSQEnabled(closeStringsPHPSQ);
+               boolean closeBracketsPHP  = preferenceStore.getBoolean (CLOSE_BRACKETS_PHP);
+               boolean closeStringsPHPDQ = preferenceStore.getBoolean (CLOSE_STRINGS_DQ_PHP);
+               boolean closeStringsPHPSQ = preferenceStore.getBoolean (CLOSE_STRINGS_SQ_PHP);
+
+               fBracketInserter.setCloseBracketsPHPEnabled (closeBracketsPHP);
+               fBracketInserter.setCloseStringsPHPDQEnabled (closeStringsPHPDQ);
+               fBracketInserter.setCloseStringsPHPSQEnabled (closeStringsPHPSQ);
+
                ISourceViewer sourceViewer = getSourceViewer();
-               if (sourceViewer instanceof ITextViewerExtension)
-                       ((ITextViewerExtension) sourceViewer)
-                                       .prependVerifyKeyListener(fBracketInserter);
+        
+               if (sourceViewer instanceof ITextViewerExtension) {
+                       ((ITextViewerExtension) sourceViewer).prependVerifyKeyListener(fBracketInserter);
+        }
        }
 
        private static char getPeerCharacter(char character) {
@@ -1881,16 +1950,16 @@ public class PHPUnitEditor extends PHPEditor { // implements
                return store.getBoolean(SPACES_FOR_TABS);
        }
 
-       private Color getColor(String key) {
-               RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
-               return getColor(rgb);
-       }
+//     private Color getColor(String key) {
+//             RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
+//             return getColor(rgb);
+//     }
 
-       private Color getColor(RGB rgb) {
-               JavaTextTools textTools = PHPeclipsePlugin.getDefault()
-                               .getJavaTextTools();
-               return textTools.getColorManager().getColor(rgb);
-       }
+//     private Color getColor(RGB rgb) {
+//             JavaTextTools textTools = PHPeclipsePlugin.getDefault()
+//                             .getJavaTextTools();
+//             return textTools.getColorManager().getColor(rgb);
+//     }
 
        // private Color getColor(AnnotationType annotationType) {
        // AnnotationInfo info = (AnnotationInfo)
@@ -2118,19 +2187,19 @@ public class PHPUnitEditor extends PHPEditor { // implements
        // IVerticalRuler ruler, int styles) {
        // return new AdaptedSourceViewer(parent, ruler, styles);
        // }
-       private boolean isValidSelection(int offset, int length) {
-               IDocumentProvider provider = getDocumentProvider();
-               if (provider != null) {
-                       IDocument document = provider.getDocument(getEditorInput());
-                       if (document != null) {
-                               int end = offset + length;
-                               int documentLength = document.getLength();
-                               return 0 <= offset && offset <= documentLength && 0 <= end
-                                               && end <= documentLength;
-                       }
-               }
-               return false;
-       }
+//     private boolean isValidSelection(int offset, int length) {
+//             IDocumentProvider provider = getDocumentProvider();
+//             if (provider != null) {
+//                     IDocument document = provider.getDocument(getEditorInput());
+//                     if (document != null) {
+//                             int end = offset + length;
+//                             int documentLength = document.getLength();
+//                             return 0 <= offset && offset <= documentLength && 0 <= end
+//                                             && end <= documentLength;
+//                     }
+//             }
+//             return false;
+//     }
 
        /*
         * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#getInputElement()
@@ -2560,43 +2629,43 @@ public class PHPUnitEditor extends PHPEditor { // implements
        /**
         * Returns the updated java element for the old java element.
         */
-       private IJavaElement findElement(IJavaElement element) {
-               if (element == null)
-                       return null;
-               IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
-                               .getWorkingCopyManager();
-               ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
-               if (unit != null) {
-                       try {
-                               synchronized (unit) {
-                                       unit.reconcile();
-                               }
-                               IJavaElement[] findings = unit.findElements(element);
-                               if (findings != null && findings.length > 0)
-                                       return findings[0];
-                       } catch (JavaModelException x) {
-                               PHPeclipsePlugin.log(x.getStatus());
-                               // nothing found, be tolerant and go on
-                       }
-               }
-               return null;
-       }
+//     private IJavaElement findElement(IJavaElement element) {
+//             if (element == null)
+//                     return null;
+//             IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
+//                             .getWorkingCopyManager();
+//             ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
+//             if (unit != null) {
+//                     try {
+//                             synchronized (unit) {
+//                                     unit.reconcile();
+//                             }
+//                             IJavaElement[] findings = unit.findElements(element);
+//                             if (findings != null && findings.length > 0)
+//                                     return findings[0];
+//                     } catch (JavaModelException x) {
+//                             PHPeclipsePlugin.log(x.getStatus());
+//                             // nothing found, be tolerant and go on
+//                     }
+//             }
+//             return null;
+//     }
 
        /**
         * Returns the offset of the given Java element.
         */
-       private int getOffset(IJavaElement element) {
-               if (element instanceof ISourceReference) {
-                       ISourceReference sr = (ISourceReference) element;
-                       try {
-                               ISourceRange srcRange = sr.getSourceRange();
-                               if (srcRange != null)
-                                       return srcRange.getOffset();
-                       } catch (JavaModelException e) {
-                       }
-               }
-               return -1;
-       }
+//     private int getOffset(IJavaElement element) {
+//             if (element instanceof ISourceReference) {
+//                     ISourceReference sr = (ISourceReference) element;
+//                     try {
+//                             ISourceRange srcRange = sr.getSourceRange();
+//                             if (srcRange != null)
+//                                     return srcRange.getOffset();
+//                     } catch (JavaModelException e) {
+//                     }
+//             }
+//             return -1;
+//     }
 
        /*
         * @see AbstractTextEditor#restoreSelection()
@@ -2758,7 +2827,8 @@ public class PHPUnitEditor extends PHPEditor { // implements
                super.editorSaved();
                ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();
                if (a != null) {
-                       a.refresh(ShowExternalPreviewAction.PHP_TYPE);
+                       //a.refresh(ShowExternalPreviewAction.PHP_TYPE);
+                       a.doRun(ShowExternalPreviewAction.PHP_TYPE);
                }
        }
 }
\ No newline at end of file