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,
51 TemplateContext context, IRegion region, ITextViewer viewer) {
53 fBuiltinFunctionName = functionName;
61 * @see ICompletionProposal#apply(IDocument)
63 public void apply(IDocument document) {
65 // if (fTemplateBuffer == null)
66 // fTemplateBuffer= fContext.evaluate(fTemplate);
68 int start = fRegion.getOffset();
69 int end = fRegion.getOffset() + fRegion.getLength();
71 // insert template string
72 // String templateString = fTemplate; //
73 // fTemplateBuffer.getString();
74 document.replace(start, end - start, fBuiltinFunctionName + "()");
76 // translate positions
77 LinkedPositionManager manager = new LinkedPositionManager(document);
78 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
79 // for (int i= 0; i != variables.length; i++) {
80 // TemplatePosition variable= variables[i];
82 // if (variable.isResolved())
85 // int[] offsets= variable.getOffsets();
86 // int length= variable.getLength();
88 // for (int j= 0; j != offsets.length; j++)
89 // manager.addPosition(offsets[j] + start, length);
92 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
93 editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start
95 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) +
99 fSelectedRegion = editor.getSelectedRegion();
101 } catch (BadLocationException e) {
102 PHPeclipsePlugin.log(e);
106 // catch (CoreException e) {
107 // handleException(e);
111 public String getAdditionalProposalInfo() {
112 return fFunction.getHoverText();
115 public IContextInformation getContextInformation() {
116 if (fContextInfo == null) {
117 String contextInfoString = fFunction.getHoverText();
118 if (contextInfoString != null && contextInfoString.length() > 0) {
119 // extract the parameter context information for the function:
120 int i0 = contextInfoString.indexOf('(');
121 int newline = contextInfoString.indexOf('\n');
122 if (i0 >= 0 && (i0 < newline || newline < 0)) {
123 int i1 = contextInfoString.indexOf(')', i0 + 1);
125 fContextInfo = new ContextInformation(null,
126 contextInfoString.substring(i0 + 1, i1));
128 fContextInfo = new ContextInformation(null,
132 fContextInfo = new ContextInformation(null,
141 * @see ICompletionProposal#getDisplayString()
143 public String getDisplayString() {
144 return fBuiltinFunctionName
145 + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$
150 * @see ICompletionProposal#getImage()
152 public Image getImage() {
153 return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
157 * @see IJavaCompletionProposal#getRelevance()
159 public int getRelevance() {
161 if (fContext instanceof JavaContext) {
162 JavaContext context = (JavaContext) fContext;
163 switch (context.getCharacterBeforeStart()) {
164 // high relevance after whitespace