2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
8 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
9 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
10 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
11 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
12 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
14 import net.sourceforge.phpeclipse.ui.WebUI;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.contentassist.ContextInformation;
21 import org.eclipse.jface.text.contentassist.IContextInformation;
22 import org.eclipse.jface.text.templates.TemplateContext;
23 import org.eclipse.swt.graphics.Image;
25 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
26 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
27 // import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
30 * A PHP identifier proposal.
32 public class BuiltInProposal extends AbstractProposal {
33 private final TemplateContext fContext;
35 private final PHPFunction fFunction;
37 private final IRegion fRegion;
39 private final String fBuiltinFunctionName;
42 * Creates a template proposal with a template and its context.
47 * the context in which the template was requested.
49 * the icon of the proposal.
51 public BuiltInProposal(String functionName, PHPFunction function,
52 TemplateContext context, IRegion region, ITextViewer viewer) {
54 fBuiltinFunctionName = functionName;
62 * @see ICompletionProposal#apply(IDocument)
64 public void apply(IDocument document) {
66 // if (fTemplateBuffer == null)
67 // fTemplateBuffer= fContext.evaluate(fTemplate);
69 int start = fRegion.getOffset();
70 int end = fRegion.getOffset() + fRegion.getLength();
72 // insert template string
73 // String templateString = fTemplate; //
74 // fTemplateBuffer.getString();
75 document.replace(start, end - start, fBuiltinFunctionName + "()");
77 // translate positions
78 LinkedPositionManager manager = new LinkedPositionManager(document);
79 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
80 // for (int i= 0; i != variables.length; i++) {
81 // TemplatePosition variable= variables[i];
83 // if (variable.isResolved())
86 // int[] offsets= variable.getOffsets();
87 // int length= variable.getLength();
89 // for (int j= 0; j != offsets.length; j++)
90 // manager.addPosition(offsets[j] + start, length);
93 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
94 editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start
96 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) +
100 fSelectedRegion = editor.getSelectedRegion();
102 } catch (BadLocationException e) {
107 // catch (CoreException e) {
108 // handleException(e);
112 public String getAdditionalProposalInfo() {
113 return fFunction.getHoverText();
116 public IContextInformation getContextInformation() {
117 if (fContextInfo == null) {
118 String contextInfoString = fFunction.getHoverText();
119 if (contextInfoString != null && contextInfoString.length() > 0) {
120 // extract the parameter context information for the function:
121 int i0 = contextInfoString.indexOf('(');
122 int newline = contextInfoString.indexOf('\n');
123 if (i0 >= 0 && (i0 < newline || newline < 0)) {
124 int i1 = contextInfoString.indexOf(')', i0 + 1);
126 fContextInfo = new ContextInformation(null,
127 contextInfoString.substring(i0 + 1, i1));
129 fContextInfo = new ContextInformation(null,
133 fContextInfo = new ContextInformation(null,
142 * @see ICompletionProposal#getDisplayString()
144 public String getDisplayString() {
145 return fBuiltinFunctionName
146 + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$
151 * @see ICompletionProposal#getImage()
153 public Image getImage() {
154 return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
158 * @see IJavaCompletionProposal#getRelevance()
160 public int getRelevance() {
162 if (fContext instanceof JavaContext) {
163 JavaContext context = (JavaContext) fContext;
164 switch (context.getCharacterBeforeStart()) {
165 // high relevance after whitespace