2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocUtil;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
9 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
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.builder.PHPIdentifierLocation;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.contentassist.ContextInformation;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.jface.text.templates.TemplateContext;
24 import org.eclipse.swt.graphics.Image;
26 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
27 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
28 // import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
31 * A PHP identifier proposal.
33 public class DeclarationProposal extends AbstractProposal { // implements
34 // IPHPCompletionProposal
36 private IProject fProject;
38 private final TemplateContext fContext;
40 private final PHPIdentifierLocation fLocation;
44 // private TemplateBuffer fTemplateBuffer;
45 // private String fOldText;
46 // private final Image fImage_fun;
47 // private final Image fImage_var;
48 private final IRegion fRegion;
50 // private IRegion fSelectedRegion; // initialized by apply()
52 private final String fIdentifierName;
54 // private final ITextViewer fViewer;
57 * Creates a template proposal with a template and its context.
62 * the context in which the template was requested.
64 * the icon of the proposal.
66 public DeclarationProposal(IProject project, String identifierName, PHPIdentifierLocation location, TemplateContext context,
67 IRegion region, ITextViewer viewer) {
72 fIdentifierName = identifierName;
80 * @see ICompletionProposal#apply(IDocument)
82 public void apply(IDocument document) {
84 // if (fTemplateBuffer == null)
85 // fTemplateBuffer= fContext.evaluate(fTemplate);
87 int start = fRegion.getOffset();
88 int end = fRegion.getOffset() + fRegion.getLength();
90 switch (fLocation.getType()) {
91 case PHPIdentifierLocation.FUNCTION:
92 document.replace(start, end - start, fIdentifierName + "()");
94 case PHPIdentifierLocation.CONSTRUCTOR:
95 document.replace(start, end - start, fIdentifierName + "()");
97 case PHPIdentifierLocation.METHOD:
98 document.replace(start, end - start, fIdentifierName + "()");
102 document.replace(start, end - start, fIdentifierName);
105 // translate positions
106 LinkedPositionManager manager = new LinkedPositionManager(document);
107 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
108 // for (int i= 0; i != variables.length; i++) {
109 // TemplatePosition variable= variables[i];
111 // if (variable.isResolved())
114 // int[] offsets= variable.getOffsets();
115 // int length= variable.getLength();
117 // for (int j= 0; j != offsets.length; j++)
118 // manager.addPosition(offsets[j] + start, length);
121 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
122 switch (fLocation.getType()) {
123 case PHPIdentifierLocation.FUNCTION:
124 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
126 case PHPIdentifierLocation.CONSTRUCTOR:
127 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
129 case PHPIdentifierLocation.METHOD:
130 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
134 editor.setFinalCaretOffset(fIdentifierName.length() + start);
138 fSelectedRegion = editor.getSelectedRegion();
140 } catch (BadLocationException e) {
141 PHPeclipsePlugin.log(e);
145 // catch (CoreException e) {
146 // handleException(e);
151 * @see ICompletionProposal#getAdditionalProposalInfo()
153 public String getAdditionalProposalInfo() {
155 fInfo = computeProposalInfo();
160 private String computeProposalInfo() {
161 StringBuffer hoverInfoBuffer = new StringBuffer();
162 // String workspaceLocation =
163 // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
164 String workspaceLocation;
165 if (fProject != null) {
166 workspaceLocation = fProject.getLocation().toString() + '/';
168 // should never happen?
169 workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
171 String filename = workspaceLocation + fLocation.getFilename();
172 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
173 return hoverInfoBuffer.toString();
176 public IContextInformation getContextInformation() {
177 if (fContextInfo == null) {
178 if (fLocation != null) {
179 fInfo = fLocation.getUsage();
181 // extract the parameter context information for the function:
182 int i0 = fInfo.indexOf('(');
183 int newline = fInfo.indexOf('\n');
184 if (i0 >= 0 && (i0 < newline || newline < 0)) {
185 int i1 = fInfo.indexOf(')', i0 + 1);
188 fContextInfo = new ContextInformation(null, fInfo.substring(i0+1, i1));
190 fContextInfo = new ContextInformation(null, fInfo);
193 fContextInfo = new ContextInformation(null, fInfo);
202 * @see ICompletionProposal#getDisplayString()
204 public String getDisplayString() {
205 String workspaceLocation;
206 if (fProject != null) {
207 workspaceLocation = fProject.getName().toString() + '/';
209 // should never happen?
210 workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
212 String filename = workspaceLocation + fLocation.getFilename();
213 String usage = PHPDocUtil.getUsage(filename, fLocation);
214 String result = fIdentifierName + TemplateMessages.getString("TemplateProposal.delimiter");
215 if (usage.length() > 0) {
216 result += usage + TemplateMessages.getString("TemplateProposal.delimiter");
223 * @see ICompletionProposal#getImage()
225 public Image getImage() {
226 switch (fLocation.getType()) {
227 case PHPIdentifierLocation.FUNCTION:
228 return PHPUiImages.get(PHPUiImages.IMG_FUN);
229 case PHPIdentifierLocation.CLASS:
230 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
231 case PHPIdentifierLocation.CONSTRUCTOR:
232 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
233 case PHPIdentifierLocation.METHOD:
234 return PHPUiImages.get(PHPUiImages.IMG_FUN);
235 case PHPIdentifierLocation.DEFINE:
236 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
237 case PHPIdentifierLocation.VARIABLE:
238 return PHPUiImages.get(PHPUiImages.IMG_VAR);
239 case PHPIdentifierLocation.GLOBAL_VARIABLE:
240 return PHPUiImages.get(PHPUiImages.IMG_VAR);
242 return PHPUiImages.get(PHPUiImages.IMG_FUN);
246 * @see IJavaCompletionProposal#getRelevance()
248 public int getRelevance() {
250 if (fContext instanceof JavaContext) {
251 JavaContext context = (JavaContext) fContext;
252 switch (context.getCharacterBeforeStart()) {
253 // high relevance after whitespace