implemented 1416503, automatic closing of braces
authorbananeweizen <bananeweizen>
Fri, 27 Jan 2006 22:11:45 +0000 (22:11 +0000)
committerbananeweizen <bananeweizen>
Fri, 27 Jan 2006 22:11:45 +0000 (22:11 +0000)
net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPUnitEditor.java

index 153bf6d..41387fe 100644 (file)
@@ -17,6 +17,7 @@ import net.sourceforge.phpdt.core.JavaCore;
 import net.sourceforge.phpdt.core.JavaModelException;
 import net.sourceforge.phpdt.core.dom.CompilationUnit;
 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
+import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
 import net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction;
 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
 import net.sourceforge.phpdt.internal.ui.actions.IndentAction;
@@ -914,10 +915,6 @@ public class PHPUnitEditor extends PHPEditor { //implements
 
        private boolean fCloseStringsPHPSQ = true;
 
-       private boolean fCloseBracketsHTML = true;
-
-       private boolean fCloseStringsHTML = true;
-
        private int fOffset;
 
        private int fLength;
@@ -934,14 +931,6 @@ public class PHPUnitEditor extends PHPEditor { //implements
          fCloseStringsPHPSQ = enabled;
        }
 
-       public void setCloseBracketsHTMLEnabled(boolean enabled) {
-         fCloseBracketsHTML = enabled;
-       }
-
-       public void setCloseStringsHTMLEnabled(boolean enabled) {
-         fCloseStringsHTML = enabled;
-       }
-
        private boolean hasIdentifierToTheRight(IDocument document, int offset) {
          try {
                int end = offset;
@@ -969,6 +958,19 @@ public class PHPUnitEditor extends PHPEditor { //implements
          }
        }
 
+       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)))
+                         --start;
+                       return start != minStart && document.getChar(start - 1)== character;
+                 } catch (BadLocationException e) {
+                       return false;
+                 }
+               }
+
        private boolean hasCharacterToTheRight(IDocument document, int offset, char character) {
          try {
                int end = offset;
@@ -1048,7 +1050,6 @@ public class PHPUnitEditor extends PHPEditor { //implements
                        sourceViewer.setSelectedRange(newSelectionDQ.getOffset(), newSelectionDQ.getLength());
                        event.doit = false;
                        break;
-                 //          fall through
                  case '\'':
                        if (event.character == '\'') {
                          if (!fCloseStringsPHPSQ)
@@ -1086,57 +1087,37 @@ public class PHPUnitEditor extends PHPEditor { //implements
                        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,'{')) {
+                                 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);
+                               buffer.append("\t");
+                               int cursorPos=buffer.length();
+                               buffer.append(lineDelimiter);
+                               buffer.append(indent);
+                               buffer.append("}");
+                               document.replace(offset, length, buffer.toString());
+                               sourceViewer.getTextWidget().setCaretOffset(caretPos+cursorPos);
+                               event.doit = false;
+                         }
+                 }
                  }
                }
-               //        } else if (type.equals(IPHPPartitions.HTML)) {
-               //          switch (event.character) {
-               //          case '(':
-               //            if (hasCharacterToTheRight(document, offset + length, '('))
-               //              return;
-               //          // fall through
-               //          case '[':
-               //            if (!fCloseBracketsHTML)
-               //              return;
-               //            if (hasIdentifierToTheRight(document, offset + length))
-               //              return;
-               //          // fall through
-               //          case '"':
-               //            if (event.character == '"') {
-               //              if (!fCloseStringsHTML)
-               //                return;
-               //              if (hasIdentifierToTheLeft(document, offset)
-               //                  || hasIdentifierToTheRight(document, offset + length))
-               //                return;
-               //            }
-               //            // ITypedRegion partition=
-               //            // document.getPartition(offset);
-               //            // if (!
-               //            // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
-               //            // &&
-               //            // (partition.getOffset() != offset))
-               //            // return;
-               //            final char character = event.character;
-               //            final char closingCharacter = getPeerCharacter(character);
-               //            final StringBuffer buffer = new StringBuffer();
-               //            buffer.append(character);
-               //            buffer.append(closingCharacter);
-               //            document.replace(offset, length, buffer.toString());
-               //            LinkedPositionManager manager = new LinkedPositionManager(document);
-               //            manager.addPosition(offset + 1, 0);
-               //            fOffset = offset;
-               //            fLength = 2;
-               //            LinkedPositionUI editor = new LinkedPositionUI(sourceViewer,
-               //                manager);
-               //            editor.setCancelListener(this);
-               //            editor.setExitPolicy(new ExitPolicy(closingCharacter));
-               //            editor.setFinalCaretOffset(offset + 2);
-               //            editor.enter();
-               //            IRegion newSelection = editor.getSelectedRegion();
-               //            sourceViewer.setSelectedRange(newSelection.getOffset(),
-               //                newSelection.getLength());
-               //            event.doit = false;
-               //          }
-               //        }
          } catch (BadLocationException e) {
          }
        }
@@ -1498,13 +1479,13 @@ public class PHPUnitEditor extends PHPEditor { //implements
        //    markAsSelectionDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
        //              WorkbenchHelp.setHelp(action,
        // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
-       //              action= new IndentAction(PHPEditorMessages.getResourceBundle(),
-       // "Indent.", this, false); //$NON-NLS-1$
-       //              action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
-       //              setAction("Indent", action); //$NON-NLS-1$
-       //              markAsStateDependentAction("Indent", true); //$NON-NLS-1$
-       //              markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
-       //// WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
+                       action= new IndentAction(PHPEditorMessages.getResourceBundle(),
+        "Indent.", this, false); //$NON-NLS-1$
+                       action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
+                       setAction("Indent", action); //$NON-NLS-1$
+                       markAsStateDependentAction("Indent", true); //$NON-NLS-1$
+                       markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
+       // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
        //
        //              action= new IndentAction(PHPEditorMessages.getResourceBundle(),
        // "Indent.", this, true); //$NON-NLS-1$
@@ -1640,13 +1621,9 @@ public class PHPUnitEditor extends PHPEditor { //implements
        boolean closeBracketsPHP = preferenceStore.getBoolean(CLOSE_BRACKETS_PHP);
        boolean closeStringsPHPDQ = preferenceStore.getBoolean(CLOSE_STRINGS_DQ_PHP);
        boolean closeStringsPHPSQ = preferenceStore.getBoolean(CLOSE_STRINGS_SQ_PHP);
-       boolean closeBracketsHTML = preferenceStore.getBoolean(CLOSE_BRACKETS_HTML);
-       boolean closeStringsHTML = preferenceStore.getBoolean(CLOSE_STRINGS_HTML);
        fBracketInserter.setCloseBracketsPHPEnabled(closeBracketsPHP);
        fBracketInserter.setCloseStringsPHPDQEnabled(closeStringsPHPDQ);
        fBracketInserter.setCloseStringsPHPSQEnabled(closeStringsPHPSQ);
-       fBracketInserter.setCloseBracketsHTMLEnabled(closeBracketsHTML);
-       fBracketInserter.setCloseStringsHTMLEnabled(closeStringsHTML);
        ISourceViewer sourceViewer = getSourceViewer();
        if (sourceViewer instanceof ITextViewerExtension)
          ((ITextViewerExtension) sourceViewer).prependVerifyKeyListener(fBracketInserter);
@@ -1666,6 +1643,8 @@ public class PHPUnitEditor extends PHPEditor { //implements
          return character;
        case '\'':
          return character;
+       case '{':
+               return '}';
        default:
          throw new IllegalArgumentException();
        }
@@ -1828,14 +1807,6 @@ public class PHPUnitEditor extends PHPEditor { //implements
                  fBracketInserter.setCloseStringsPHPSQEnabled(getPreferenceStore().getBoolean(p));
                  return;
                }
-               if (CLOSE_BRACKETS_HTML.equals(p)) {
-                 fBracketInserter.setCloseBracketsHTMLEnabled(getPreferenceStore().getBoolean(p));
-                 return;
-               }
-               if (CLOSE_STRINGS_HTML.equals(p)) {
-                 fBracketInserter.setCloseStringsHTMLEnabled(getPreferenceStore().getBoolean(p));
-                 return;
-               }
                if (SPACES_FOR_TABS.equals(p)) {
                  if (isTabConversionEnabled())
                        startTabConversion();