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;
15 import net.sourceforge.phpeclipse.ui.WebUI;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.contentassist.ContextInformation;
23 import org.eclipse.jface.text.contentassist.IContextInformation;
24 import org.eclipse.jface.text.templates.TemplateContext;
25 import org.eclipse.swt.graphics.Image;
27 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
28 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
29 // import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
32 * A PHP identifier proposal.
34 public class DeclarationProposal extends AbstractProposal { // implements
35 // IPHPCompletionProposal
37 private IProject fProject;
39 private final TemplateContext fContext;
41 private final PHPIdentifierLocation fLocation;
45 // private TemplateBuffer fTemplateBuffer;
46 // private String fOldText;
47 // private final Image fImage_fun;
48 // private final Image fImage_var;
49 private final IRegion fRegion;
51 // private IRegion fSelectedRegion; // initialized by apply()
53 private final String fIdentifierName;
55 // private final ITextViewer fViewer;
58 * Creates a template proposal with a template and its context.
63 * the context in which the template was requested.
65 * the icon of the proposal.
67 public DeclarationProposal(IProject project, String identifierName,
68 PHPIdentifierLocation location, TemplateContext context,
69 IRegion region, ITextViewer viewer) {
74 fIdentifierName = identifierName;
82 * @see ICompletionProposal#apply(IDocument)
84 public void apply(IDocument document) {
86 // if (fTemplateBuffer == null)
87 // fTemplateBuffer= fContext.evaluate(fTemplate);
89 int start = fRegion.getOffset();
90 int end = fRegion.getOffset() + fRegion.getLength();
92 switch (fLocation.getType()) {
93 case PHPIdentifierLocation.FUNCTION:
94 document.replace(start, end - start, fIdentifierName + "()");
96 case PHPIdentifierLocation.CONSTRUCTOR:
97 document.replace(start, end - start, fIdentifierName + "()");
99 case PHPIdentifierLocation.METHOD:
100 document.replace(start, end - start, fIdentifierName + "()");
104 document.replace(start, end - start, fIdentifierName);
107 // translate positions
108 LinkedPositionManager manager = new LinkedPositionManager(document);
109 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
110 // for (int i= 0; i != variables.length; i++) {
111 // TemplatePosition variable= variables[i];
113 // if (variable.isResolved())
116 // int[] offsets= variable.getOffsets();
117 // int length= variable.getLength();
119 // for (int j= 0; j != offsets.length; j++)
120 // manager.addPosition(offsets[j] + start, length);
123 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
124 switch (fLocation.getType()) {
125 case PHPIdentifierLocation.FUNCTION:
127 .setFinalCaretOffset(fIdentifierName.length() + start
130 case PHPIdentifierLocation.CONSTRUCTOR:
132 .setFinalCaretOffset(fIdentifierName.length() + start
135 case PHPIdentifierLocation.METHOD:
137 .setFinalCaretOffset(fIdentifierName.length() + start
142 editor.setFinalCaretOffset(fIdentifierName.length() + start);
146 fSelectedRegion = editor.getSelectedRegion();
148 } catch (BadLocationException e) {
153 // catch (CoreException e) {
154 // handleException(e);
159 * @see ICompletionProposal#getAdditionalProposalInfo()
161 public String getAdditionalProposalInfo() {
163 fInfo = computeProposalInfo();
168 private String computeProposalInfo() {
169 StringBuffer hoverInfoBuffer = new StringBuffer();
170 // String workspaceLocation =
171 // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
172 String workspaceLocation;
173 if (fProject != null) {
174 workspaceLocation = fProject.getLocation().toString() + '/';
176 // should never happen?
177 workspaceLocation = WebUI.getWorkspace().getRoot()
178 .getFullPath().toString();
180 String filename = workspaceLocation + fLocation.getFilename();
181 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
182 return hoverInfoBuffer.toString();
185 public IContextInformation getContextInformation() {
186 if (fContextInfo == null) {
187 if (fLocation != null) {
188 fInfo = fLocation.getUsage();
190 // extract the parameter context information for the
192 int i0 = fInfo.indexOf('(');
193 int newline = fInfo.indexOf('\n');
194 if (i0 >= 0 && (i0 < newline || newline < 0)) {
195 int i1 = fInfo.indexOf(')', i0 + 1);
198 fContextInfo = new ContextInformation(null, fInfo
199 .substring(i0 + 1, i1));
201 fContextInfo = new ContextInformation(null, fInfo);
204 fContextInfo = new ContextInformation(null, fInfo);
213 * @see ICompletionProposal#getDisplayString()
215 public String getDisplayString() {
216 String workspaceLocation;
217 String workspaceName;
218 if (fProject != null) {
219 workspaceLocation = fProject.getFullPath().toString() + '/';
220 workspaceName = fProject.getName().toString() + '/';
222 // should never happen?
223 workspaceLocation = WebUI.getWorkspace().getRoot()
224 .getFullPath().toString();
225 workspaceName = workspaceLocation;
227 String filename = fLocation.getFilename();
228 String usage = PHPDocUtil.getUsage(workspaceLocation + filename, fLocation);
229 String result = fIdentifierName
230 + TemplateMessages.getString("TemplateProposal.delimiter");
231 if (usage.length() > 0) {
233 + TemplateMessages.getString("TemplateProposal.delimiter");
235 result += workspaceName + filename;
240 * @see ICompletionProposal#getImage()
242 public Image getImage() {
243 switch (fLocation.getType()) {
244 case PHPIdentifierLocation.FUNCTION:
245 return PHPUiImages.get(PHPUiImages.IMG_FUN);
246 case PHPIdentifierLocation.CLASS:
247 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
248 case PHPIdentifierLocation.CONSTRUCTOR:
249 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
250 case PHPIdentifierLocation.METHOD:
251 return PHPUiImages.get(PHPUiImages.IMG_FUN);
252 case PHPIdentifierLocation.DEFINE:
253 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
254 case PHPIdentifierLocation.VARIABLE:
255 return PHPUiImages.get(PHPUiImages.IMG_VAR);
256 case PHPIdentifierLocation.GLOBAL_VARIABLE:
257 return PHPUiImages.get(PHPUiImages.IMG_VAR);
259 return PHPUiImages.get(PHPUiImages.IMG_FUN);
263 * @see IJavaCompletionProposal#getRelevance()
265 public int getRelevance() {
267 if (fContext instanceof JavaContext) {
268 JavaContext context = (JavaContext) fContext;
269 switch (context.getCharacterBeforeStart()) {
270 // high relevance after whitespace