fixed Bug 1024299
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPUnitEditor.java
index 0a2fd89..6755004 100644 (file)
@@ -110,277 +110,6 @@ public class PHPUnitEditor extends PHPEditor { //implements
     void customizeDocumentCommand(IDocument document, DocumentCommand command);
   };
 
-  //  class AdaptedRulerLayout extends Layout {
-  //
-  //    protected int fGap;
-  //    protected AdaptedSourceViewer fAdaptedSourceViewer;
-  //
-  //    protected AdaptedRulerLayout(int gap, AdaptedSourceViewer asv) {
-  //      fGap = gap;
-  //      fAdaptedSourceViewer = asv;
-  //    }
-  //
-  //    protected Point computeSize(Composite composite, int wHint, int hHint,
-  // boolean flushCache) {
-  //      Control[] children = composite.getChildren();
-  //      Point s = children[children.length - 1].computeSize(SWT.DEFAULT,
-  // SWT.DEFAULT, flushCache);
-  //      if (fAdaptedSourceViewer.isVerticalRulerVisible())
-  //        s.x += fAdaptedSourceViewer.getVerticalRuler().getWidth() + fGap;
-  //      return s;
-  //    }
-  //
-  //    protected void layout(Composite composite, boolean flushCache) {
-  //      Rectangle clArea = composite.getClientArea();
-  //      if (fAdaptedSourceViewer.isVerticalRulerVisible()) {
-  //
-  //        StyledText textWidget = fAdaptedSourceViewer.getTextWidget();
-  //        Rectangle trim = textWidget.computeTrim(0, 0, 0, 0);
-  //        int scrollbarHeight = trim.height;
-  //
-  //        IVerticalRuler vr = fAdaptedSourceViewer.getVerticalRuler();
-  //        int vrWidth = vr.getWidth();
-  //
-  //        int orWidth = 0;
-  //        if (fAdaptedSourceViewer.isOverviewRulerVisible()) {
-  //          OverviewRuler or = fAdaptedSourceViewer.getOverviewRuler();
-  //          orWidth = or.getWidth();
-  //          or.getControl().setBounds(clArea.width - orWidth, scrollbarHeight,
-  // orWidth, clArea.height - 3 * scrollbarHeight);
-  //        }
-  //
-  //        textWidget.setBounds(vrWidth + fGap, 0, clArea.width - vrWidth - orWidth
-  // -
-  // 2 * fGap, clArea.height);
-  //        vr.getControl().setBounds(0, 0, vrWidth, clArea.height -
-  // scrollbarHeight);
-  //
-  //      } else {
-  //        StyledText textWidget = fAdaptedSourceViewer.getTextWidget();
-  //        textWidget.setBounds(0, 0, clArea.width, clArea.height);
-  //      }
-  //    }
-  //  };
-  //
-  //  class AdaptedSourceViewer extends SourceViewer { // extends
-  // JavaCorrectionSourceViewer {
-  //
-  //    private List fTextConverters;
-  //
-  //    private OverviewRuler fOverviewRuler;
-  //    private boolean fIsOverviewRulerVisible;
-  //    /** The viewer's overview ruler hovering controller */
-  //    private AbstractHoverInformationControlManager
-  // fOverviewRulerHoveringController;
-  //
-  //    private boolean fIgnoreTextConverters = false;
-  //
-  //    private IVerticalRuler fCachedVerticalRuler;
-  //    private boolean fCachedIsVerticalRulerVisible;
-  //
-  //    public AdaptedSourceViewer(Composite parent, IVerticalRuler ruler, int
-  // styles) {
-  //      super(parent, ruler, styles); //, CompilationUnitEditor.this);
-  //
-  //      fCachedVerticalRuler = ruler;
-  //      fCachedIsVerticalRulerVisible = (ruler != null);
-  //      fOverviewRuler = new OverviewRuler(VERTICAL_RULER_WIDTH);
-  //
-  //      delayedCreateControl(parent, styles);
-  //    }
-  //
-  //    /*
-  //     * @see ISourceViewer#showAnnotations(boolean)
-  //     */
-  //    public void showAnnotations(boolean show) {
-  //      fCachedIsVerticalRulerVisible = (show && fCachedVerticalRuler != null);
-  //      // super.showAnnotations(show);
-  //    }
-  //
-  //    public IContentAssistant getContentAssistant() {
-  //      return fContentAssistant;
-  //    }
-  //
-  //    /*
-  //     * @see ITextOperationTarget#doOperation(int)
-  //     */
-  //    public void doOperation(int operation) {
-  //
-  //      if (getTextWidget() == null)
-  //        return;
-  //
-  //      switch (operation) {
-  //        case CONTENTASSIST_PROPOSALS :
-  //          String msg = fContentAssistant.showPossibleCompletions();
-  //          setStatusLineErrorMessage(msg);
-  //          return;
-  //        case UNDO :
-  //          fIgnoreTextConverters = true;
-  //          break;
-  //        case REDO :
-  //          fIgnoreTextConverters = true;
-  //          break;
-  //      }
-  //
-  //      super.doOperation(operation);
-  //    }
-  //
-  //    public void insertTextConverter(ITextConverter textConverter, int index)
-  // {
-  //      throw new UnsupportedOperationException();
-  //    }
-  //
-  //    public void addTextConverter(ITextConverter textConverter) {
-  //      if (fTextConverters == null) {
-  //        fTextConverters = new ArrayList(1);
-  //        fTextConverters.add(textConverter);
-  //      } else if (!fTextConverters.contains(textConverter))
-  //        fTextConverters.add(textConverter);
-  //    }
-  //
-  //    public void removeTextConverter(ITextConverter textConverter) {
-  //      if (fTextConverters != null) {
-  //        fTextConverters.remove(textConverter);
-  //        if (fTextConverters.size() == 0)
-  //          fTextConverters = null;
-  //      }
-  //    }
-  //
-  //    /*
-  //     * @see TextViewer#customizeDocumentCommand(DocumentCommand)
-  //     */
-  //    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);
-  //      }
-  //      fIgnoreTextConverters = false;
-  //    }
-  //
-  //    public IVerticalRuler getVerticalRuler() {
-  //      return fCachedVerticalRuler;
-  //    }
-  //
-  //    public boolean isVerticalRulerVisible() {
-  //      return fCachedIsVerticalRulerVisible;
-  //    }
-  //
-  //    public OverviewRuler getOverviewRuler() {
-  //      return fOverviewRuler;
-  //    }
-  //
-  //    /*
-  //     * @see TextViewer#createControl(Composite, int)
-  //     */
-  //    protected void createControl(Composite parent, int styles) {
-  //      // do nothing here
-  //    }
-  //
-  //    protected void delayedCreateControl(Composite parent, int styles) {
-  //      //create the viewer
-  //      super.createControl(parent, styles);
-  //
-  //      Control control = getControl();
-  //      if (control instanceof Composite) {
-  //        Composite composite = (Composite) control;
-  //        composite.setLayout(new AdaptedRulerLayout(GAP_SIZE, this));
-  //        fOverviewRuler.createControl(composite, this);
-  //      }
-  //    }
-  //    protected void ensureOverviewHoverManagerInstalled() {
-  //      if (fOverviewRulerHoveringController == null && fAnnotationHover != null
-  // && fHoverControlCreator != null) {
-  //        fOverviewRulerHoveringController =
-  //          new OverviewRulerHoverManager(fOverviewRuler, this, fAnnotationHover,
-  // fHoverControlCreator);
-  //        fOverviewRulerHoveringController.install(fOverviewRuler.getControl());
-  //      }
-  //    }
-  //
-  //    public void hideOverviewRuler() {
-  //      fIsOverviewRulerVisible = false;
-  //      Control control = getControl();
-  //      if (control instanceof Composite) {
-  //        Composite composite = (Composite) control;
-  //        composite.layout();
-  //      }
-  //      if (fOverviewRulerHoveringController != null) {
-  //        fOverviewRulerHoveringController.dispose();
-  //        fOverviewRulerHoveringController = null;
-  //      }
-  //    }
-  //
-  //    public void showOverviewRuler() {
-  //      fIsOverviewRulerVisible = true;
-  //      Control control = getControl();
-  //      if (control instanceof Composite) {
-  //        Composite composite = (Composite) control;
-  //        composite.layout();
-  //      }
-  //      ensureOverviewHoverManagerInstalled();
-  //    }
-  //
-  //    public boolean isOverviewRulerVisible() {
-  //      return fIsOverviewRulerVisible;
-  //    }
-  //
-  //    /*
-  //     * @see ISourceViewer#setDocument(IDocument, IAnnotationModel, int, int)
-  //     */
-  //    public void setDocument(
-  //      IDocument document,
-  //      IAnnotationModel annotationModel,
-  //      int visibleRegionOffset,
-  //      int visibleRegionLength) {
-  //      super.setDocument(document, annotationModel, visibleRegionOffset,
-  // visibleRegionLength);
-  //      fOverviewRuler.setModel(annotationModel);
-  //    }
-  //
-  //    // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
-  //    public void updateIndentationPrefixes() {
-  //      SourceViewerConfiguration configuration = getSourceViewerConfiguration();
-  //      String[] types = configuration.getConfiguredContentTypes(this);
-  //      for (int i = 0; i < types.length; i++) {
-  //        String[] prefixes = configuration.getIndentPrefixes(this, types[i]);
-  //        if (prefixes != null && prefixes.length > 0)
-  //          setIndentPrefixes(prefixes, types[i]);
-  //      }
-  //    }
-  //
-  //    /*
-  //     * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
-  //     */
-  //    public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
-  //      if (WorkbenchHelp.isContextHelpDisplayed())
-  //        return false;
-  //      return super.requestWidgetToken(requester);
-  //    }
-  //
-  //    /*
-  //     * @see
-  // org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
-  //     */
-  //    public void configure(SourceViewerConfiguration configuration) {
-  //      super.configure(configuration);
-  //      // prependAutoEditStrategy(new SmartBracesAutoEditStrategy(this),
-  // IDocument.DEFAULT_CONTENT_TYPE);
-  //    }
-  //
-  //    protected void handleDispose() {
-  //      fOverviewRuler = null;
-  //
-  //      if (fOverviewRulerHoveringController != null) {
-  //        fOverviewRulerHoveringController.dispose();
-  //        fOverviewRulerHoveringController = null;
-  //      }
-  //
-  //      super.handleDispose();
-  //    }
-  //
-  //  };
   class AdaptedSourceViewer extends JavaSourceViewer {
     private List fTextConverters;
 
@@ -990,8 +719,8 @@ public class PHPUnitEditor extends PHPEditor { //implements
       LinkedPositionUI.ExitListener {
     private boolean fCloseBracketsPHP = true;
 
-    private boolean fCloseStringsPHP = true;
-
+    private boolean fCloseStringsPHPDQ = true;
+    private boolean fCloseStringsPHPSQ = true;
     private boolean fCloseBracketsHTML = true;
 
     private boolean fCloseStringsHTML = true;
@@ -1004,10 +733,12 @@ public class PHPUnitEditor extends PHPEditor { //implements
       fCloseBracketsPHP = enabled;
     }
 
-    public void setCloseStringsPHPEnabled(boolean enabled) {
-      fCloseStringsPHP = enabled;
+    public void setCloseStringsPHPDQEnabled(boolean enabled) {
+      fCloseStringsPHPDQ = enabled;
+    }
+    public void setCloseStringsPHPSQEnabled(boolean enabled) {
+      fCloseStringsPHPSQ = enabled;
     }
-
     public void setCloseBracketsHTMLEnabled(boolean enabled) {
       fCloseBracketsHTML = enabled;
     }
@@ -1075,7 +806,9 @@ public class PHPUnitEditor extends PHPEditor { //implements
       try {
         ITypedRegion partition = document.getPartition(offset);
         String type = partition.getType();
-        if (type.equals(IPHPPartitions.PHP_PARTITIONING)) {
+        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, '('))
@@ -1089,7 +822,7 @@ public class PHPUnitEditor extends PHPEditor { //implements
           // fall through
           case '"':
             if (event.character == '"') {
-              if (!fCloseStringsPHP)
+              if (!fCloseStringsPHPDQ)
                 return;
               // changed for statements like echo "" print ""
               //    if (hasIdentifierToTheLeft(document, offset)
@@ -1106,46 +839,38 @@ public class PHPUnitEditor extends PHPEditor { //implements
             // &&
             // (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);
+            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 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());
+            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;
-          }
-        } else if (type.equals(IPHPPartitions.HTML)
-            || type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
-          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)
+            break;
+//          fall through
+          case '\'':
+            if (event.character == '\'') {
+              if (!fCloseStringsPHPSQ)
                 return;
-              if (hasIdentifierToTheLeft(document, offset)
-                  || hasIdentifierToTheRight(document, offset + length))
+              // changed for statements like echo "" print ""
+              //    if (hasIdentifierToTheLeft(document, offset)
+              // ||
+              // hasIdentifierToTheRight(document, offset +
+              // length))
+              if (hasIdentifierToTheRight(document, offset + length))
                 return;
             }
             //     ITypedRegion partition=
@@ -1155,28 +880,77 @@ public class PHPUnitEditor extends PHPEditor { //implements
             // &&
             // (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);
+            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 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());
+            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;
           }
         }
+//        } 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) {
       }
     }
@@ -1348,8 +1122,10 @@ public class PHPUnitEditor extends PHPEditor { //implements
   /** 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;
 
-  /** Preference key for automatically closing strings */
-  private final static String CLOSE_STRINGS_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_PHP;
+  /** Preference key for automatically closing double quoted strings */
+  private final static String CLOSE_STRINGS_DQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_DQ_PHP;
+  /** Preference key for automatically closing single quoted strings */
+  private final static String CLOSE_STRINGS_SQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_SQ_PHP;
 
   /** Preference key for automatically wrapping Java strings */
   private final static String WRAP_STRINGS = PreferenceConstants.EDITOR_WRAP_STRINGS;
@@ -1673,11 +1449,13 @@ public class PHPUnitEditor extends PHPEditor { //implements
     //    preferences.addPropertyChangeListener(fPropertyChangeListener);
     IPreferenceStore preferenceStore = getPreferenceStore();
     boolean closeBracketsPHP = preferenceStore.getBoolean(CLOSE_BRACKETS_PHP);
-    boolean closeStringsPHP = preferenceStore.getBoolean(CLOSE_STRINGS_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.setCloseStringsPHPEnabled(closeStringsPHP);
+    fBracketInserter.setCloseStringsPHPDQEnabled(closeStringsPHPDQ);
+    fBracketInserter.setCloseStringsPHPSQEnabled(closeStringsPHPSQ);
     fBracketInserter.setCloseBracketsHTMLEnabled(closeBracketsHTML);
     fBracketInserter.setCloseStringsHTMLEnabled(closeStringsHTML);
     ISourceViewer sourceViewer = getSourceViewer();
@@ -1698,6 +1476,8 @@ public class PHPUnitEditor extends PHPEditor { //implements
       return '[';
     case '"':
       return character;
+    case '\'':
+      return character;
     default:
       throw new IllegalArgumentException();
     }
@@ -1953,8 +1733,13 @@ public class PHPUnitEditor extends PHPEditor { //implements
               .getBoolean(p));
           return;
         }
-        if (CLOSE_STRINGS_PHP.equals(p)) {
-          fBracketInserter.setCloseStringsPHPEnabled(getPreferenceStore()
+        if (CLOSE_STRINGS_DQ_PHP.equals(p)) {
+          fBracketInserter.setCloseStringsPHPDQEnabled(getPreferenceStore()
+              .getBoolean(p));
+          return;
+        }
+        if (CLOSE_STRINGS_SQ_PHP.equals(p)) {
+          fBracketInserter.setCloseStringsPHPSQEnabled(getPreferenceStore()
               .getBoolean(p));
           return;
         }