From: axelcl <axelcl>
Date: Wed, 22 Feb 2006 22:06:40 +0000 (+0000)
Subject: Fix Request #1431769: Support Context assistance when entering functions arguments
X-Git-Url: http://secure.phpeclipse.com

Fix Request #1431769: Support Context assistance when entering functions arguments
Now available for built-in funcitons
---

diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/AbstractProposal.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/AbstractProposal.java
index f26f6a8..51773b9 100644
--- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/AbstractProposal.java
+++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/AbstractProposal.java
@@ -14,59 +14,67 @@ 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.ContextInformation;
+import org.eclipse.jface.text.contentassist.IContextInformation;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.widgets.Shell;
-//import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
-//import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
-//import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
+
+// import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
+// import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
+// import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
 
 /**
  * A PHP identifier proposal.
  */
 public abstract class AbstractProposal implements IPHPCompletionProposal {
 	protected IRegion fSelectedRegion; // initialized by apply()
+
 	protected final ITextViewer fViewer;
-	
+
+	protected ContextInformation fContextInfo;
+
 	public AbstractProposal(ITextViewer viewer) {
+		fContextInfo = null;
 		fViewer = viewer;
 	}
+
 	protected static String textToHTML(String string) {
-    StringBuffer buffer = new StringBuffer(string.length());
-    buffer.append("<pre>"); //$NON-NLS-1$
+		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);
+		for (int i = 0; i != string.length(); i++) {
+			char ch = string.charAt(i);
 
-      switch (ch) {
-        case '&' :
-          buffer.append("&amp;"); //$NON-NLS-1$
-          break;
+			switch (ch) {
+			case '&':
+				buffer.append("&amp;"); //$NON-NLS-1$
+				break;
 
-        case '<' :
-          buffer.append("&lt;"); //$NON-NLS-1$
-          break;
+			case '<':
+				buffer.append("&lt;"); //$NON-NLS-1$
+				break;
 
-        case '>' :
-          buffer.append("&gt;"); //$NON-NLS-1$
-          break;
+			case '>':
+				buffer.append("&gt;"); //$NON-NLS-1$
+				break;
 
-        case '\t' :
-          buffer.append("    "); //$NON-NLS-1$
-          break;
+			case '\t':
+				buffer.append("    "); //$NON-NLS-1$
+				break;
 
-        case '\n' :
-          buffer.append("<br>"); //$NON-NLS-1$
-          break;
+			case '\n':
+				buffer.append("<br>"); //$NON-NLS-1$
+				break;
 
-        default :
-          buffer.append(ch);
-          break;
-      }
-    }
+			default:
+				buffer.append(ch);
+				break;
+			}
+		}
 
-    buffer.append("</pre>"); //$NON-NLS-1$
-    return buffer.toString();
-  }
+		buffer.append("</pre>"); //$NON-NLS-1$
+		return buffer.toString();
+	}
 
 	/*
 	 * @see ICompletionProposal#getSelection(IDocument)
@@ -78,11 +86,18 @@ public abstract class AbstractProposal implements IPHPCompletionProposal {
 	protected 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$
+		// ExceptionHandler.handle(e, shell,
+		// ObfuscatorMessages.getString("TemplateEvaluator.error.title"), null);
+		// //$NON-NLS-1$
 	}
 
 	protected void openErrorDialog(BadLocationException e) {
 		Shell shell = fViewer.getTextWidget().getShell();
 		MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
 	}
+
+	public IContextInformation getContextInformation() {
+		return null;
+	}
+
 }
\ No newline at end of file
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/BuiltInProposal.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/BuiltInProposal.java
index 7c9570c..daafd3d 100644
--- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/BuiltInProposal.java
+++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/BuiltInProposal.java
@@ -3,6 +3,7 @@
  * All Rights Reserved.
  */
 package net.sourceforge.phpdt.internal.ui.text.template;
+
 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
@@ -15,138 +16,158 @@ 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.ContextInformation;
 import org.eclipse.jface.text.contentassist.IContextInformation;
 import org.eclipse.jface.text.templates.TemplateContext;
 import org.eclipse.swt.graphics.Image;
-//import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
-//import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
-//import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
+
+// import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
+// import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
+// import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
 
 /**
  * A PHP identifier proposal.
  */
-public class BuiltInProposal extends AbstractProposal { // implements IPHPCompletionProposal {
-  private final TemplateContext fContext;
-  private final PHPFunction fFunction;
-
-  private String fOldText;
-
-  private final IRegion fRegion;
-
-  private final String fBuiltinFunctionName;
-
-  /**
-   * Creates a template proposal with a template and its context.
-   * @param template  the template
-   * @param context   the context in which the template was requested.
-   * @param image     the icon of the proposal.
-   */
-  public BuiltInProposal(String functionName, PHPFunction function, TemplateContext context, IRegion region, ITextViewer viewer) {
-    super(viewer);
-    fBuiltinFunctionName = functionName;
-    fFunction = function;
-    fContext = context;
-    //    fViewer = viewer;
-    fRegion = region;
-  }
-
-  /*
-   * @see ICompletionProposal#apply(IDocument)
-   */
-  public void apply(IDocument document) {
-    try {
-      //		    if (fTemplateBuffer == null)
-      //				fTemplateBuffer= fContext.evaluate(fTemplate);
-
-      int start = fRegion.getOffset();
-      int end = fRegion.getOffset() + fRegion.getLength();
-
-      // insert template string
-      //  String templateString = fTemplate; // fTemplateBuffer.getString();	
-      document.replace(start, end - start, fBuiltinFunctionName + "()");
-
-      // translate positions
-      LinkedPositionManager manager = new LinkedPositionManager(document);
-      //			TemplatePosition[] variables= fTemplateBuffer.getVariables();
-      //			for (int i= 0; i != variables.length; i++) {
-      //				TemplatePosition variable= variables[i];
-      //
-      //				if (variable.isResolved())
-      //					continue;
-      //				
-      //				int[] offsets= variable.getOffsets();
-      //				int length= variable.getLength();
-      //				
-      //				for (int j= 0; j != offsets.length; j++)
-      //					manager.addPosition(offsets[j] + start, length);
-      //			}
-
-      LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
-      editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start + 1);
-      //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
-      editor.enter();
-
-      fSelectedRegion = editor.getSelectedRegion();
-
-    } catch (BadLocationException e) {
-      PHPeclipsePlugin.log(e);
-      openErrorDialog(e);
-
-    }
-    //      catch (CoreException e) {
-    //			handleException(e);
-    //	    }	    
-  }
-
-  /*
-   * @see ICompletionProposal#getAdditionalProposalInfo()
-   */
-  public String getAdditionalProposalInfo() {
-    return textToHTML(fFunction.getDescription()); 
-  }
-
-  /*
-   * @see ICompletionProposal#getContextInformation()
-   */
-  public IContextInformation getContextInformation() {
-    return null;
-  }
-
-  /*
-   * @see ICompletionProposal#getDisplayString()
-   */
-  public String getDisplayString() {
-    return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$ //$NON-NLS-1$
-  }
-
-  /*
-   * @see ICompletionProposal#getImage()
-   */
-  public Image getImage() {
-    return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
-  }
-
-  /*
-   * @see IJavaCompletionProposal#getRelevance()
-   */
-  public int getRelevance() {
-
-    if (fContext instanceof JavaContext) {
-      JavaContext context = (JavaContext) fContext;
-      switch (context.getCharacterBeforeStart()) {
-        // high relevance after whitespace
-        case ' ' :
-        case '\r' :
-        case '\n' :
-        case '\t' :
-          return 50;
-
-        default :
-          return 0;
-      }
-    } else {
-      return 50;
-    }
-  }
+public class BuiltInProposal extends AbstractProposal {
+	private final TemplateContext fContext;
+
+	private final PHPFunction fFunction;
+
+	private final IRegion fRegion;
+
+	private final String fBuiltinFunctionName;
+
+	/**
+	 * Creates a template proposal with a template and its context.
+	 *
+	 * @param template
+	 *          the template
+	 * @param context
+	 *          the context in which the template was requested.
+	 * @param image
+	 *          the icon of the proposal.
+	 */
+	public BuiltInProposal(String functionName, PHPFunction function, TemplateContext context, IRegion region, ITextViewer viewer) {
+		super(viewer);
+		fBuiltinFunctionName = functionName;
+		fFunction = function;
+		fContext = context;
+		// fViewer = viewer;
+		fRegion = region;
+	}
+
+	/*
+	 * @see ICompletionProposal#apply(IDocument)
+	 */
+	public void apply(IDocument document) {
+		try {
+			// if (fTemplateBuffer == null)
+			// fTemplateBuffer= fContext.evaluate(fTemplate);
+
+			int start = fRegion.getOffset();
+			int end = fRegion.getOffset() + fRegion.getLength();
+
+			// insert template string
+			// String templateString = fTemplate; // fTemplateBuffer.getString();
+			document.replace(start, end - start, fBuiltinFunctionName + "()");
+
+			// translate positions
+			LinkedPositionManager manager = new LinkedPositionManager(document);
+			// TemplatePosition[] variables= fTemplateBuffer.getVariables();
+			// for (int i= 0; i != variables.length; i++) {
+			// TemplatePosition variable= variables[i];
+			//
+			// if (variable.isResolved())
+			// continue;
+			//
+			// int[] offsets= variable.getOffsets();
+			// int length= variable.getLength();
+			//
+			// for (int j= 0; j != offsets.length; j++)
+			// manager.addPosition(offsets[j] + start, length);
+			// }
+
+			LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
+			editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start + 1);
+			// editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
+			editor.enter();
+
+			fSelectedRegion = editor.getSelectedRegion();
+
+		} catch (BadLocationException e) {
+			PHPeclipsePlugin.log(e);
+			openErrorDialog(e);
+
+		}
+		// catch (CoreException e) {
+		// handleException(e);
+		// }
+	}
+
+	/*
+	 * @see ICompletionProposal#getAdditionalProposalInfo()
+	 */
+	public String getAdditionalProposalInfo() {
+		return fFunction.getHoverText();
+	}
+
+	public IContextInformation getContextInformation() {
+		if (fContextInfo == null) {
+			String contextInfoString = fFunction.getHoverText();
+			if (contextInfoString != null && contextInfoString.length() > 0) {
+				// extract the parameter context information for the function:
+				int i0 = contextInfoString.indexOf('(');
+				if (i0 >= 0) {
+					int i1 = contextInfoString.indexOf(')', i0 + 1);
+					if (i1 > 0) {
+						fContextInfo = new ContextInformation(null, contextInfoString.substring(i0, i1+1));
+					} else {
+						fContextInfo = new ContextInformation(null, contextInfoString);
+					}
+				} else {
+					fContextInfo = new ContextInformation(null, contextInfoString);
+				}
+			}
+		}
+		return fContextInfo;
+	}
+
+	/*
+	 * @see ICompletionProposal#getDisplayString()
+	 */
+	public String getDisplayString() {
+		return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$
+		// //$NON-NLS-1$
+	}
+
+	/*
+	 * @see ICompletionProposal#getImage()
+	 */
+	public Image getImage() {
+		return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
+	}
+
+	/*
+	 * @see IJavaCompletionProposal#getRelevance()
+	 */
+	public int getRelevance() {
+
+		if (fContext instanceof JavaContext) {
+			JavaContext context = (JavaContext) fContext;
+			switch (context.getCharacterBeforeStart()) {
+			// high relevance after whitespace
+			case ' ':
+			case '\r':
+			case '\n':
+			case '\t':
+				return 50;
+
+			default:
+				return 0;
+			}
+		} else {
+			return 50;
+		}
+	}
 
 }
\ No newline at end of file
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierProposal.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierProposal.java
index ec4eef6..6ebdb62 100644
--- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierProposal.java
+++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierProposal.java
@@ -1,8 +1,5 @@
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
 package net.sourceforge.phpdt.internal.ui.text.template;
+
 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
@@ -13,19 +10,17 @@ 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.ContextInformation;
 import org.eclipse.jface.text.contentassist.IContextInformation;
 import org.eclipse.jface.text.templates.TemplateContext;
 import org.eclipse.swt.graphics.Image;
-//import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
-//import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
-//import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
+
 /**
  * A PHP identifier proposal.
  */
-public class IdentifierProposal extends AbstractProposal { //implements
-                                                           // IPHPCompletionProposal
-                                                           // {
+public class IdentifierProposal extends AbstractProposal {
   private final TemplateContext fContext;
+
   private final Image fImage_fun;
   private final Image fImage_var;
   //private TemplateBuffer fTemplateBuffer;
@@ -33,54 +28,27 @@ public class IdentifierProposal extends AbstractProposal { //implements
   private final IRegion fRegion;
   //  private IRegion fSelectedRegion; // initialized by apply()
   private final String fTemplate;
-  //  private final ITextViewer fViewer;
-  /**
-   * Creates a template proposal with a template and its context.
-   * 
-   * @param template
-   *            the template
-   * @param context
-   *            the context in which the template was requested.
-   * @param image
-   *            the icon of the proposal.
-   */
+
   public IdentifierProposal(String template, TemplateContext context,
       IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
     super(viewer);
     fTemplate = template;
     fContext = context;
+
     //    fViewer = viewer;
     fImage_fun = image_fun;
     fImage_var = image_var;
     fRegion = region;
   }
-  /*
-   * @see ICompletionProposal#apply(IDocument)
-   */
+
   public void apply(IDocument document) {
     try {
-      //		    if (fTemplateBuffer == null)
-      //				fTemplateBuffer= fContext.evaluate(fTemplate);
       int start = fRegion.getOffset();
       int end = fRegion.getOffset() + fRegion.getLength();
-      // insert template string
-      //  String templateString = fTemplate; // fTemplateBuffer.getString();
       document.replace(start, end - start, fTemplate);
       // translate positions
       LinkedPositionManager manager = new LinkedPositionManager(document);
-      //			TemplatePosition[] variables= fTemplateBuffer.getVariables();
-      //			for (int i= 0; i != variables.length; i++) {
-      //				TemplatePosition variable= variables[i];
-      //
-      //				if (variable.isResolved())
-      //					continue;
-      //				
-      //				int[] offsets= variable.getOffsets();
-      //				int length= variable.getLength();
-      //				
-      //				for (int j= 0; j != offsets.length; j++)
-      //					manager.addPosition(offsets[j] + start, length);
-      //			}
+
       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
       editor.setFinalCaretOffset(fTemplate.length() + start);
       //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
@@ -90,47 +58,25 @@ public class IdentifierProposal extends AbstractProposal { //implements
       PHPeclipsePlugin.log(e);
       openErrorDialog(e);
     }
-    //      catch (CoreException e) {
-    //			handleException(e);
-    //	    }
   }
   /*
    * @see ICompletionProposal#getAdditionalProposalInfo()
    */
   public String getAdditionalProposalInfo() {
-    //	    try {
-    //			if (fTemplateBuffer == null)
-    //				fTemplateBuffer= fContext.evaluate(fTemplate);
     return textToHTML(fTemplate); // fTemplateBuffer.getString());
-    //	    } catch (CoreException e) {
-    //			handleException(e);
-    //			return null;
-    //	    }
   }
-  /*
-   * @see ICompletionProposal#getContextInformation()
-   */
-  public IContextInformation getContextInformation() {
-    return null;
-  }
-  /*
-   * @see ICompletionProposal#getDisplayString()
-   */
+
   public String getDisplayString() {
-    return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ 
+    return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$
   }
-  /*
-   * @see ICompletionProposal#getImage()
-   */
+
   public Image getImage() {
     if (fTemplate.charAt(0) == '$') {
       return fImage_var;
     }
     return fImage_fun;
   }
-  /*
-   * @see IJavaCompletionProposal#getRelevance()
-   */
+
   public int getRelevance() {
     if (fContext instanceof JavaContext) {
       JavaContext context = (JavaContext) fContext;