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 String workspaceName;
217 if (fProject != null) {
218 workspaceLocation = fProject.getLocation().toString() + '/';
219 workspaceName = fProject.getName().toString() + '/';
221 // should never happen?
222 workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
223 .getLocation().toString();
224 workspaceName = workspaceLocation;
226 String filename = fLocation.getFilename();
227 String usage = PHPDocUtil.getUsage(workspaceLocation + filename, fLocation);
228 String result = fIdentifierName
229 + TemplateMessages.getString("TemplateProposal.delimiter");
230 if (usage.length() > 0) {
232 + TemplateMessages.getString("TemplateProposal.delimiter");
234 result += workspaceName + filename;
239 * @see ICompletionProposal#getImage()
241 public Image getImage() {
242 switch (fLocation.getType()) {
243 case PHPIdentifierLocation.FUNCTION:
244 return PHPUiImages.get(PHPUiImages.IMG_FUN);
245 case PHPIdentifierLocation.CLASS:
246 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
247 case PHPIdentifierLocation.CONSTRUCTOR:
248 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
249 case PHPIdentifierLocation.METHOD:
250 return PHPUiImages.get(PHPUiImages.IMG_FUN);
251 case PHPIdentifierLocation.DEFINE:
252 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
253 case PHPIdentifierLocation.VARIABLE:
254 return PHPUiImages.get(PHPUiImages.IMG_VAR);
255 case PHPIdentifierLocation.GLOBAL_VARIABLE:
256 return PHPUiImages.get(PHPUiImages.IMG_VAR);
258 return PHPUiImages.get(PHPUiImages.IMG_FUN);
262 * @see IJavaCompletionProposal#getRelevance()
264 public int getRelevance() {
266 if (fContext instanceof JavaContext) {
267 JavaContext context = (JavaContext) fContext;
268 switch (context.getCharacterBeforeStart()) {
269 // high relevance after whitespace