2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
6 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
7 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
8 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
9 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
10 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
11 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
12 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.contentassist.IContextInformation;
19 import org.eclipse.jface.text.templates.TemplateContext;
20 import org.eclipse.swt.graphics.Image;
21 //import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
22 //import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
23 //import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
26 * A PHP identifier proposal.
28 public class BuiltInProposal extends AbstractProposal { // implements IPHPCompletionProposal {
29 private final TemplateContext fContext;
30 private final PHPFunction fFunction;
32 private String fOldText;
34 private final IRegion fRegion;
36 private final String fBuiltinFunctionName;
39 * Creates a template proposal with a template and its context.
40 * @param template the template
41 * @param context the context in which the template was requested.
42 * @param image the icon of the proposal.
44 public BuiltInProposal(String functionName, PHPFunction function, TemplateContext context, IRegion region, ITextViewer viewer) {
46 fBuiltinFunctionName = functionName;
54 * @see ICompletionProposal#apply(IDocument)
56 public void apply(IDocument document) {
58 // if (fTemplateBuffer == null)
59 // fTemplateBuffer= fContext.evaluate(fTemplate);
61 int start = fRegion.getOffset();
62 int end = fRegion.getOffset() + fRegion.getLength();
64 // insert template string
65 // String templateString = fTemplate; // fTemplateBuffer.getString();
66 document.replace(start, end - start, fBuiltinFunctionName + "()");
68 // translate positions
69 LinkedPositionManager manager = new LinkedPositionManager(document);
70 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
71 // for (int i= 0; i != variables.length; i++) {
72 // TemplatePosition variable= variables[i];
74 // if (variable.isResolved())
77 // int[] offsets= variable.getOffsets();
78 // int length= variable.getLength();
80 // for (int j= 0; j != offsets.length; j++)
81 // manager.addPosition(offsets[j] + start, length);
84 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
85 editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start + 1);
86 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
89 fSelectedRegion = editor.getSelectedRegion();
91 } catch (BadLocationException e) {
92 PHPeclipsePlugin.log(e);
96 // catch (CoreException e) {
97 // handleException(e);
102 * @see ICompletionProposal#getAdditionalProposalInfo()
104 public String getAdditionalProposalInfo() {
105 return textToHTML(fFunction.getDescription());
109 * @see ICompletionProposal#getContextInformation()
111 public IContextInformation getContextInformation() {
116 * @see ICompletionProposal#getDisplayString()
118 public String getDisplayString() {
119 return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$ //$NON-NLS-1$
123 * @see ICompletionProposal#getImage()
125 public Image getImage() {
126 return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
130 * @see IJavaCompletionProposal#getRelevance()
132 public int getRelevance() {
134 if (fContext instanceof JavaContext) {
135 JavaContext context = (JavaContext) fContext;
136 switch (context.getCharacterBeforeStart()) {
137 // high relevance after whitespace