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;
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.IRegion;
18 import org.eclipse.jface.text.ITextViewer;
19 import org.eclipse.jface.text.contentassist.ContextInformation;
20 import org.eclipse.jface.text.contentassist.IContextInformation;
21 import org.eclipse.jface.text.templates.TemplateContext;
22 import org.eclipse.swt.graphics.Image;
24 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
25 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
26 // import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
29 * A PHP identifier proposal.
31 public class BuiltInProposal extends AbstractProposal {
32 private final TemplateContext fContext;
34 private final PHPFunction fFunction;
36 private final IRegion fRegion;
38 private final String fBuiltinFunctionName;
41 * Creates a template proposal with a template and its context.
46 * the context in which the template was requested.
48 * the icon of the proposal.
50 public BuiltInProposal(String functionName, PHPFunction function, TemplateContext context, IRegion region, ITextViewer viewer) {
52 fBuiltinFunctionName = functionName;
60 * @see ICompletionProposal#apply(IDocument)
62 public void apply(IDocument document) {
64 // if (fTemplateBuffer == null)
65 // fTemplateBuffer= fContext.evaluate(fTemplate);
67 int start = fRegion.getOffset();
68 int end = fRegion.getOffset() + fRegion.getLength();
70 // insert template string
71 // String templateString = fTemplate; // fTemplateBuffer.getString();
72 document.replace(start, end - start, fBuiltinFunctionName + "()");
74 // translate positions
75 LinkedPositionManager manager = new LinkedPositionManager(document);
76 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
77 // for (int i= 0; i != variables.length; i++) {
78 // TemplatePosition variable= variables[i];
80 // if (variable.isResolved())
83 // int[] offsets= variable.getOffsets();
84 // int length= variable.getLength();
86 // for (int j= 0; j != offsets.length; j++)
87 // manager.addPosition(offsets[j] + start, length);
90 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
91 editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start + 1);
92 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
95 fSelectedRegion = editor.getSelectedRegion();
97 } catch (BadLocationException e) {
98 PHPeclipsePlugin.log(e);
102 // catch (CoreException e) {
103 // handleException(e);
107 public String getAdditionalProposalInfo() {
108 return fFunction.getHoverText();
111 public IContextInformation getContextInformation() {
112 if (fContextInfo == null) {
113 String contextInfoString = fFunction.getHoverText();
114 if (contextInfoString != null && contextInfoString.length() > 0) {
115 // extract the parameter context information for the function:
116 int i0 = contextInfoString.indexOf('(');
117 int newline = contextInfoString.indexOf('\n');
118 if (i0 >= 0 && (i0 < newline || newline < 0)) {
119 int i1 = contextInfoString.indexOf(')', i0 + 1);
121 fContextInfo = new ContextInformation(null, contextInfoString.substring(i0+1, i1));
123 fContextInfo = new ContextInformation(null, contextInfoString);
126 fContextInfo = new ContextInformation(null, contextInfoString);
134 * @see ICompletionProposal#getDisplayString()
136 public String getDisplayString() {
137 return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$
142 * @see ICompletionProposal#getImage()
144 public Image getImage() {
145 return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
149 * @see IJavaCompletionProposal#getRelevance()
151 public int getRelevance() {
153 if (fContext instanceof JavaContext) {
154 JavaContext context = (JavaContext) fContext;
155 switch (context.getCharacterBeforeStart()) {
156 // high relevance after whitespace