GotoMatchingBracket implementiert
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPEditor.java
index 479f880..ed60680 100644 (file)
@@ -12,7 +12,9 @@ Contributors:
     Klaus Hartlage - www.eclipseproject.de
 **********************************************************************/
 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
+import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
+import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
 import net.sourceforge.phpeclipse.IPreferenceConstants;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
@@ -25,8 +27,13 @@ import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.preference.PreferenceConverter;
+import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
 import org.eclipse.jface.text.ITextOperationTarget;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.ITextViewerExtension3;
+import org.eclipse.jface.text.Region;
 import org.eclipse.jface.text.source.AnnotationRulerColumn;
 import org.eclipse.jface.text.source.CompositeRuler;
 import org.eclipse.jface.text.source.ISourceViewer;
@@ -35,6 +42,8 @@ import org.eclipse.jface.text.source.IVerticalRulerColumn;
 import org.eclipse.jface.text.source.LineNumberRulerColumn;
 import org.eclipse.jface.util.IPropertyChangeListener;
 import org.eclipse.jface.util.PropertyChangeEvent;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.graphics.RGB;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.actions.ActionContext;
@@ -42,6 +51,7 @@ import org.eclipse.ui.actions.ActionGroup;
 import org.eclipse.ui.editors.text.TextEditor;
 import org.eclipse.ui.texteditor.ContentAssistAction;
 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
+import org.eclipse.ui.texteditor.IEditorStatusLine;
 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
 import org.eclipse.ui.texteditor.TextOperationAction;
 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
@@ -49,12 +59,15 @@ import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
  * PHP specific text editor.
  */
 public class PHPEditor extends TextEditor {
+// extends StatusTextEditor implements IViewPartInputProvider  { // extends TextEditor {
 
   // protected PHPActionGroup fActionGroups;
   /** The outline page */
   private PHPContentOutlinePage fOutlinePage;
   private IPreferenceStore phpPrefStore;
 
+  /** The editor's bracket matcher */
+  private PHPPairMatcher fBracketMatcher;
   /** The line number ruler column */
   private LineNumberRulerColumn fLineNumberRulerColumn;
 
@@ -67,6 +80,7 @@ public class PHPEditor extends TextEditor {
    */
   public PHPEditor() {
     super();
+    setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
     setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
   }
   //
@@ -127,9 +141,6 @@ public class PHPEditor extends TextEditor {
       PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
     setAction("ContentAssistProposal", action); //$NON-NLS-1$
 
-    // System.out.println(action.getId());
-    // System.out.println(action.getActionDefinitionId());
-
     setAction(
       "ContentAssistTip",
       new TextOperationAction(
@@ -147,9 +158,6 @@ public class PHPEditor extends TextEditor {
     action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
     setAction("Comment", action);
 
-    // System.out.println(action.getId());
-    // System.out.println(action.getActionDefinitionId());
-
     action =
       new TextOperationAction(
         PHPEditorMessages.getResourceBundle(),
@@ -159,20 +167,20 @@ public class PHPEditor extends TextEditor {
     action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
     setAction("Uncomment", action);
 
-    action = new TextOperationAction(
-        PHPEditorMessages.getResourceBundle(), "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
+    action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
     action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
     setAction("Format", action); //$NON-NLS-1$
-    
-    // System.out.println(action.getId());
-    // System.out.println(action.getActionDefinitionId());
-
-    //  fActionGroups = new PHPActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
 
+    markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
     markAsStateDependentAction("Comment", true); //$NON-NLS-1$
     markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
     markAsStateDependentAction("Format", true); //$NON-NLS-1$
-    
+
+    action = new GotoMatchingBracketAction(this);
+    action.setActionDefinitionId(
+      PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
+    setAction(GotoMatchingBracketAction.GOTO_MATCHING_BRACKET, action);
+
     fGenerateActionGroup =
       new GenerateActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
 
@@ -248,18 +256,22 @@ public class PHPEditor extends TextEditor {
       fOutlinePage.setInput(input);
   }
 
+  /*
+   * @see org.phpeclipse.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
+   */
+//  public Object getViewPartInput() {
+//    return getEditorInput().getAdapter(IFile.class);
+//  }
+  
   /** The <code>PHPEditor</code> implementation of this 
    * <code>AbstractTextEditor</code> method adds any 
    * PHPEditor specific entries.
    */
   public void editorContextMenuAboutToShow(MenuManager menu) {
     super.editorContextMenuAboutToShow(menu);
-    //  addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
-    //  addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
-    addAction(menu, ITextEditorActionConstants.GROUP_EDIT, "Format"); //$NON-NLS-1$ 
-    
-    // fActionGroups.fillContextMenu(menu);
-    
+
+    addAction(menu, ITextEditorActionConstants.GROUP_EDIT, "Format"); //$NON-NLS-1$
+
     ActionContext context =
       new ActionContext(getSelectionProvider().getSelection());
     fContextMenuGroup.setContext(context);
@@ -471,7 +483,7 @@ public class PHPEditor extends TextEditor {
   }
 
   /* (non-Javadoc)
-   * Method declared on AbstractTextEditor
+   * Method declared on TextEditor
    */
   protected void initializeEditor() {
     PHPEditorEnvironment.connect(this);
@@ -504,4 +516,126 @@ public class PHPEditor extends TextEditor {
       }
     });
   }
+  
+  private static IRegion getSignedSelection(ITextViewer viewer) {
+
+    StyledText text = viewer.getTextWidget();
+    int caretOffset = text.getCaretOffset();
+    Point selection = text.getSelection();
+
+    // caret left
+    int offset, length;
+    if (caretOffset == selection.x) {
+      offset = selection.y;
+      length = selection.x - selection.y;
+
+      // caret right
+    } else {
+      offset = selection.x;
+      length = selection.y - selection.x;
+    }
+
+    return new Region(offset, length);
+  }
+
+  private final static char[] BRACKETS = { '{', '}', '(', ')', '[', ']' };
+
+  private static boolean isBracket(char character) {
+    for (int i = 0; i != BRACKETS.length; ++i)
+      if (character == BRACKETS[i])
+        return true;
+    return false;
+  }
+
+  private static boolean isSurroundedByBrackets(
+    IDocument document,
+    int offset) {
+    if (offset == 0 || offset == document.getLength())
+      return false;
+
+    try {
+      return isBracket(document.getChar(offset - 1))
+        && isBracket(document.getChar(offset));
+
+    } catch (BadLocationException e) {
+      return false;
+    }
+  }
+  /**
+    * Jumps to the matching bracket.
+    */
+  public void gotoMatchingBracket() {
+
+    if (fBracketMatcher == null)
+      fBracketMatcher = new PHPPairMatcher(BRACKETS);
+
+    ISourceViewer sourceViewer = getSourceViewer();
+    IDocument document = sourceViewer.getDocument();
+    if (document == null)
+      return;
+
+    IRegion selection = getSignedSelection(sourceViewer);
+
+    int selectionLength = Math.abs(selection.getLength());
+    if (selectionLength > 1) {
+      setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.invalidSelection")); //$NON-NLS-1$              
+      sourceViewer.getTextWidget().getDisplay().beep();
+      return;
+    }
+
+    // #26314
+    int sourceCaretOffset = selection.getOffset() + selection.getLength();
+    if (isSurroundedByBrackets(document, sourceCaretOffset))
+      sourceCaretOffset -= selection.getLength();
+
+    IRegion region = fBracketMatcher.match(document, sourceCaretOffset);
+    if (region == null) {
+      setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.noMatchingBracket")); //$NON-NLS-1$             
+      sourceViewer.getTextWidget().getDisplay().beep();
+      return;
+    }
+
+    int offset = region.getOffset();
+    int length = region.getLength();
+
+    if (length < 1)
+      return;
+
+    int anchor = fBracketMatcher.getAnchor();
+    int targetOffset =
+      (PHPPairMatcher.RIGHT == anchor) ? offset : offset + length - 1;
+
+    boolean visible = false;
+    if (sourceViewer instanceof ITextViewerExtension3) {
+      ITextViewerExtension3 extension = (ITextViewerExtension3) sourceViewer;
+      visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
+    } else {
+      IRegion visibleRegion = sourceViewer.getVisibleRegion();
+      visible =
+        (targetOffset >= visibleRegion.getOffset()
+          && targetOffset < visibleRegion.getOffset() + visibleRegion.getLength());
+    }
+
+    if (!visible) {
+      setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.bracketOutsideSelectedElement")); //$NON-NLS-1$         
+      sourceViewer.getTextWidget().getDisplay().beep();
+      return;
+    }
+
+    if (selection.getLength() < 0)
+      targetOffset -= selection.getLength();
+
+    sourceViewer.setSelectedRange(targetOffset, selection.getLength());
+    sourceViewer.revealRange(targetOffset, selection.getLength());
+  }
+  /**
+     * Ses the given message as error message to this editor's status line.
+     * @param msg message to be set
+     */
+  protected void setStatusLineErrorMessage(String msg) {
+    IEditorStatusLine statusLine =
+      (IEditorStatusLine) getAdapter(IEditorStatusLine.class);
+    if (statusLine != null)
+      statusLine.setMessage(true, msg, null);
+  }
 }