Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / DeclarationProposal.java
index c448d44..238423d 100644 (file)
@@ -4,24 +4,22 @@
  */
 package net.sourceforge.phpdt.internal.ui.text.template;
 
+import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocUtil;
 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
-import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
+import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
+import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
 
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IRegion;
 import org.eclipse.jface.text.ITextViewer;
 import org.eclipse.jface.text.contentassist.IContextInformation;
 import org.eclipse.swt.graphics.Image;
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.swt.widgets.Shell;
 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
@@ -29,18 +27,19 @@ import org.eclipse.swt.widgets.Shell;
 /**
  * A PHP identifier proposal.
  */
-public class DeclarationProposal implements IPHPCompletionProposal {
-
-  private final String fTemplate;
+public class DeclarationProposal extends AbstractProposal { //implements IPHPCompletionProposal {
   private final TemplateContext fContext;
-  private final ITextViewer fViewer;
-  private final Image fImage_fun;
-  private final Image fImage_var;
-  private final IRegion fRegion;
+  private final PHPIdentifierLocation fLocation;
 
   //private TemplateBuffer fTemplateBuffer;
   private String fOldText;
-  private IRegion fSelectedRegion; // initialized by apply()
+  //  private final Image fImage_fun;
+  //  private final Image fImage_var;
+  private final IRegion fRegion;
+  //  private IRegion fSelectedRegion; // initialized by apply()
+
+  private final String fIdentifierName;
+  //  private final ITextViewer fViewer;
 
   /**
    * Creates a template proposal with a template and its context.
@@ -48,17 +47,18 @@ public class DeclarationProposal implements IPHPCompletionProposal {
    * @param context   the context in which the template was requested.
    * @param image     the icon of the proposal.
    */
-  public DeclarationProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
-    //         Assert.isNotNull(template);
-    //         Assert.isNotNull(context);
-    //         Assert.isNotNull(region);
-    //         Assert.isNotNull(viewer);
-
-    fTemplate = template;
+  public DeclarationProposal(
+    String identifierName,
+    PHPIdentifierLocation location,
+    TemplateContext context,
+    IRegion region,
+    ITextViewer viewer) {
+    super(viewer);
+    //    Image image_fun,
+    //    Image image_var) {
+    fIdentifierName = identifierName;
+    fLocation = location;
     fContext = context;
-    fViewer = viewer;
-       fImage_fun= image_fun;
-    fImage_var= image_var;
     fRegion = region;
   }
 
@@ -73,9 +73,20 @@ public class DeclarationProposal implements IPHPCompletionProposal {
       int start = fRegion.getOffset();
       int end = fRegion.getOffset() + fRegion.getLength();
 
-      // insert template string
-    //  String templateString = fTemplate; // fTemplateBuffer.getString();     
-      document.replace(start, end - start, fTemplate);
+      switch (fLocation.getType()) {
+        case PHPIdentifierLocation.FUNCTION :
+          document.replace(start, end - start, fIdentifierName + "()");
+          break;
+        case PHPIdentifierLocation.CONSTRUCTOR :
+          document.replace(start, end - start, fIdentifierName + "()");
+          break;
+        case PHPIdentifierLocation.METHOD :
+          document.replace(start, end - start, fIdentifierName + "()");
+          break;
+
+        default :
+          document.replace(start, end - start, fIdentifierName);
+      }
 
       // translate positions
       LinkedPositionManager manager = new LinkedPositionManager(document);
@@ -94,8 +105,20 @@ public class DeclarationProposal implements IPHPCompletionProposal {
       //                       }
 
       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
-      editor.setFinalCaretOffset(fTemplate.length()+start);
-   //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
+      switch (fLocation.getType()) {
+        case PHPIdentifierLocation.FUNCTION :
+          editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
+          break;
+        case PHPIdentifierLocation.CONSTRUCTOR :
+          editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
+          break;
+        case PHPIdentifierLocation.METHOD :
+          editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
+          break;
+
+        default :
+          editor.setFinalCaretOffset(fIdentifierName.length() + start);
+      }
       editor.enter();
 
       fSelectedRegion = editor.getSelectedRegion();
@@ -110,114 +133,52 @@ public class DeclarationProposal implements IPHPCompletionProposal {
     //     }       
   }
 
-//  private static int getCaretOffset(TemplateBuffer buffer) {
-//    TemplatePosition[] variables = buffer.getVariables();
-//    for (int i = 0; i != variables.length; i++) {
-//      TemplatePosition variable = variables[i];
-//
-//      if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
-//        return variable.getOffsets()[0];
-//    }
-//
-//    return buffer.getString().length();
-//  }
-
   /*
-   * @see ICompletionProposal#getSelection(IDocument)
+   * @see ICompletionProposal#getAdditionalProposalInfo()
    */
-  public Point getSelection(IDocument document) {
-    return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
-    //   return null;
+  public String getAdditionalProposalInfo() {
+    StringBuffer hoverInfoBuffer = new StringBuffer();
+    String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
+    String filename = workspaceLocation + fLocation.getFilename();
+    PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
+    return textToHTML(hoverInfoBuffer.toString());
   }
 
   /*
-   * @see ICompletionProposal#getAdditionalProposalInfo()
+   * @see ICompletionProposal#getContextInformation()
    */
-  public String getAdditionalProposalInfo() {
-    //     try {
-    //                 if (fTemplateBuffer == null)
-    //                         fTemplateBuffer= fContext.evaluate(fTemplate);
-
-    return textToHTML(fTemplate); // fTemplateBuffer.getString());
-
-    //     } catch (CoreException e) {
-    //                 handleException(e);                 
-    //                 return null;
-    //     }
+  public IContextInformation getContextInformation() {
+    return null;
   }
 
   /*
    * @see ICompletionProposal#getDisplayString()
    */
   public String getDisplayString() {
-    return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
-    //         return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
+    String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
+    String filename = workspaceLocation + fLocation.getFilename();
+    return fIdentifierName + TemplateMessages.getString("TemplateProposal.delimiter") + PHPDocUtil.getUsage(filename, fLocation) + TemplateMessages.getString("TemplateProposal.delimiter") + filename; // $NON-NLS-1$ //$NON-NLS-1$
   }
 
   /*
    * @see ICompletionProposal#getImage()
    */
   public Image getImage() {
-    if (fTemplate.charAt(0)=='$') {
-      return fImage_var;
-    }
-               return fImage_fun;
-  }
-
-  /*
-   * @see ICompletionProposal#getContextInformation()
-   */
-  public IContextInformation getContextInformation() {
-    return null;
-  }
-
-  private static String textToHTML(String string) {
-    StringBuffer buffer = new StringBuffer(string.length());
-    buffer.append("<pre>"); //$NON-NLS-1$
-
-    for (int i = 0; i != string.length(); i++) {
-      char ch = string.charAt(i);
-
-      switch (ch) {
-        case '&' :
-          buffer.append("&amp;"); //$NON-NLS-1$
-          break;
-
-        case '<' :
-          buffer.append("&lt;"); //$NON-NLS-1$
-          break;
-
-        case '>' :
-          buffer.append("&gt;"); //$NON-NLS-1$
-          break;
-
-        case '\t' :
-          buffer.append("    "); //$NON-NLS-1$
-          break;
-
-        case '\n' :
-          buffer.append("<br>"); //$NON-NLS-1$
-          break;
-
-        default :
-          buffer.append(ch);
-          break;
-      }
+    switch (fLocation.getType()) {
+      case PHPIdentifierLocation.FUNCTION :
+        return PHPUiImages.get(PHPUiImages.IMG_FUN);
+      case PHPIdentifierLocation.CLASS :
+        return PHPUiImages.get(PHPUiImages.IMG_CLASS);
+      case PHPIdentifierLocation.CONSTRUCTOR :
+        return PHPUiImages.get(PHPUiImages.IMG_CLASS);
+      case PHPIdentifierLocation.METHOD :
+        return PHPUiImages.get(PHPUiImages.IMG_FUN);
+      case PHPIdentifierLocation.DEFINE :
+        return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
+      case PHPIdentifierLocation.VARIABLE :
+        return PHPUiImages.get(PHPUiImages.IMG_VAR);
     }
-
-    buffer.append("</pre>"); //$NON-NLS-1$
-    return buffer.toString();
-  }
-
-  private void openErrorDialog(BadLocationException e) {
-    Shell shell = fViewer.getTextWidget().getShell();
-    MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
-  }
-
-  private void handleException(CoreException e) {
-    Shell shell = fViewer.getTextWidget().getShell();
-    PHPeclipsePlugin.log(e);
-    //         ExceptionHandler.handle(e, shell, ObfuscatorMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
+    return PHPUiImages.get(PHPUiImages.IMG_FUN);
   }
 
   /*
@@ -234,7 +195,9 @@ public class DeclarationProposal implements IPHPCompletionProposal {
         case '\n' :
         case '\t' :
           return 90;
-
+        case '>' : // ->
+        case ':' : // ::
+          return 95;
         default :
           return 0;
       }