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,
67 PHPIdentifierLocation location, TemplateContext context,
68 IRegion region, ITextViewer viewer) {
73 fIdentifierName = identifierName;
81 * @see ICompletionProposal#apply(IDocument)
83 public void apply(IDocument document) {
85 // if (fTemplateBuffer == null)
86 // fTemplateBuffer= fContext.evaluate(fTemplate);
88 int start = fRegion.getOffset();
89 int end = fRegion.getOffset() + fRegion.getLength();
91 switch (fLocation.getType()) {
92 case PHPIdentifierLocation.FUNCTION:
93 document.replace(start, end - start, fIdentifierName + "()");
95 case PHPIdentifierLocation.CONSTRUCTOR:
96 document.replace(start, end - start, fIdentifierName + "()");
98 case PHPIdentifierLocation.METHOD:
99 document.replace(start, end - start, fIdentifierName + "()");
103 document.replace(start, end - start, fIdentifierName);
106 // translate positions
107 LinkedPositionManager manager = new LinkedPositionManager(document);
108 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
109 // for (int i= 0; i != variables.length; i++) {
110 // TemplatePosition variable= variables[i];
112 // if (variable.isResolved())
115 // int[] offsets= variable.getOffsets();
116 // int length= variable.getLength();
118 // for (int j= 0; j != offsets.length; j++)
119 // manager.addPosition(offsets[j] + start, length);
122 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
123 switch (fLocation.getType()) {
124 case PHPIdentifierLocation.FUNCTION:
126 .setFinalCaretOffset(fIdentifierName.length() + start
129 case PHPIdentifierLocation.CONSTRUCTOR:
131 .setFinalCaretOffset(fIdentifierName.length() + start
134 case PHPIdentifierLocation.METHOD:
136 .setFinalCaretOffset(fIdentifierName.length() + start
141 editor.setFinalCaretOffset(fIdentifierName.length() + start);
145 fSelectedRegion = editor.getSelectedRegion();
147 } catch (BadLocationException e) {
148 PHPeclipsePlugin.log(e);
152 // catch (CoreException e) {
153 // handleException(e);
158 * @see ICompletionProposal#getAdditionalProposalInfo()
160 public String getAdditionalProposalInfo() {
162 fInfo = computeProposalInfo();
167 private String computeProposalInfo() {
168 StringBuffer hoverInfoBuffer = new StringBuffer();
169 // String workspaceLocation =
170 // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
171 String workspaceLocation;
172 if (fProject != null) {
173 workspaceLocation = fProject.getLocation().toString() + '/';
175 // should never happen?
176 workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
177 .getLocation().toString();
179 String filename = workspaceLocation + fLocation.getFilename();
180 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
181 return hoverInfoBuffer.toString();
184 public IContextInformation getContextInformation() {
185 if (fContextInfo == null) {
186 if (fLocation != null) {
187 fInfo = fLocation.getUsage();
189 // extract the parameter context information for the
191 int i0 = fInfo.indexOf('(');
192 int newline = fInfo.indexOf('\n');
193 if (i0 >= 0 && (i0 < newline || newline < 0)) {
194 int i1 = fInfo.indexOf(')', i0 + 1);
197 fContextInfo = new ContextInformation(null, fInfo
198 .substring(i0 + 1, i1));
200 fContextInfo = new ContextInformation(null, fInfo);
203 fContextInfo = new ContextInformation(null, fInfo);
212 * @see ICompletionProposal#getDisplayString()
214 public String getDisplayString() {
215 String workspaceLocation;
216 if (fProject != null) {
217 workspaceLocation = fProject.getName().toString() + '/';
219 // should never happen?
220 workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
221 .getLocation().toString();
223 String filename = workspaceLocation + fLocation.getFilename();
224 String usage = PHPDocUtil.getUsage(filename, fLocation);
225 String result = fIdentifierName
226 + TemplateMessages.getString("TemplateProposal.delimiter");
227 if (usage.length() > 0) {
229 + TemplateMessages.getString("TemplateProposal.delimiter");
236 * @see ICompletionProposal#getImage()
238 public Image getImage() {
239 switch (fLocation.getType()) {
240 case PHPIdentifierLocation.FUNCTION:
241 return PHPUiImages.get(PHPUiImages.IMG_FUN);
242 case PHPIdentifierLocation.CLASS:
243 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
244 case PHPIdentifierLocation.CONSTRUCTOR:
245 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
246 case PHPIdentifierLocation.METHOD:
247 return PHPUiImages.get(PHPUiImages.IMG_FUN);
248 case PHPIdentifierLocation.DEFINE:
249 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
250 case PHPIdentifierLocation.VARIABLE:
251 return PHPUiImages.get(PHPUiImages.IMG_VAR);
252 case PHPIdentifierLocation.GLOBAL_VARIABLE:
253 return PHPUiImages.get(PHPUiImages.IMG_VAR);
255 return PHPUiImages.get(PHPUiImages.IMG_FUN);
259 * @see IJavaCompletionProposal#getRelevance()
261 public int getRelevance() {
263 if (fContext instanceof JavaContext) {
264 JavaContext context = (JavaContext) fContext;
265 switch (context.getCharacterBeforeStart()) {
266 // high relevance after whitespace