Fix Request #1431769: Support Context assistance when entering functions arguments
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / BuiltInProposal.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.template;
6
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
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;
23
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;
27
28 /**
29  * A PHP identifier proposal.
30  */
31 public class BuiltInProposal extends AbstractProposal {
32         private final TemplateContext fContext;
33
34         private final PHPFunction fFunction;
35
36         private final IRegion fRegion;
37
38         private final String fBuiltinFunctionName;
39
40         /**
41          * Creates a template proposal with a template and its context.
42          *
43          * @param template
44          *          the template
45          * @param context
46          *          the context in which the template was requested.
47          * @param image
48          *          the icon of the proposal.
49          */
50         public BuiltInProposal(String functionName, PHPFunction function, TemplateContext context, IRegion region, ITextViewer viewer) {
51                 super(viewer);
52                 fBuiltinFunctionName = functionName;
53                 fFunction = function;
54                 fContext = context;
55                 // fViewer = viewer;
56                 fRegion = region;
57         }
58
59         /*
60          * @see ICompletionProposal#apply(IDocument)
61          */
62         public void apply(IDocument document) {
63                 try {
64                         // if (fTemplateBuffer == null)
65                         // fTemplateBuffer= fContext.evaluate(fTemplate);
66
67                         int start = fRegion.getOffset();
68                         int end = fRegion.getOffset() + fRegion.getLength();
69
70                         // insert template string
71                         // String templateString = fTemplate; // fTemplateBuffer.getString();
72                         document.replace(start, end - start, fBuiltinFunctionName + "()");
73
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];
79                         //
80                         // if (variable.isResolved())
81                         // continue;
82                         //
83                         // int[] offsets= variable.getOffsets();
84                         // int length= variable.getLength();
85                         //
86                         // for (int j= 0; j != offsets.length; j++)
87                         // manager.addPosition(offsets[j] + start, length);
88                         // }
89
90                         LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
91                         editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start + 1);
92                         // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
93                         editor.enter();
94
95                         fSelectedRegion = editor.getSelectedRegion();
96
97                 } catch (BadLocationException e) {
98                         PHPeclipsePlugin.log(e);
99                         openErrorDialog(e);
100
101                 }
102                 // catch (CoreException e) {
103                 // handleException(e);
104                 // }
105         }
106
107         /*
108          * @see ICompletionProposal#getAdditionalProposalInfo()
109          */
110         public String getAdditionalProposalInfo() {
111                 return fFunction.getHoverText();
112         }
113
114         public IContextInformation getContextInformation() {
115                 if (fContextInfo == null) {
116                         String contextInfoString = fFunction.getHoverText();
117                         if (contextInfoString != null && contextInfoString.length() > 0) {
118                                 // extract the parameter context information for the function:
119                                 int i0 = contextInfoString.indexOf('(');
120                                 if (i0 >= 0) {
121                                         int i1 = contextInfoString.indexOf(')', i0 + 1);
122                                         if (i1 > 0) {
123                                                 fContextInfo = new ContextInformation(null, contextInfoString.substring(i0, i1+1));
124                                         } else {
125                                                 fContextInfo = new ContextInformation(null, contextInfoString);
126                                         }
127                                 } else {
128                                         fContextInfo = new ContextInformation(null, contextInfoString);
129                                 }
130                         }
131                 }
132                 return fContextInfo;
133         }
134
135         /*
136          * @see ICompletionProposal#getDisplayString()
137          */
138         public String getDisplayString() {
139                 return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$
140                 // //$NON-NLS-1$
141         }
142
143         /*
144          * @see ICompletionProposal#getImage()
145          */
146         public Image getImage() {
147                 return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
148         }
149
150         /*
151          * @see IJavaCompletionProposal#getRelevance()
152          */
153         public int getRelevance() {
154
155                 if (fContext instanceof JavaContext) {
156                         JavaContext context = (JavaContext) fContext;
157                         switch (context.getCharacterBeforeStart()) {
158                         // high relevance after whitespace
159                         case ' ':
160                         case '\r':
161                         case '\n':
162                         case '\t':
163                                 return 50;
164
165                         default:
166                                 return 0;
167                         }
168                 } else {
169                         return 50;
170                 }
171         }
172
173 }