2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
9 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
10 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
11 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
12 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
13 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
14 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
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.IContextInformation;
21 import org.eclipse.swt.graphics.Image;
22 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
23 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
24 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
27 * A PHP identifier proposal.
29 public class BuiltInProposal extends AbstractProposal { // implements IPHPCompletionProposal {
30 private final TemplateContext fContext;
31 private final PHPFunction fFunction;
33 //private TemplateBuffer fTemplateBuffer;
34 private String fOldText;
35 // private final Image fImage_builtin;
37 private final IRegion fRegion;
38 // private IRegion fSelectedRegion; // initialized by apply()
40 private final String fBuiltinFunctionName;
41 // private final ITextViewer fViewer;
44 * Creates a template proposal with a template and its context.
45 * @param template the template
46 * @param context the context in which the template was requested.
47 * @param image the icon of the proposal.
49 public BuiltInProposal(String functionName, PHPFunction function, TemplateContext context, IRegion region, ITextViewer viewer) {
51 fBuiltinFunctionName = functionName;
59 * @see ICompletionProposal#apply(IDocument)
61 public void apply(IDocument document) {
63 // if (fTemplateBuffer == null)
64 // fTemplateBuffer= fContext.evaluate(fTemplate);
66 int start = fRegion.getOffset();
67 int end = fRegion.getOffset() + fRegion.getLength();
69 // insert template string
70 // String templateString = fTemplate; // fTemplateBuffer.getString();
71 document.replace(start, end - start, fBuiltinFunctionName + "()");
73 // translate positions
74 LinkedPositionManager manager = new LinkedPositionManager(document);
75 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
76 // for (int i= 0; i != variables.length; i++) {
77 // TemplatePosition variable= variables[i];
79 // if (variable.isResolved())
82 // int[] offsets= variable.getOffsets();
83 // int length= variable.getLength();
85 // for (int j= 0; j != offsets.length; j++)
86 // manager.addPosition(offsets[j] + start, length);
89 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
90 editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start + 1);
91 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
94 fSelectedRegion = editor.getSelectedRegion();
96 } catch (BadLocationException e) {
97 PHPeclipsePlugin.log(e);
101 // catch (CoreException e) {
102 // handleException(e);
107 * @see ICompletionProposal#getAdditionalProposalInfo()
109 public String getAdditionalProposalInfo() {
110 return textToHTML(fFunction.getDescription()); // fTemplateBuffer.getString());
114 * @see ICompletionProposal#getContextInformation()
116 public IContextInformation getContextInformation() {
121 * @see ICompletionProposal#getDisplayString()
123 public String getDisplayString() {
124 return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$ //$NON-NLS-1$
125 // return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
129 * @see ICompletionProposal#getImage()
131 public Image getImage() {
132 return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
136 * @see IJavaCompletionProposal#getRelevance()
138 public int getRelevance() {
140 if (fContext instanceof PHPUnitContext) {
141 PHPUnitContext context = (PHPUnitContext) fContext;
142 switch (context.getCharacterBeforeStart()) {
143 // high relevance after whitespace