shortened display text of variable proposal in content assist
[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.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
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.IContextInformation;
22 import org.eclipse.jface.text.templates.TemplateContext;
23 import org.eclipse.swt.graphics.Image;
24 //import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
25 //import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
26 //import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
27
28 /**
29  * A PHP identifier proposal.
30  */
31 public class DeclarationProposal extends AbstractProposal { //implements IPHPCompletionProposal {
32   private IProject fProject;
33   private final TemplateContext fContext;
34   private final PHPIdentifierLocation fLocation;
35
36   //private TemplateBuffer fTemplateBuffer;
37   // private String fOldText;
38   //  private final Image fImage_fun;
39   //  private final Image fImage_var;
40   private final IRegion fRegion;
41   //  private IRegion fSelectedRegion; // initialized by apply()
42
43   private final String fIdentifierName;
44   //  private final ITextViewer fViewer;
45
46   /**
47    * Creates a template proposal with a template and its context.
48    * @param template  the template
49    * @param context   the context in which the template was requested.
50    * @param image     the icon of the proposal.
51    */
52   public DeclarationProposal(
53           IProject project,
54         String identifierName,
55         PHPIdentifierLocation location,
56         TemplateContext context,
57         IRegion region,
58         ITextViewer viewer) {
59         super(viewer);
60         //    Image image_fun,
61         //    Image image_var) {
62         fProject = project;
63         fIdentifierName = identifierName;
64         fLocation = location;
65         fContext = context;
66         fRegion = region;
67   }
68
69   /*
70    * @see ICompletionProposal#apply(IDocument)
71    */
72   public void apply(IDocument document) {
73         try {
74           //                if (fTemplateBuffer == null)
75           //                            fTemplateBuffer= fContext.evaluate(fTemplate);
76
77           int start = fRegion.getOffset();
78           int end = fRegion.getOffset() + fRegion.getLength();
79
80           switch (fLocation.getType()) {
81                 case PHPIdentifierLocation.FUNCTION :
82                   document.replace(start, end - start, fIdentifierName + "()");
83                   break;
84                 case PHPIdentifierLocation.CONSTRUCTOR :
85                   document.replace(start, end - start, fIdentifierName + "()");
86                   break;
87                 case PHPIdentifierLocation.METHOD :
88                   document.replace(start, end - start, fIdentifierName + "()");
89                   break;
90
91                 default :
92                   document.replace(start, end - start, fIdentifierName);
93           }
94
95           // translate positions
96           LinkedPositionManager manager = new LinkedPositionManager(document);
97           //                    TemplatePosition[] variables= fTemplateBuffer.getVariables();
98           //                    for (int i= 0; i != variables.length; i++) {
99           //                            TemplatePosition variable= variables[i];
100           //
101           //                            if (variable.isResolved())
102           //                                    continue;
103           //
104           //                            int[] offsets= variable.getOffsets();
105           //                            int length= variable.getLength();
106           //
107           //                            for (int j= 0; j != offsets.length; j++)
108           //                                    manager.addPosition(offsets[j] + start, length);
109           //                    }
110
111           LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
112           switch (fLocation.getType()) {
113                 case PHPIdentifierLocation.FUNCTION :
114                   editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
115                   break;
116                 case PHPIdentifierLocation.CONSTRUCTOR :
117                   editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
118                   break;
119                 case PHPIdentifierLocation.METHOD :
120                   editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
121                   break;
122
123                 default :
124                   editor.setFinalCaretOffset(fIdentifierName.length() + start);
125           }
126           editor.enter();
127
128           fSelectedRegion = editor.getSelectedRegion();
129
130         } catch (BadLocationException e) {
131           PHPeclipsePlugin.log(e);
132           openErrorDialog(e);
133
134         }
135         //      catch (CoreException e) {
136         //                      handleException(e);
137         //          }
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 workspaceLocation;
147         if (fProject!=null) {
148           workspaceLocation = fProject.getLocation().toString()+'/';
149         } else {
150           // should never happen?
151           workspaceLocation = PHPeclipsePlugin.getWorkspace()
152           .getRoot().getLocation().toString();
153         }
154         String filename = workspaceLocation + fLocation.getFilename();
155         PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
156         return hoverInfoBuffer.toString();
157   }
158
159   /*
160    * @see ICompletionProposal#getContextInformation()
161    */
162   public IContextInformation getContextInformation() {
163         return null;
164   }
165
166   /*
167          * @see ICompletionProposal#getDisplayString()
168          */
169         public String getDisplayString() {
170                 String workspaceLocation;
171                 if (fProject != null) {
172                         workspaceLocation = fProject.getName().toString() + '/';
173                 } else {
174                         // should never happen?
175                         workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
176                                         .getLocation().toString();
177                 }
178                 String filename = workspaceLocation + fLocation.getFilename();
179                 String usage = PHPDocUtil.getUsage(filename, fLocation);
180                 String result = fIdentifierName
181                                 + TemplateMessages.getString("TemplateProposal.delimiter");
182                 if (usage.length() > 0) {
183                         result += usage
184                                         + TemplateMessages.getString("TemplateProposal.delimiter");
185                 }
186                 result += filename;
187                 return result;
188         }
189
190   /*
191    * @see ICompletionProposal#getImage()
192    */
193   public Image getImage() {
194         switch (fLocation.getType()) {
195           case PHPIdentifierLocation.FUNCTION :
196                 return PHPUiImages.get(PHPUiImages.IMG_FUN);
197           case PHPIdentifierLocation.CLASS :
198                 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
199           case PHPIdentifierLocation.CONSTRUCTOR :
200                 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
201           case PHPIdentifierLocation.METHOD :
202                 return PHPUiImages.get(PHPUiImages.IMG_FUN);
203           case PHPIdentifierLocation.DEFINE :
204                 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
205           case PHPIdentifierLocation.VARIABLE :
206                 return PHPUiImages.get(PHPUiImages.IMG_VAR);
207           case PHPIdentifierLocation.GLOBAL_VARIABLE :
208                 return PHPUiImages.get(PHPUiImages.IMG_VAR);
209         }
210         return PHPUiImages.get(PHPUiImages.IMG_FUN);
211   } 
212
213   /*
214    * @see IJavaCompletionProposal#getRelevance()
215    */
216   public int getRelevance() {
217
218         if (fContext instanceof JavaContext) {
219           JavaContext context = (JavaContext) fContext;
220           switch (context.getCharacterBeforeStart()) {
221                 // high relevance after whitespace
222                 case ' ' :
223                 case '\r' :
224                 case '\n' :
225                 case '\t' :
226                   return 80;
227                 case '>' : // ->
228                 case ':' : // ::
229                   return 85;
230                 default :
231                   return 0;
232           }
233         } else {
234           return 80;
235         }
236   }
237
238 }