Defined a limit for code completion list entries PHPeclipsePlugin.MAX_PROPOSALS
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / DeclarationProposal.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.phpdoc.PHPDocUtil;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
9 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
10 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
11 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
12 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
13 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
14 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.contentassist.IContextInformation;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.widgets.Shell;
28 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
29 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
30 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
31
32 /**
33  * A PHP identifier proposal.
34  */
35 public class DeclarationProposal implements IPHPCompletionProposal {
36
37   private final String fTemplate;
38   private final PHPIdentifierLocation fLocation;
39   private final TemplateContext fContext;
40   private final ITextViewer fViewer;
41   //  private final Image fImage_fun;
42   //  private final Image fImage_var;
43   private final IRegion fRegion;
44
45   //private TemplateBuffer fTemplateBuffer;
46   private String fOldText;
47   private IRegion fSelectedRegion; // initialized by apply()
48
49   /**
50    * Creates a template proposal with a template and its context.
51    * @param template  the template
52    * @param context   the context in which the template was requested.
53    * @param image     the icon of the proposal.
54    */
55   public DeclarationProposal(
56     String template,
57     PHPIdentifierLocation location,
58     TemplateContext context,
59     IRegion region,
60     ITextViewer viewer) {
61     //    Image image_fun,
62     //    Image image_var) {
63     fTemplate = template;
64     fLocation = location;
65     fContext = context;
66     fViewer = viewer;
67     //    fImage_fun = image_fun;
68     //    fImage_var = image_var;
69     fRegion = region;
70   }
71
72   /*
73    * @see ICompletionProposal#apply(IDocument)
74    */
75   public void apply(IDocument document) {
76     try {
77       //                    if (fTemplateBuffer == null)
78       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
79
80       int start = fRegion.getOffset();
81       int end = fRegion.getOffset() + fRegion.getLength();
82
83       // insert template string
84       //  String templateString = fTemplate; // fTemplateBuffer.getString();    
85       document.replace(start, end - start, fTemplate);
86
87       // translate positions
88       LinkedPositionManager manager = new LinkedPositionManager(document);
89       //                        TemplatePosition[] variables= fTemplateBuffer.getVariables();
90       //                        for (int i= 0; i != variables.length; i++) {
91       //                                TemplatePosition variable= variables[i];
92       //
93       //                                if (variable.isResolved())
94       //                                        continue;
95       //                                
96       //                                int[] offsets= variable.getOffsets();
97       //                                int length= variable.getLength();
98       //                                
99       //                                for (int j= 0; j != offsets.length; j++)
100       //                                        manager.addPosition(offsets[j] + start, length);
101       //                        }
102
103       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
104       editor.setFinalCaretOffset(fTemplate.length() + start);
105       //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
106       editor.enter();
107
108       fSelectedRegion = editor.getSelectedRegion();
109
110     } catch (BadLocationException e) {
111       PHPeclipsePlugin.log(e);
112       openErrorDialog(e);
113
114     }
115     //      catch (CoreException e) {
116     //                  handleException(e);
117     //      }       
118   }
119
120   //  private static int getCaretOffset(TemplateBuffer buffer) {
121   //    TemplatePosition[] variables = buffer.getVariables();
122   //    for (int i = 0; i != variables.length; i++) {
123   //      TemplatePosition variable = variables[i];
124   //
125   //      if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
126   //        return variable.getOffsets()[0];
127   //    }
128   //
129   //    return buffer.getString().length();
130   //  }
131
132   /*
133    * @see ICompletionProposal#getSelection(IDocument)
134    */
135   public Point getSelection(IDocument document) {
136     return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
137     //    return null;
138   }
139
140   /*
141    * @see ICompletionProposal#getAdditionalProposalInfo()
142    */
143   public String getAdditionalProposalInfo() {
144     StringBuffer hoverInfoBuffer = new StringBuffer();
145     String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
146     String filename = workspaceLocation + fLocation.getFilename();
147     PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
148     return textToHTML(hoverInfoBuffer.toString());
149   }
150
151   /*
152    * @see ICompletionProposal#getDisplayString()
153    */
154   public String getDisplayString() {
155     return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
156     //          return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
157   }
158
159   /*
160    * @see ICompletionProposal#getImage()
161    */
162   public Image getImage() {
163     switch (fLocation.getType()) {
164       case PHPIdentifierLocation.FUNCTION :
165         return PHPUiImages.get(PHPUiImages.IMG_FUN);
166                         case PHPIdentifierLocation.CLASS :
167                                 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
168                         case PHPIdentifierLocation.METHOD :
169                                 return PHPUiImages.get(PHPUiImages.IMG_FUN);
170                         case PHPIdentifierLocation.DEFINE :
171                                 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
172                         case PHPIdentifierLocation.VARIABLE :
173                                 return PHPUiImages.get(PHPUiImages.IMG_VAR);
174     }
175     return PHPUiImages.get(PHPUiImages.IMG_FUN);
176   }
177
178   /*
179    * @see ICompletionProposal#getContextInformation()
180    */
181   public IContextInformation getContextInformation() {
182     return null;
183   }
184
185   private static String textToHTML(String string) {
186     StringBuffer buffer = new StringBuffer(string.length());
187     buffer.append("<pre>"); //$NON-NLS-1$
188
189     for (int i = 0; i != string.length(); i++) {
190       char ch = string.charAt(i);
191
192       switch (ch) {
193         case '&' :
194           buffer.append("&amp;"); //$NON-NLS-1$
195           break;
196
197         case '<' :
198           buffer.append("&lt;"); //$NON-NLS-1$
199           break;
200
201         case '>' :
202           buffer.append("&gt;"); //$NON-NLS-1$
203           break;
204
205         case '\t' :
206           buffer.append("    "); //$NON-NLS-1$
207           break;
208
209         case '\n' :
210           buffer.append("<br>"); //$NON-NLS-1$
211           break;
212
213         default :
214           buffer.append(ch);
215           break;
216       }
217     }
218
219     buffer.append("</pre>"); //$NON-NLS-1$
220     return buffer.toString();
221   }
222
223   private void openErrorDialog(BadLocationException e) {
224     Shell shell = fViewer.getTextWidget().getShell();
225     MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
226   }
227
228   private void handleException(CoreException e) {
229     Shell shell = fViewer.getTextWidget().getShell();
230     PHPeclipsePlugin.log(e);
231     //          ExceptionHandler.handle(e, shell, ObfuscatorMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
232   }
233
234   /*
235    * @see IJavaCompletionProposal#getRelevance()
236    */
237   public int getRelevance() {
238
239     if (fContext instanceof PHPUnitContext) {
240       PHPUnitContext context = (PHPUnitContext) fContext;
241       switch (context.getCharacterBeforeStart()) {
242         // high relevance after whitespace
243         case ' ' :
244         case '\r' :
245         case '\n' :
246         case '\t' :
247           return 90;
248
249         default :
250           return 0;
251       }
252     } else {
253       return 90;
254     }
255   }
256
257 }