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         public String getAdditionalProposalInfo() {
108                 return fFunction.getHoverText();
109         }
110
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);
120                                         if (i1 > 0) {
121                                                 fContextInfo = new ContextInformation(null, contextInfoString.substring(i0+1, i1));
122                                         } else {
123                                                 fContextInfo = new ContextInformation(null, contextInfoString);
124                                         }
125                                 } else {
126                                         fContextInfo = new ContextInformation(null, contextInfoString);
127                                 }
128                         }
129                 }
130                 return fContextInfo;
131         }
132
133         /*
134          * @see ICompletionProposal#getDisplayString()
135          */
136         public String getDisplayString() {
137                 return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$
138                 // //$NON-NLS-1$
139         }
140
141         /*
142          * @see ICompletionProposal#getImage()
143          */
144         public Image getImage() {
145                 return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
146         }
147
148         /*
149          * @see IJavaCompletionProposal#getRelevance()
150          */
151         public int getRelevance() {
152
153                 if (fContext instanceof JavaContext) {
154                         JavaContext context = (JavaContext) fContext;
155                         switch (context.getCharacterBeforeStart()) {
156                         // high relevance after whitespace
157                         case ' ':
158                         case '\r':
159                         case '\n':
160                         case '\t':
161                                 return 50;
162
163                         default:
164                                 return 0;
165                         }
166                 } else {
167                         return 50;
168                 }
169         }
170
171 }